-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
91 lines (80 loc) · 1.89 KB
/
bin.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
'use strict'
process.env.NODE_ENV = 'production'
// Node.js modules
const path = require('path')
// build bundle with Webpack
const webpack = require('webpack')
const webpackConfig = require(path.join(process.cwd(), 'webpack.config'))
// handle Webpack output object and plugins
const webpackOutput = { ...webpackConfig.output }
const fatalError = err => {
if (err) {
console.error(err)
}
// exit with failure
process.exit(1)
}
// setup config for multiple outputs
const webpackConfigList = []
// web target builds
;[
'.bundle',
'.polyfill',
''
].forEach(outputType => {
const config = {
...webpackConfig,
output: {
...webpackOutput,
// custom filename by output type
filename: webpackOutput.filename.replace('.js', `${outputType}.min.js`)
}
}
// edit Webpack config by output type
if (outputType !== '.bundle') {
// lib and polyfills without external dependencies
config.externals = [{
'@ecomplus/utils': {
commonjs: '@ecomplus/utils',
commonjs2: '@ecomplus/utils',
root: 'ecomUtils'
},
axios: 'axios'
}]
if (outputType === '') {
// standalone lib output
// also ignore polyfills
config.externals.push(/^core-js/i)
}
}
webpackConfigList.push(config)
})
// add Node.js output
webpackConfigList.push({
...webpackConfig,
target: 'node',
optimization: {
minimize: false
},
output: {
...webpackOutput,
filename: webpackOutput.filename.replace('.js', '.node.js')
},
externals: /^(@ecomplus\/utils|axios)/i
})
webpack(webpackConfigList, (err, stats) => {
// console.log(stats)
if (err) {
fatalError(err)
}
// check and handle webpack errors and warnings
const info = stats.toJson()
if (stats.hasErrors()) {
const err = info.errors
fatalError(err)
}
if (stats.hasWarnings()) {
console.warn(info.warnings)
}
})