【问题标题】:IE11 error in bundle.js by invalid characterbundle.js 中的 IE11 错误由无效字符引起
【发布时间】:2019-11-10 05:46:38
【问题描述】:

我正在使用带有 awesome-typescript-loader 和 Webpack 的 typescript 进行编译。但在 IE11 中它不起作用。我有这个错误。

SCRIPT1014

bundle.js (58458,12)

在这一行我有这个。我认为问题出在性格上`

const v6 = `

这是我对 tsconfig.json 的配置

{
  "compilerOptions": {
    "target": "es6",
    "module": "es6",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "declaration": false,
    "esModuleInterop": true,
    "noImplicitAny": false,
    "jsx": "react",
    "sourceMap": true,
    "noLib": false,
    "suppressImplicitAnyIndexErrors": true,
    "baseUrl": "./src/",
    "paths": {
      "@pages": ["./pages/*"],
      "@core": ["./core/*"],
      "@pods": ["./pods/*"],
      "@common": ["./common/*"],
      "@state": ["./state/*"]
    },
    "skipLibCheck": true
  },
  "compileOnSave": false,
  "exclude": ["node_modules"]
}

这是我的 webpack.config.js

    var HtmlWebpackPlugin = require("html-webpack-plugin");
var MiniCssExtractPlugin = require("mini-css-extract-plugin");
var webpack = require("webpack");
var path = require("path");

var basePath = __dirname;

module.exports = {
  context: path.join(basePath, "src"),
  resolve: {
    extensions: [".js", ".ts", ".tsx"],
    alias: {
      // Later on we will add more aliases here
      pages: path.resolve(__dirname, "./src/pages/"),
      core: path.resolve(__dirname, "./src/core/"),
      pods: path.resolve(__dirname, "./src/pods/"),
      common: path.resolve(__dirname, "./src/common/"),
      state: path.resolve(__dirname, "./src/state/"),
    }
  },
  entry: ["@babel/polyfill", "./index.tsx"],
  output: {
    path: path.join(basePath, "dist"),
    filename: "bundle.js"
  },
  devtool: "source-map",
  devServer: {
    contentBase: "./dist", // Content base
    inline: true, // Enable watch and live reload
    host: "test",
    port: 8080,
    stats: "errors-only",
    disableHostCheck: true,
    historyApiFallback: true,
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        loader: "awesome-typescript-loader",
        options: {
          useBabel: true,
          babelCore: "@babel/core" // needed for Babel v7
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: "file-loader",
        options: {
          name: "assets/img/[name].[ext]?[hash]"
        }
      }
    ]
  },
  plugins: [
    //Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: "index.html", //Name of file in ./dist/
      template: "index.html", //Name of template in ./src
      hash: true
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
};

通过 .babelrc 文件

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry"
      }
    ]
  ]
}

我对这个问题没有更多的想法。

【问题讨论】:

  • 入口中不需要添加@babel/polyfill。 .babelrc 文件应该看起来像这样 { "presets": [ [ "@babel/preset-env", { "useBuiltIns": "usage", "corejs": 3 } ] ] }
  • 尝试将目标设置为具体包括 ie11:babeljs.io/docs/en/babel-preset-env#targets 具体来说,它说“旁注,如果没有指定目标,@babel/preset-env 将默认转换所有 ECMAScript 2015+ 代码。” .不确定这是否包括 es2015。如果假定支持 es2015,它将留下反引号。这会破坏一切。

标签: typescript webpack internet-explorer-11 babeljs tsconfig


【解决方案1】:

请检查字符,可能您使用的是中文符号字符'`'。尝试将其更改为英文字符'''。

【讨论】:

    【解决方案2】:

    IE 11 不支持模板文字。 模板文字是 es6 语法,所以我建议你在 tsconfig 中使用 es5 目标

    如果您不确定每个浏览器可以使用什么,请查看here

    【讨论】:

    • 在 babel + core-js 的帮助下,你将 ES6 转换为 ES5,这也意味着字面量模板。
    • 这个例子适用于打字稿吗?我使用 awesome-typescript-loader 的加载器
    • 适用于 awesome-typescript-loader 和 babel + code-js github.com/tomik23/webpack-babel-corejs/tree/…
    • 我会让 typescript 编译器来完成这项工作。既然您已经必须将 typescript 编译为某些 javascript 版本,为什么不将其编译为 es5?将其编译为 es6,然后让 babel 将其转换为 es5 是过度工程......只是我的看法
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 2014-09-30
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多