2019-03-30 19:23:17 +00:00
|
|
|
const coreJsMeta = {
|
|
|
|
2: {
|
|
|
|
prefixes: {
|
|
|
|
es6: 'es6',
|
|
|
|
es7: 'es7'
|
|
|
|
},
|
2020-03-12 18:13:04 +00:00
|
|
|
builtIns: '@babel/compat-data/corejs2-built-ins'
|
2019-03-30 19:23:17 +00:00
|
|
|
},
|
|
|
|
3: {
|
|
|
|
prefixes: {
|
|
|
|
es6: 'es',
|
|
|
|
es7: 'es'
|
|
|
|
},
|
|
|
|
builtIns: 'core-js-compat/data'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 10:21:39 +00:00
|
|
|
function getMajorVersion (version) {
|
|
|
|
if (typeof version === 'number') {
|
|
|
|
return Math.floor(version)
|
|
|
|
} else {
|
|
|
|
return Number(version.split('.')[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
function getDefaultPolyfills (corejs) {
|
2020-09-21 10:21:39 +00:00
|
|
|
const { prefixes: { es6, es7 } } = coreJsMeta[getMajorVersion(corejs.version)]
|
2019-03-30 19:23:17 +00:00
|
|
|
return [
|
|
|
|
// Promise polyfill alone doesn't work in IE,
|
|
|
|
// Needs this as well. see: #1642
|
|
|
|
`${es6}.array.iterator`,
|
|
|
|
// This is required for webpack code splitting, vuex etc.
|
|
|
|
`${es6}.promise`,
|
|
|
|
// this is needed for object rest spread support in templates
|
|
|
|
// as vue-template-es2015-compiler 1.8+ compiles it to Object.assign() calls.
|
|
|
|
`${es6}.object.assign`,
|
|
|
|
// #2012 es7.promise replaces native Promise in FF and causes missing finally
|
|
|
|
`${es7}.promise.finally`
|
|
|
|
]
|
|
|
|
}
|
2018-10-26 17:58:21 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath, corejs }) {
|
2020-03-12 18:13:04 +00:00
|
|
|
const { default: getTargets, isRequired } = require('@babel/helper-compilation-targets')
|
2020-09-21 10:21:39 +00:00
|
|
|
const builtInsList = require(coreJsMeta[getMajorVersion(corejs.version)].builtIns)
|
2018-10-26 17:58:21 +00:00
|
|
|
const builtInTargets = getTargets(targets, {
|
|
|
|
ignoreBrowserslistConfig,
|
|
|
|
configPath
|
|
|
|
})
|
|
|
|
|
2020-03-12 18:13:04 +00:00
|
|
|
return includes.filter(item => isRequired(
|
|
|
|
'nuxt-polyfills',
|
|
|
|
builtInTargets,
|
|
|
|
{
|
|
|
|
compatData: { 'nuxt-polyfills': builtInsList[item] }
|
|
|
|
}
|
|
|
|
))
|
2018-10-26 17:58:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:36:54 +00:00
|
|
|
module.exports = (api, options = {}) => {
|
2018-10-26 17:58:21 +00:00
|
|
|
const presets = []
|
|
|
|
const plugins = []
|
|
|
|
|
2019-09-03 19:36:54 +00:00
|
|
|
const envName = api.env()
|
2018-10-26 17:58:21 +00:00
|
|
|
|
|
|
|
const {
|
2020-03-30 09:48:59 +00:00
|
|
|
bugfixes,
|
2018-11-29 16:54:38 +00:00
|
|
|
polyfills: userPolyfills,
|
2018-10-26 17:58:21 +00:00
|
|
|
loose = false,
|
2018-11-29 16:54:38 +00:00
|
|
|
debug = false,
|
|
|
|
useBuiltIns = 'usage',
|
2018-10-26 17:58:21 +00:00
|
|
|
modules = false,
|
2018-11-29 16:54:38 +00:00
|
|
|
spec,
|
2019-09-03 19:36:54 +00:00
|
|
|
ignoreBrowserslistConfig = envName === 'modern',
|
2018-10-26 17:58:21 +00:00
|
|
|
configPath,
|
2018-11-29 16:54:38 +00:00
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
shippedProposals,
|
2018-10-26 17:58:21 +00:00
|
|
|
forceAllTransforms,
|
2018-11-29 16:54:38 +00:00
|
|
|
decoratorsBeforeExport,
|
2019-03-20 12:42:34 +00:00
|
|
|
decoratorsLegacy,
|
|
|
|
absoluteRuntime
|
2018-10-26 17:58:21 +00:00
|
|
|
} = options
|
|
|
|
|
2019-03-30 19:23:17 +00:00
|
|
|
let { corejs = { version: 2 } } = options
|
|
|
|
|
|
|
|
if (typeof corejs !== 'object') {
|
|
|
|
corejs = { version: Number(corejs) }
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:36:54 +00:00
|
|
|
const defaultTargets = {
|
|
|
|
server: { node: 'current' },
|
|
|
|
client: { ie: 9 },
|
|
|
|
modern: { esmodules: true }
|
2018-10-26 17:58:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:36:54 +00:00
|
|
|
let { targets = defaultTargets[envName] } = options
|
|
|
|
|
|
|
|
// modern mode can only be { esmodules: true }
|
|
|
|
if (envName === 'modern') {
|
|
|
|
targets = defaultTargets.modern
|
|
|
|
}
|
|
|
|
|
|
|
|
const polyfills = []
|
|
|
|
|
|
|
|
if (envName === 'client' && useBuiltIns === 'usage') {
|
|
|
|
polyfills.push(
|
|
|
|
...getPolyfills(
|
|
|
|
targets,
|
|
|
|
userPolyfills || getDefaultPolyfills(corejs),
|
|
|
|
{
|
|
|
|
ignoreBrowserslistConfig,
|
|
|
|
configPath,
|
|
|
|
corejs
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2018-10-26 17:58:21 +00:00
|
|
|
plugins.push([require('./polyfills-plugin'), { polyfills }])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass options along to babel-preset-env
|
|
|
|
presets.push([
|
|
|
|
require('@babel/preset-env'), {
|
2020-03-30 09:48:59 +00:00
|
|
|
bugfixes,
|
2018-11-29 16:54:38 +00:00
|
|
|
spec,
|
2018-10-26 17:58:21 +00:00
|
|
|
loose,
|
2018-11-29 16:54:38 +00:00
|
|
|
debug,
|
2018-10-26 17:58:21 +00:00
|
|
|
modules,
|
|
|
|
targets,
|
|
|
|
useBuiltIns,
|
2019-03-20 12:10:10 +00:00
|
|
|
corejs,
|
2018-10-26 17:58:21 +00:00
|
|
|
ignoreBrowserslistConfig,
|
2018-11-29 16:54:38 +00:00
|
|
|
configPath,
|
|
|
|
include,
|
|
|
|
exclude: polyfills.concat(exclude || []),
|
|
|
|
shippedProposals,
|
|
|
|
forceAllTransforms
|
2018-10-26 17:58:21 +00:00
|
|
|
}
|
|
|
|
])
|
|
|
|
|
2019-01-11 09:39:07 +00:00
|
|
|
// JSX
|
|
|
|
if (options.jsx !== false) {
|
|
|
|
presets.push([require('@vue/babel-preset-jsx'), Object.assign({}, options.jsx)])
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:58:21 +00:00
|
|
|
plugins.push(
|
2018-11-29 16:54:38 +00:00
|
|
|
[require('@babel/plugin-proposal-decorators'), {
|
|
|
|
decoratorsBeforeExport,
|
|
|
|
legacy: decoratorsLegacy !== false
|
|
|
|
}],
|
2019-09-20 07:53:58 +00:00
|
|
|
[require('@babel/plugin-proposal-class-properties'), { loose: true }]
|
2018-10-26 17:58:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Transform runtime, but only for helpers
|
|
|
|
plugins.push([require('@babel/plugin-transform-runtime'), {
|
2018-11-29 16:54:38 +00:00
|
|
|
regenerator: useBuiltIns !== 'usage',
|
2019-07-03 07:27:18 +00:00
|
|
|
corejs: false,
|
2018-11-29 16:54:38 +00:00
|
|
|
helpers: useBuiltIns === 'usage',
|
2019-09-03 19:36:54 +00:00
|
|
|
useESModules: envName !== 'server',
|
2019-03-20 12:42:34 +00:00
|
|
|
absoluteRuntime
|
2018-10-26 17:58:21 +00:00
|
|
|
}])
|
|
|
|
|
|
|
|
return {
|
2020-03-10 21:09:46 +00:00
|
|
|
sourceType: 'unambiguous',
|
2018-10-26 17:58:21 +00:00
|
|
|
presets,
|
|
|
|
plugins
|
|
|
|
}
|
|
|
|
}
|