-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
babel-loader.js
73 lines (67 loc) · 1.86 KB
/
babel-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path')
const babelLoader = require('babel-loader')
const { log } = require('saber-log')
const logUpdate = require('log-update')
// increment '0' to invalidate cache
const CACHE_KEY = `babel-cache-0`
module.exports = babelLoader.custom(babel => {
const configs = new Set()
return {
customOptions(opts) {
const custom = opts.customLoaderOptions
const filename = path.join(custom.cwd, 'noop.js')
const loader = Object.assign(
custom.shouldCache
? {
cacheCompression: false,
cacheDirectory: path.join(
custom.distDir,
'cache',
'saber-babel-loader'
),
cacheIdentifier: JSON.stringify({
key: CACHE_KEY,
type: custom.type,
config: babel.loadPartialConfig({
filename,
cwd: custom.cwd,
sourceFileName: filename
}).options
})
}
: {},
opts
)
delete loader.customLoaderOptions
return { loader, custom }
},
config(cfg, { customOptions }) {
const options = Object.assign({}, cfg.options)
if (cfg.hasFilesystemConfig()) {
for (const file of [cfg.babelrc, cfg.config]) {
if (file && !configs.has(file)) {
configs.add(file)
if (log.logLevel > 3) {
logUpdate.clear()
log.verbose(`Applying Babel config file ${file}`)
}
}
}
}
options.presets.unshift(
babel.createConfigItem(
[
require('../babel/preset'),
{
isServer: customOptions.type === 'server'
}
],
{
type: 'preset'
}
)
)
return options
}
}
})