Files
xen-orchestra/@xen-orchestra/babel-config/index.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-02-26 22:30:37 +01:00
'use strict'
2018-05-23 11:23:25 +02:00
const PLUGINS_RE = /^(?:@babel\/|babel-)plugin-.+$/
2018-02-26 22:30:37 +01:00
const PRESETS_RE = /^@babel\/preset-.+$/
const NODE_ENV = process.env.NODE_ENV || 'development'
const __PROD__ = NODE_ENV === 'production'
const __TEST__ = NODE_ENV === 'test'
2018-05-23 11:23:25 +02:00
const configs = {
'@babel/plugin-proposal-decorators': {
legacy: true,
},
2018-07-17 11:03:57 +02:00
'@babel/plugin-proposal-pipeline-operator': {
proposal: 'minimal',
},
2018-12-11 10:37:46 +01:00
'@babel/preset-env'(pkg) {
2018-05-23 11:23:25 +02:00
return {
debug: !__TEST__,
// disabled until https://github.com/babel/babel/issues/8323 is resolved
// loose: true,
2018-05-23 11:23:25 +02:00
shippedProposals: true,
targets: (() => {
let node = (pkg.engines || {}).node
if (node !== undefined) {
const trimChars = '^=>~'
while (trimChars.includes(node[0])) {
node = node.slice(1)
}
}
return { browsers: pkg.browserslist, node }
})(),
2018-05-23 11:23:25 +02:00
useBuiltIns: '@babel/polyfill' in (pkg.dependencies || {}) && 'usage',
}
},
}
const getConfig = (key, ...args) => {
const config = configs[key]
return config === undefined
? {}
: typeof config === 'function'
2018-11-07 18:37:23 +01:00
? config(...args)
: config
2018-05-23 11:23:25 +02:00
}
2019-03-28 17:25:46 +01:00
// some plugins must be used in a specific order
const pluginsOrder = [
'@babel/plugin-proposal-decorators',
'@babel/plugin-proposal-class-properties',
]
2018-12-11 10:37:46 +01:00
module.exports = function(pkg, plugins, presets) {
2018-02-26 22:30:37 +01:00
plugins === undefined && (plugins = {})
presets === undefined && (presets = {})
Object.keys(pkg.devDependencies || {}).forEach(name => {
if (!(name in presets) && PLUGINS_RE.test(name)) {
2018-05-23 11:23:25 +02:00
plugins[name] = getConfig(name, pkg)
2018-02-26 22:30:37 +01:00
} else if (!(name in presets) && PRESETS_RE.test(name)) {
2018-05-23 11:23:25 +02:00
presets[name] = getConfig(name, pkg)
2018-02-26 22:30:37 +01:00
}
})
return {
comments: !__PROD__,
ignore: __TEST__ ? undefined : [/\.spec\.js$/],
2019-03-28 17:25:46 +01:00
plugins: Object.keys(plugins)
.map(plugin => [plugin, plugins[plugin]])
.sort(([a], [b]) => {
const oA = pluginsOrder.indexOf(a)
const oB = pluginsOrder.indexOf(b)
return oA !== -1 && oB !== -1 ? oA - oB : a < b ? -1 : 1
}),
2018-02-26 22:30:37 +01:00
presets: Object.keys(presets).map(preset => [preset, presets[preset]]),
}
}