mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
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:
parent
5bbae3190d
commit
6a2576b6f6
@ -73,7 +73,11 @@ exports[`webpack nuxt webpack module.rules 1`] = `
|
|||||||
\\"presets\\": Array [
|
\\"presets\\": Array [
|
||||||
Array [
|
Array [
|
||||||
\\"<nuxtDir>/packages/babel-preset-app/src/index.js\\",
|
\\"<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 [
|
\\"presets\\": Array [
|
||||||
Array [
|
Array [
|
||||||
\\"<nuxtDir>/packages/babel-preset-app/src/index.js\\",
|
\\"<nuxtDir>/packages/babel-preset-app/src/index.js\\",
|
||||||
Object {},
|
Object {
|
||||||
|
\\"corejs\\": Object {
|
||||||
|
\\"version\\": \\"2\\",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -75,6 +75,7 @@ export default () => ({
|
|||||||
pages: true,
|
pages: true,
|
||||||
commons: true
|
commons: true
|
||||||
},
|
},
|
||||||
|
corejs: 'auto',
|
||||||
babel: {
|
babel: {
|
||||||
configFile: false,
|
configFile: false,
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
|
@ -31,6 +31,7 @@ Object {
|
|||||||
"configFile": false,
|
"configFile": false,
|
||||||
},
|
},
|
||||||
"cache": false,
|
"cache": false,
|
||||||
|
"corejs": "auto",
|
||||||
"cssSourceMap": false,
|
"cssSourceMap": false,
|
||||||
"devMiddleware": Object {},
|
"devMiddleware": Object {},
|
||||||
"extractCSS": false,
|
"extractCSS": false,
|
||||||
|
@ -16,6 +16,7 @@ Object {
|
|||||||
"configFile": false,
|
"configFile": false,
|
||||||
},
|
},
|
||||||
"cache": false,
|
"cache": false,
|
||||||
|
"corejs": "auto",
|
||||||
"cssSourceMap": undefined,
|
"cssSourceMap": undefined,
|
||||||
"devMiddleware": Object {},
|
"devMiddleware": Object {},
|
||||||
"extractCSS": false,
|
"extractCSS": false,
|
||||||
@ -395,6 +396,7 @@ Object {
|
|||||||
"configFile": false,
|
"configFile": false,
|
||||||
},
|
},
|
||||||
"cache": false,
|
"cache": false,
|
||||||
|
"corejs": "auto",
|
||||||
"cssSourceMap": undefined,
|
"cssSourceMap": undefined,
|
||||||
"devMiddleware": Object {},
|
"devMiddleware": Object {},
|
||||||
"extractCSS": false,
|
"extractCSS": false,
|
||||||
|
@ -13,6 +13,7 @@ import semver from 'semver'
|
|||||||
|
|
||||||
import { TARGETS, isUrl, urlJoin, getPKG } from '@nuxt/utils'
|
import { TARGETS, isUrl, urlJoin, getPKG } from '@nuxt/utils'
|
||||||
|
|
||||||
|
import createRequire from 'create-require'
|
||||||
import PerfLoader from '../utils/perf-loader'
|
import PerfLoader from '../utils/perf-loader'
|
||||||
import StyleLoader from '../utils/style-loader'
|
import StyleLoader from '../utils/style-loader'
|
||||||
import WarningIgnorePlugin from '../plugins/warning-ignore'
|
import WarningIgnorePlugin from '../plugins/warning-ignore'
|
||||||
@ -96,6 +97,7 @@ export default class WebpackBaseConfig {
|
|||||||
|
|
||||||
getBabelOptions () {
|
getBabelOptions () {
|
||||||
const envName = this.name
|
const envName = this.name
|
||||||
|
const { buildOptions: { corejs }, options: { rootDir } } = this.buildContext
|
||||||
const options = {
|
const options = {
|
||||||
...this.buildContext.buildOptions.babel,
|
...this.buildContext.buildOptions.babel,
|
||||||
envName
|
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') {
|
if (typeof options.presets === 'function') {
|
||||||
options.presets = options.presets(
|
options.presets = options.presets(
|
||||||
|
Loading…
Reference in New Issue
Block a user