Nuxt/packages/babel-preset-app/src/index.js

118 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-11-29 16:54:38 +00:00
const path = require('path')
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,
buildTarget,
loose = false,
2018-11-29 16:54:38 +00:00
debug = false,
useBuiltIns = 'usage',
modules = false,
2018-11-29 16:54:38 +00:00
spec,
ignoreBrowserslistConfig = modern,
configPath,
2018-11-29 16:54:38 +00:00
include,
exclude,
shippedProposals,
forceAllTransforms,
2018-11-29 16:54:38 +00:00
decoratorsBeforeExport,
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,
loose,
2018-11-29 16:54:38 +00:00
debug,
modules,
targets,
useBuiltIns,
ignoreBrowserslistConfig,
2018-11-29 16:54:38 +00:00
configPath,
include,
exclude: polyfills.concat(exclude || []),
shippedProposals,
forceAllTransforms
}
])
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
}],
[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'))
}])
return {
presets,
plugins
}
}