配置corejs版本问题解决

自己写的脚手架工具 npm i -g xd-es-cli
报错 =>
直接使用@babel/core

    "@babel/core": "^7.12.3",

执行如下代码报错 =>

let {name, age = 18} = {
    name: "xxx",
    age: 28
}
console.log(name, age)

报错 =>

i 「wds」: Project is running at http://localhost:8080/
i 「wds」: webpack output is served from undefined
i 「wds」: Content not from webpack is served from H:\ld\task\code\ECMScript\mk02\es-demo

WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:

  npm install --save core-js@2    npm install --save core-js@3
  yarn add core-js@2              yarn add core-js@3

(node:17604) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(Use `node --trace-deprecation ...` to show where the warning was created)
× 「wdm」: assets by path static/ 15.1 KiB
  asset static/favicon.ico 13.3 KiB [emitted] [from: static/favicon.ico] [copied]
  asset static/1-1.js 1.07 KiB [emitted] [from: static/1-1.js] [copied]
  asset static/1-2.js 750 bytes [emitted] [from: static/1-2.js] [copied]
asset index.js 896 KiB [emitted] (name: main)
asset index.html 310 bytes [emitted]
runtime modules 704 bytes 4 modules
modules by path ./node_modules/ 330 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
  modules by path ./node_modules/html-entities/lib/*.js 57.9 KiB 4 modules
  modules by path ./node_modules/webpack/hot/ 1.58 KiB 3 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB
    ./node_modules/querystring/index.js 127 bytes [built] [code generated]
    ./node_modules/querystring/decode.js 2.34 KiB [built] [code generated]
    ./node_modules/querystring/encode.js 2.04 KiB [built] [code generated]
modules by path ./src/*.js 1.34 KiB
  ./src/index.js 98 bytes [built] [code generated]
  ./src/1-3.js 1.25 KiB [built] [code generated]

ERROR in ./src/1-3.js 1:0-43
Module not found: Error: Can't resolve 'core-js/modules/es6.function.name' in 'H:\ld\task\code\ECMScript\mk02\es-demo\src'
 @ ./src/index.js 4:0-15

webpack 5.4.0 compiled with 1 error in 1888 ms
i 「wdm」: Failed to compile.

参考 => https://github.com/nuxt/nuxt.js/issues/5287
图片描述
修改corejs版本

"plugin": [
  ["@babel/plugin-transform-runtime", {
  "corejs":2
}]
]

报错

i 「wds」: Project is running at http://localhost:8080/
i 「wds」: webpack output is served from undefined
i 「wds」: Content not from webpack is served from H:\ld\task\code\ECMScript\mk02\es-demo

WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:

  npm install --save core-js@2    npm install --save core-js@3
  yarn add core-js@2              yarn add core-js@3

(node:9088) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(Use `node --trace-deprecation ...` to show where the warning was created)
× 「wdm」: assets by path static/ 15.1 KiB
  asset static/favicon.ico 13.3 KiB [emitted] [from: static/favicon.ico] [copied]
  asset static/1-1.js 1.07 KiB [emitted] [from: static/1-1.js] [copied]
  asset static/1-2.js 750 bytes [emitted] [from: static/1-2.js] [copied]
asset index.js 891 KiB [emitted] (name: main)
asset index.html 310 bytes [emitted]
runtime modules 430 bytes 3 modules
cacheable modules 330 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
  modules by path ./node_modules/html-entities/lib/*.js 57.9 KiB 4 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB
    ./node_modules/querystring/index.js 127 bytes [built] [code generated]
    ./node_modules/querystring/decode.js 2.34 KiB [built] [code generated]
    ./node_modules/querystring/encode.js 2.04 KiB [built] [code generated]
  modules by path ./node_modules/webpack/hot/*.js 1.42 KiB
    ./node_modules/webpack/hot/emitter.js 75 bytes [built] [code generated]
    ./node_modules/webpack/hot/log.js 1.34 KiB [built] [code generated]
./node_modules/webpack/hot/ sync nonrecursive ^\.\/log$ 170 bytes [built] [code generated]

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] H:\ld\task\code\ECMScript\mk02\es-demo\src\index.js: .corejs is not a valid Plugin property
- Maybe you meant to use
"plugin": [
  ["@babel/plugin-transform-runtime", {
  "corejs": 3
}]
]
To be a valid plugin, its name and options should be wrapped in a pair of brackets
    at H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\validation\plugins.js:65:42
    at Array.forEach (<anonymous>)
    at validatePluginObject (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\validation\plugins.js:54:20)
    at H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\full.js:256:55
    at Generator.next (<anonymous>)
    at Function.<anonymous> (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\gensync-utils\async.js:26:3)
    at Generator.next (<anonymous>)
    at step (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:261:32)
    at evaluateAsync (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:291:5)
    at Function.errback (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:113:7)

webpack 5.4.0 compiled with 1 error in 1773 ms

参考 => https://github.com/babel/babel/issues/8451
图片描述
正确配置方式 =>
es-demo\build\webpack.base.config.js

    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: [
                        ['@babel/preset-env', {
                            "useBuiltIns": "usage"
                        }]
                    ],
                    plugins: [[
                        '@babel/plugin-transform-runtime',
                        {
                            corejs: { version: 2 } // 指定 runtime-corejs 的版本,目前有 2 3 两个版本
                        }
                    ]]
                }
            }
        }]
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值