17

我升级了 babel6.x → 7.x但在运行Webpack时遇到问题。

它在抱怨失踪core-js/modules/*

babel.config.js的在根目录下。我将以前存在的转换.babelrc为 js(.babelrc也产生了相同的错误)。我猜这是与所有核心corejs2、运行时内容的一些冲突。

我的 src 中有两个应用程序,我的和 Styleguidist(在 中./node_modules)。我的应用程序转换并使用这些相同的package.json/babel.config,但 Styleguidist 没有。


使用 webpack 运行 Styleguidist 时的错误:

Module not found: Error: Can't resolve 'core-js/modules/es.array.concat' in '/project/src/node_modules/react-styleguidist/lib/client/rsg-components/Slot'

/node_modules/react-styleguidist/lib/client/rsg-components/Slot.js

import "core-js/modules/es.array.concat";
import "core-js/modules/es.array.filter";
...

包.json

"dependencies": {
    "@babel/polyfill": "^7.0.0",
    "@babel/runtime-corejs2": "^7.4.3",
}
"devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
    "@babel/plugin-proposal-function-sent": "^7.0.0",
    "@babel/plugin-proposal-json-strings": "^7.0.0",
    "@babel/plugin-proposal-numeric-separator": "^7.0.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.4.3",
    "@babel/plugin-proposal-throw-expressions": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/plugin-syntax-import-meta": "^7.0.0",
    "@babel/plugin-syntax-jsx": "^7.0.0",
    "@babel/plugin-transform-modules-commonjs": "^7.4.3",
    "@babel/plugin-transform-react-jsx": "^7.3.0",
    "@babel/plugin-transform-runtime": "^7.4.3",
    "@babel/preset-env": "^7.4.3",
    "@babel/register": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-jest": "^24.7.1",
    "babel-loader": "^8.0.0",
    "babel-plugin-dynamic-import-node": "^2.2.0",
    "babel-plugin-transform-vue-jsx": "^4.0.1",
}

babel.config.js

module.exports = {
    presets: ['@babel/preset-env'],
    plugins: [
        '@babel/plugin-transform-runtime',
        '@babel/plugin-transform-react-jsx',
        'transform-vue-jsx',
        "@babel/plugin-proposal-object-rest-spread",
        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-syntax-import-meta",
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-proposal-json-strings",
        [
            "@babel/plugin-proposal-decorators",
            {
                "legacy": true
            }
        ],
        "@babel/plugin-proposal-function-sent",
        "@babel/plugin-proposal-export-namespace-from",
        "@babel/plugin-proposal-numeric-separator",
        "@babel/plugin-proposal-throw-expressions"],
    comments: false
}
4

3 回答 3

21

引用Babel 7.4.0 发布

@babel/polyfill 不是插件或预设,而是运行时包:如果我们添加了在 core-js@2 和 core-js@3 之间切换的选项,则两个包版本都需要包含在您的包中。出于这个原因,我们决定弃用它:你现在应该为 polyfills 加载 core-js,如果你正在转换生成器,则应该加载 regenerator-runtime/runtime:

当您使用7.4.3babel 版本时,@babel/polyfill可能无法按预期工作。请改为core-js手动regenerator-runtime添加。引用core-js3发布公告

Instead of

import "@babel/polyfill";

you should use those 2 lines:

import "core-js/stable";
import "regenerator-runtime/runtime";

Don't forget install those dependencies directly!

npm i --save core-js regenerator-runtime
于 2019-06-06T09:37:16.547 回答
3

I had the same issue and as often happens I forgot to have another package installed as:

"@babel/runtime-corejs3": "^7.5.5",

Don't forgot to install it at same level where you have the issue(either dev, local or production) level:

 npm i -D(or --save-dev) @babel/runtime-corejs3

So in general this kind of errors happens when there are dependencies update change significantly in the version and aren't backward compatible with previous versions. Indeed corejs3 isn't at all compatible with corejs2 or older.

于 2019-08-31T15:20:00.097 回答
0

我找到了可能的答案。要解决此错误,您可以将 core-js 版本降级到 2.5.7。此版本生成正确的目录结构,具有单独的 ES6 和 ES7 文件夹。

要降级版本,只需运行:

npm i -S core-js@2.5.7

于 2019-05-14T07:57:38.597 回答