由于 ES6 的扩展语法,Webpack 和 Babel 没有在 node_modules 内转译破坏 IE 11 和 Edge 的依赖项

Dan*_*ger 2 ecmascript-6 webpack babeljs es6-modules babel-loader

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

SCRIPT1028: SCRIPT1028: Expected identifier, string or number
Run Code Online (Sandbox Code Playgroud)

由于这里的传播语法,它实际上中断了:

const allNodes = sortedNodes.map(({ start: _, ...node }, i) => { 
Run Code Online (Sandbox Code Playgroud)

这行代码来自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"
]
Run Code Online (Sandbox Code Playgroud)

.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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

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,
      }
    }],
  }
Run Code Online (Sandbox Code Playgroud)

我正在使用 Babel 7 和 Webpack 4。

Dan*_*ger 12

好的,结果是处理node_modules您需要使用的文件,babel.config.js而不是.babelrc这里这里所解释的那样:

你的用例是什么?

  • 您正在使用 monorepo 吗?
  • 你想编译 node_modules 吗?

    babel.config.json 是给你的!

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

    .babelrc.json 是给你的!

此外,如果您使用的是monorepo你需要设置rootMode: 'upward'让巴贝尔能够找到新的配置文件,如解释在这里

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',
      }
    }],
  }
Run Code Online (Sandbox Code Playgroud)

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

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
Run Code Online (Sandbox Code Playgroud)

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

有关为什么会发生这种情况的更多信息,请参阅如何在供应商捆绑包上使用 babel 的 `useBuiltIns: 'usage'` 选项?.

Babelbabel.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"
    }]
}
Run Code Online (Sandbox Code Playgroud)