【问题标题】:Webpack and Babel not transpiling a dependency inside node_modules that breaks IE 11 and Edge due to ES6's spread syntax由于 ES6 的扩展语法,Webpack 和 Babel 没有在 node_modules 中转换破坏 IE 11 和 Edge 的依赖项
【发布时间】:2020-06-09 05:24:04
【问题描述】:

我有一个使用 @mdx-js/runtime 的项目,它在 IE 11 或 Edge (Edge 44.18362.449.0) 上完全崩溃:

SCRIPT1028: SCRIPT1028: Expected identifier, string or number

由于这里的扩展语法,它实际上会中断:

const allNodes = sortedNodes.map(({ start: _, ...node }, i) => { 

这行代码来自remark-mdx,是@mdx-js/runtime的依赖,特别是这个文件和行:https://github.com/mdx-js/mdx/blob/master/packages/remark-mdx/extract-imports-and-exports.js#L66

我一直在尝试让 Webpack 和 Babel 转换该文件,以便不再存在传播:

浏览器列表:

如果我运行 npx browserslist 我可以看到 IE 11 在那里。

"browserslist": [
    "> 0.5%",
    "last 2 version",
    "Firefox ESR",
    "not dead",
    "iOS >= 9"
]

.babelrc:

我尝试禁用loose 模式并添加@babel/plugin-transform-spread@babel/plugin-proposal-object-rest-spread,但没有解决问题。

  {
    "presets": [[
        "@babel/preset-env", {
            "useBuiltIns": "usage",
            "loose": false, // Was true before
            "modules": "auto",
            "corejs": 3
        }],
        "@babel/preset-react",
        "@babel/preset-typescript"
    ],

    "plugins": [
        ["@babel/plugin-proposal-decorators", {
            "legacy": true
        }],
        ["@babel/plugin-proposal-class-properties", {
            "loose": true
        }],
        "@babel/plugin-transform-spread", // Just added
        "@babel/plugin-proposal-object-rest-spread", // Just added
        "@babel/plugin-proposal-optional-chaining",
        "@babel/plugin-proposal-nullish-coalescing-operator",
        "react-hot-loader/babel"
    ]
}

webpack.config.js:

在这里,我尝试明确包含remark-mdx@mdx-js/runtime,并删除exclude 属性或将其更改为/node_modules\/(?!(remark-mdx|@mdx-js\/runtime)\/).*/,但似乎没有任何效果:

  {
    test: /\.(j|t)sx?$/,
    include: [
      path.resolve(__dirname, 'src'),
      // Tried explicitly adding these 2:
      path.resolve('node_modules/remark-mdx'),
      path.resolve('node_modules/@mdx-js/runtime'),
    ],
    // Tried removing `exclude` or not excluding those 2 packages:
    // exclude: /node_modules/,
    // exclude: /node_modules\/(?!(remark-mdx|@mdx-js\/runtime)\/).*/,
    use: [{
      loader: 'babel-loader',
      options: {
        cacheDirectory: true,
        babelrc: true,
      }
    }],
  }

我正在使用 Babel 7 和 Webpack 4。

【问题讨论】:

    标签: webpack ecmascript-6 babeljs babel-loader es6-modules


    【解决方案1】:

    好的,事实证明要处理node_modules 中的文件,您需要使用babel.config.js 而不是.babelrc,如herehere 所述:

    您的用例是什么?

    • 您使用的是 monorepo?
    • 你想编译 node_modules 吗?

      babel.config.json 是给你的!

    • 您的配置仅适用于项目的单个部分?

      .babelrc.json 是给你的!

    此外,如果您使用的是 monorepo,您​​需要设置 rootMode: 'upward' 以便 Babel 可以找到您的新配置文件,如 here 所述。

    Webpack 的 babel-loader 配置应该如下所示:

      {
        test: /\.(j|t)sx?$/,
        include: [
          path.resolve(__dirname, 'src'),
          // No need to add @mdx-js/runtime, just add the problematic package:
          path.resolve('node_modules/remark-mdx'),
        ],
        // You also need to exclude it from the exclusions:
        exclude: /node_modules\/(?!(remark-mdx)\/).*/,
        use: [{
          loader: 'babel-loader',
          options: {
            cacheDirectory: true,
            // And replace .babelrc with babel.config.json...
            babelrc: false,
            // ...which might also mean you need this if you are using a monorepo:
            rootMode: 'upward',
          }
        }],
      }
    

    在此更改之后,正在处理文件,但我得到了一个不同的错误:

    Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    

    在这种情况下,我必须将sourceType: "unambiguous" 添加到babel.config.json,因为remark-mdx 使用的是CommonJS 模块而不是ES6 模块。您可以将其添加到 babel.config.json 文件的根目录或 overrides 内,以便它仅针对 node_modules 内的包。

    有关发生这种情况的更多信息,请参阅How do I use babel's `useBuiltIns: 'usage'` option on the vendors bundle?

    Babel 的 babel.config.json 最终看起来像这样:

    {
        "presets": [[
            "@babel/preset-env", {
                "useBuiltIns": "usage",
                "loose": true,
                "modules": "auto",
                "corejs": 3
            }],
            "@babel/preset-react",
            "@babel/preset-typescript"
        ],
    
        "plugins": [
            ["@babel/plugin-proposal-decorators", {
                "legacy": true
            }],
            ["@babel/plugin-proposal-class-properties", {
                "loose": true
            }],
            "@babel/plugin-proposal-optional-chaining",
            "@babel/plugin-proposal-nullish-coalescing-operator",
            "react-hot-loader/babel"
        ],
    
        "overrides": [{
            "test": "./node_modules",
            "sourceType": "unambiguous"
        }]
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2019-06-28
      • 2015-09-14
      • 2019-04-14
      • 2020-08-07
      • 1970-01-01
      • 2020-04-16
      • 2021-04-21
      相关资源
      最近更新 更多