Repository navigation
Expand file tree
/
Copy pathwebpack.prod.cjs
More file actions
91 lines (83 loc) · 2.3 KB
/
Copy pathwebpack.prod.cjs
File metadata and controls
91 lines (83 loc) · 2.3 KB
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
// webpack.prod.cjs
const { BannerPlugin } = require('webpack')
const { merge } = require('webpack-merge')
const { resolve: pathResolve } = require('path')
const { multibanner } = require('bannerjs')
const { remove, pick, get, unset } = require('lodash')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const common = require('./webpack.common.cjs')
const pkg = require('./package.json')
const BUILD_TARGET = get(process.env, 'BUILD_TARGET', 'esm')
const { filename: OUTPUT_FILE } = get(pkg, 'jslib')
const banner = multibanner(
pick(pkg, ['author', 'name', 'license', 'version', 'description', 'homepage'])
)
const prodConfig = {
mode: 'production',
devtool: 'source-map',
plugins: [
new BannerPlugin({
raw: true,
banner,
}),
],
}
function generateOutput(target) {
let libraryTarget
switch (target) {
case 'esm':
libraryTarget = 'commonjs-module'
// libraryTarget = 'commonjs2'
break
case 'cjs':
libraryTarget = 'commonjs2'
break
case 'umd':
libraryTarget = 'umd'
break
default:
libraryTarget = 'umd'
break;
}
return {
libraryTarget: libraryTarget,
path: pathResolve(__dirname, `./dist/${target}`),
}
}
function generateConfig (name) {
const config = merge(common, prodConfig)
const { output, optimization, plugins } = config
const { path, libraryTarget } = generateOutput(BUILD_TARGET)
const uglify = name.indexOf('min') > -1
output.path = uglify ? path.replace('.min', '') : path
output.libraryTarget = libraryTarget
optimization.minimize = uglify
if (uglify) {
optimization.minimizer = [
new TerserPlugin({
extractComments: false // 👈 LICENSE.txt 파일 생성 방지
})
]
// if (config.entry && config.entry.sample) {
// delete config.entry.sample
// }
output.filename = `${OUTPUT_FILE}.min.js`
remove(plugins, p => p instanceof HtmlWebpackPlugin)
}
return config
}
let buildJobs
switch (BUILD_TARGET) {
// case 'esm':
// case 'cjs':
// buildJobs = [BUILD_TARGET]
// break
// case 'umd':
// buildJobs = [BUILD_TARGET, `${BUILD_TARGET}.min`]
// break
default:
buildJobs = [BUILD_TARGET]
break
}
module.exports = buildJobs.map((name) => generateConfig(name))