2018-11-29 16:54:38 +00:00
|
|
|
const path = require('path')
|
|
|
|
|
2018-10-26 17:58:21 +00:00
|
|
|
const defaultPolyfills = [
|
|
|
|
// 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',
|
|
|
|
// #2012 es6.promise replaces native Promise in FF and causes missing finally
|
|
|
|
'es7.promise.finally'
|
|
|
|
]
|
|
|
|
|
|
|
|
function getPolyfills(targets, includes, { ignoreBrowserslistConfig, configPath }) {
|
|
|
|
const { isPluginRequired } = require('@babel/preset-env')
|
|
|
|
const builtInsList = require('@babel/preset-env/data/built-ins.json')
|
|
|
|
const getTargets = require('@babel/preset-env/lib/targets-parser').default
|
|
|
|
const builtInTargets = getTargets(targets, {
|
|
|
|
ignoreBrowserslistConfig,
|
|
|
|
configPath
|
|
|
|
})
|
|
|
|
|
|
|
|
return includes.filter(item => isPluginRequired(builtInTargets, builtInsList[item]))
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (context, options = {}) => {
|
|
|
|
const presets = []
|
|
|
|
const plugins = []
|
|
|
|
|
|
|
|
// JSX
|
|
|
|
if (options.jsx !== false) {
|
|
|
|
plugins.push(
|
|
|
|
require('@babel/plugin-syntax-jsx'),
|
|
|
|
require('babel-plugin-transform-vue-jsx')
|
|
|
|
// require('babel-plugin-jsx-event-modifiers'),
|
|
|
|
// require('babel-plugin-jsx-v-model')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const modern = !!options.modern
|
|
|
|
|
|
|
|
const {
|
2018-11-29 16:54:38 +00:00
|
|
|
polyfills: userPolyfills,
|
2018-10-26 17:58:21 +00:00
|
|
|
buildTarget,
|
|
|
|
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,
|
2018-10-26 17:58:21 +00:00
|
|
|
ignoreBrowserslistConfig = modern,
|
|
|
|
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,
|
2018-10-26 17:58:21 +00:00
|
|
|
decoratorsLegacy
|
|
|
|
} = options
|
|
|
|
|
|
|
|
let targets = options.targets
|
|
|
|
if (modern === true) {
|
|
|
|
targets = { esmodules: true }
|
|
|
|
} else if (targets === undefined) {
|
|
|
|
targets = buildTarget === 'server' ? { node: 'current' } : { ie: 9 }
|
|
|
|
}
|
|
|
|
|
|
|
|
let polyfills
|
|
|
|
if (modern === false && useBuiltIns === 'usage' && buildTarget === 'client') {
|
|
|
|
polyfills = getPolyfills(targets, userPolyfills || defaultPolyfills, {
|
|
|
|
ignoreBrowserslistConfig,
|
|
|
|
configPath
|
|
|
|
})
|
|
|
|
plugins.push([require('./polyfills-plugin'), { polyfills }])
|
|
|
|
} else {
|
|
|
|
polyfills = []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass options along to babel-preset-env
|
|
|
|
presets.push([
|
|
|
|
require('@babel/preset-env'), {
|
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,
|
|
|
|
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
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
plugins.push(
|
|
|
|
require('@babel/plugin-syntax-dynamic-import'),
|
2018-11-29 16:54:38 +00:00
|
|
|
[require('@babel/plugin-proposal-decorators'), {
|
|
|
|
decoratorsBeforeExport,
|
|
|
|
legacy: decoratorsLegacy !== false
|
|
|
|
}],
|
2018-10-26 17:58:21 +00:00
|
|
|
[require('@babel/plugin-proposal-class-properties'), { loose }]
|
|
|
|
)
|
|
|
|
|
|
|
|
// Transform runtime, but only for helpers
|
|
|
|
plugins.push([require('@babel/plugin-transform-runtime'), {
|
2018-11-29 16:54:38 +00:00
|
|
|
regenerator: useBuiltIns !== 'usage',
|
|
|
|
corejs: useBuiltIns !== false ? false : 2,
|
|
|
|
helpers: useBuiltIns === 'usage',
|
|
|
|
useESModules: true,
|
|
|
|
absoluteRuntime: path.dirname(require.resolve('@babel/runtime/package.json'))
|
2018-10-26 17:58:21 +00:00
|
|
|
}])
|
|
|
|
|
|
|
|
return {
|
|
|
|
presets,
|
|
|
|
plugins
|
|
|
|
}
|
|
|
|
}
|