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

70 lines
2.0 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',
},
'@babel/preset-env': {
debug: !__TEST__,
// disabled until https://github.com/babel/babel/issues/8323 is resolved
// loose: true,
shippedProposals: true,
2018-05-23 11:23:25 +02:00
},
}
const getConfig = (key, ...args) => {
const config = configs[key]
2020-11-24 10:50:40 +01:00
return config === undefined ? {} : typeof config === 'function' ? 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
2020-11-24 10:50:40 +01:00
const pluginsOrder = ['@babel/plugin-proposal-decorators', '@babel/plugin-proposal-class-properties']
2019-03-28 17:25:46 +01:00
2021-04-20 22:29:06 +02:00
module.exports = function (pkg, configs = {}) {
const plugins = {}
const presets = {}
2018-02-26 22:30:37 +01:00
Object.keys(pkg.devDependencies || {}).forEach(name => {
if (!(name in presets) && PLUGINS_RE.test(name)) {
2021-04-20 22:29:06 +02:00
plugins[name] = { ...getConfig(name, pkg), ...configs[name] }
2018-02-26 22:30:37 +01:00
} else if (!(name in presets) && PRESETS_RE.test(name)) {
2021-04-20 22:29:06 +02:00
presets[name] = { ...getConfig(name, pkg), ...configs[name] }
2018-02-26 22:30:37 +01:00
}
})
return {
comments: !__PROD__,
ignore: __PROD__ ? [/\btests?\//, /\.spec\.js$/] : undefined,
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]]),
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-02-26 22:30:37 +01:00
}
}