feat(webpack): auto detect core-js version (#7785)

* feat: add build option to set corejs version

* fix tests

* refactor: improve error handling and warn for invalid corejs version

* fix: only pick major part

* simplify as we are already using try/catch

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Ahad Birang 2020-07-27 18:06:46 +04:30 committed by GitHub
parent 5bbae3190d
commit 6a2576b6f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 3 deletions

View File

@ -73,7 +73,11 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"presets\\": Array [
Array [
\\"<nuxtDir>/packages/babel-preset-app/src/index.js\\",
Object {},
Object {
\\"corejs\\": Object {
\\"version\\": \\"2\\",
},
},
],
],
},
@ -648,7 +652,11 @@ exports[`webpack nuxt webpack module.rules test=.jsx 1`] = `
\\"presets\\": Array [
Array [
\\"<nuxtDir>/packages/babel-preset-app/src/index.js\\",
Object {},
Object {
\\"corejs\\": Object {
\\"version\\": \\"2\\",
},
},
],
],
},

View File

@ -75,6 +75,7 @@ export default () => ({
pages: true,
commons: true
},
corejs: 'auto',
babel: {
configFile: false,
babelrc: false,

View File

@ -31,6 +31,7 @@ Object {
"configFile": false,
},
"cache": false,
"corejs": "auto",
"cssSourceMap": false,
"devMiddleware": Object {},
"extractCSS": false,

View File

@ -16,6 +16,7 @@ Object {
"configFile": false,
},
"cache": false,
"corejs": "auto",
"cssSourceMap": undefined,
"devMiddleware": Object {},
"extractCSS": false,
@ -395,6 +396,7 @@ Object {
"configFile": false,
},
"cache": false,
"corejs": "auto",
"cssSourceMap": undefined,
"devMiddleware": Object {},
"extractCSS": false,

View File

@ -13,6 +13,7 @@ import semver from 'semver'
import { TARGETS, isUrl, urlJoin, getPKG } from '@nuxt/utils'
import createRequire from 'create-require'
import PerfLoader from '../utils/perf-loader'
import StyleLoader from '../utils/style-loader'
import WarningIgnorePlugin from '../plugins/warning-ignore'
@ -96,6 +97,7 @@ export default class WebpackBaseConfig {
getBabelOptions () {
const envName = this.name
const { buildOptions: { corejs }, options: { rootDir } } = this.buildContext
const options = {
...this.buildContext.buildOptions.babel,
envName
@ -114,7 +116,26 @@ export default class WebpackBaseConfig {
)
}
const defaultPreset = [require.resolve('@nuxt/babel-preset-app'), {}]
// Auto detect corejs version
let corejsVersion = corejs
if (corejsVersion === 'auto') {
try {
corejsVersion = createRequire(rootDir)('core-js').version.split('.')[0]
} catch (_err) {
corejsVersion = '2'
}
}
if (corejsVersion !== '2' && corejsVersion !== '3') {
consola.warn(`Invalid corejs version ${JSON.stringify(corejsVersion)}! Possible values are 2 and 3`)
corejsVersion = '2'
}
const defaultPreset = [require.resolve('@nuxt/babel-preset-app'), {
corejs: {
version: corejsVersion
}
}]
if (typeof options.presets === 'function') {
options.presets = options.presets(