由于 ES6 的扩展语法,Webpack 和 Babel 没有在 node_modules 内转译依赖项,这会破坏 IE 11 和 Edge
Webpack and Babel not transpiling a dependency inside node_modules that breaks IE 11 and Edge due to ES6's spread syntax
我有一个使用 @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。
好的,事实证明要处理 node_modules
中的文件,您需要使用 babel.config.js
而不是 .babelrc
,如 here and here:
所述
What's your use case?
- You are using a monorepo?
You want to compile node_modules?
babel.config.json
is for you!
You have a configuration that only applies to a single part of your project?
.babelrc.json
is for you!
此外,如果您使用的是 monorepo,则需要设置 rootMode: 'upward'
so that Babel can find your new config file, as explained 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"
to babel.config.json
, as remark-mdx
was using CommonJS modules rather than ES6 ones. You can add this at the root of your babel.config.json
file or inside overrides
,以便它只针对 node_modules
.
内的包
有关发生这种情况的原因的更多信息,请参阅 。
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"
}]
}
我有一个使用 @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。
好的,事实证明要处理 node_modules
中的文件,您需要使用 babel.config.js
而不是 .babelrc
,如 here and here:
What's your use case?
- You are using a monorepo?
You want to compile node_modules?
babel.config.json
is for you!You have a configuration that only applies to a single part of your project?
.babelrc.json
is for you!
此外,如果您使用的是 monorepo,则需要设置 rootMode: 'upward'
so that Babel can find your new config file, as explained 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"
to babel.config.json
, as remark-mdx
was using CommonJS modules rather than ES6 ones. You can add this at the root of your babel.config.json
file or inside overrides
,以便它只针对 node_modules
.
有关发生这种情况的原因的更多信息,请参阅
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"
}]
}