-
Notifications
You must be signed in to change notification settings - Fork 244
/
webpack.config.cjs
120 lines (108 loc) · 2.65 KB
/
webpack.config.cjs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Run with `npm run webpack`
*/
const path = require('path')
const webpack = require('webpack')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const rimraf = require('rimraf').sync
const doAnalyze = process.env.npm_lifecycle_event === 'webpack:analyze'
const variants = doAnalyze
? {
target: [
['commonjs2', 'index']
]
}
: {
minified: [true],
target: [
['commonjs2', 'index'],
['umd', 'umd']
]
}
rimraf('./dist')
function createConfig (options) {
const plugins = [
new webpack.IgnorePlugin({
checkResource (resource, context) {
// ---- do not bundle astronomia vsop planet data
if (/\/astronomia\/data$/.test(context)) {
return !['./deltat.js', './vsop87Bearth.js'].includes(resource)
}
// ---- do not bundle moment locales
if (/\/moment\/locale$/.test(context)) {
return true
}
return false
}
})
// ---- using a custom set of timezones
// new webpack.NormalModuleReplacementPlugin(
// /moment-timezone\/data\/packed\/latest\.json/,
// require.resolve('./timezones.json')
// )
]
if (doAnalyze) {
plugins.push(new BundleAnalyzerPlugin())
}
return {
mode: 'production',
devtool: 'source-map',
entry: {
'date.holidays': [
// '@babel/polyfill',
'./src/index.js'
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: options.target[1] +
(options.minified ? '.min' : '') +
'.js',
library: 'Holidays',
libraryTarget: options.target[0]
},
resolve: {
// mainFields: ['browser', 'main', 'module']
},
optimization: {
minimize: options.minified
},
module: {
rules: [{
test: /\.(js|jsx)$/,
// exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}]
},
plugins
}
}
const createVariants = (variants, createConfig) => {
const targets = variants.target.reduce((a, target) => {
a.push({ target })
return a
}, [])
let configs = Object.entries(variants).reduce((a, [key, val]) => {
if (key !== 'target') {
val.forEach(v => {
targets.forEach(({ target }) => {
a.push({ target, [key]: v })
})
})
}
return a
}, [])
if (!configs.length) {
configs = targets
}
return configs.map(opts => createConfig(opts))
}
module.exports = createVariants(variants, createConfig)
if (module === require.main) {
console.log(module.exports)
}