简体   繁体   English

Webpack 未捕获错误:模块解析失败:意外令牌

[英]Webpack Uncaught Error: Module parse failed: Unexpected token

This is a first I see this with Webpack, I'm trying out LitElement and trying to query my shadowRoot with getElementByID but this completly kills webpack for some reason like it needs a loader to parse the value of this basic javascript functionality.这是我第一次看到 Webpack,我正在尝试 LitElement 并尝试使用 getElementByID 查询我的 shadowRoot 但这完全杀死了 webpack 出于某种原因,例如它需要一个加载程序来解析这个基本 ZDE9B9ED780D7 的值

It should be said I use typescript, I have a small test case here:应该说我用的是typescript,我这里有个小测试用例:

App.ts应用程序.ts

import { customElement, html, LitElement } from "lit-element";

@customElement("open-studio")
export class OpenStudio extends LitElement {
    
    async onClick() {
        const textarea = this.shadowRoot?.getElementById("file-content")
    }

    render() {
        return html`
            <div>Open Studio</div>
            <button @click=${this.onClick}>Button</button>
            <textarea id="file-content"></textarea>
        `
    }
}

Webpack.config.js Webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(png|jpe?g|gif|svg|ttf|woff|woff2|eot)$/i,
        use: [
          { loader: 'file-loader' },
        ],
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  devServer: {
    port: 3000,
    hot: true,
    contentBase: path.resolve(__dirname,
      "public"),
    disableHostCheck: true,
    historyApiFallback: true
  },
};

Error thrown抛出错误

app.ts:1 Uncaught Error: Module parse failed: Unexpected token (13:41)
File was processed with these loaders:
 * ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| let OpenStudio = class OpenStudio extends lit_element_1.LitElement {
|     async onClick() {
>         const textarea = this.shadowRoot?.getElementById("file-content");
|     }

I've been looking up and down for the last few hours, but I can't find a solution to this, and I've never faced this before (In React), I doubt its a TypeScript or LitElements vs React issue, its more like a configuration of webpack I just don't know of or do not understand yet, does any one here have a solution for this.在过去的几个小时里我一直在上下寻找,但我找不到解决方案,而且我以前从未遇到过这个问题(在 React 中),我怀疑它是 TypeScript 或 LitElements vs React 问题,它更像是webpack的配置我只是不知道或还不明白,这里有没有人有解决方案。

You are using typescript, but putting it in a file called.js therefore the ts-loader is not transpiling it, and the first time it hits something non-javascripty it dies.您正在使用 typescript,但将其放在名为 .js 的文件中,因此 ts-loader 不会对其进行转换,并且第一次遇到非 javascripty 时它会死掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM