UNPKG

1.3 kBJavaScriptView Raw
1const babelLoader = require('babel-loader')
2const logger = require('@poi/logger')
3
4const macroCheck = new RegExp('[./]macro')
5
6module.exports = babelLoader.custom(babel => {
7 const configs = new Set()
8 const presetItem = babel.createConfigItem(require('../babel/preset'), {
9 type: 'preset'
10 })
11
12 return {
13 customOptions(opts) {
14 const custom = opts.customLoaderOptions
15 delete opts.customLoaderOptions
16
17 return { loader: opts, custom }
18 },
19 config(cfg, { source }) {
20 const options = Object.assign({}, cfg.options, {
21 caller: Object.assign({}, cfg.options.caller, {
22 // for babel-plugin-macros it should never be cached
23 poiInvalidationToken: macroCheck.test(source)
24 ? require('crypto')
25 .randomBytes(32)
26 .toString('hex')
27 : ''
28 })
29 })
30
31 if (cfg.hasFilesystemConfig()) {
32 for (const file of [cfg.babelrc, cfg.config]) {
33 if (file && !configs.has(file)) {
34 configs.add(file)
35 logger.debug(`Using external babel config file: ${file}`)
36 }
37 }
38 } else {
39 // Add our default preset if the no "babelrc" found.
40 options.presets = [...options.presets, presetItem]
41 }
42
43 return options
44 }
45 }
46})