forked from puemos/web-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
112 lines (93 loc) · 2.32 KB
/
webpack.config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { resolve } = require('path')
const webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const { getIfUtils, removeEmpty } = require('webpack-config-utils')
const packageJSON = require('./package.json')
const packageName = normalizePackageName(packageJSON.name)
const LIB_NAME = pascalCase(packageName)
const PATHS = {
entryPoint: resolve(__dirname, 'src/index.ts'),
umd: resolve(__dirname, 'dist')
}
const DEFAULT_ENV = 'dev'
const EXTERNALS = {}
const RULES = {
ts: {
test: /\.ts?$/,
include: /src/,
use: [
{
loader: 'ts-loader',
options: {
compilerOptions: {
declarationDir: 'types'
}
}
}
]
}
}
const config = (env = DEFAULT_ENV) => {
const { ifProd, ifNotProd } = getIfUtils(env)
const PLUGINS = removeEmpty([
// enable scope hoisting
new webpack.optimize.ModuleConcatenationPlugin(),
ifProd(
new UglifyJsPlugin({
sourceMap: true,
parallel: true,
uglifyOptions: {
warnings: false,
output: { comments: false }
}
})
),
new webpack.LoaderOptionsPlugin({
debug: false,
minimize: true
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: ifProd('"production"', '"development"') }
})
])
const UMDConfig = {
entry: {
[ifProd(`${packageName}.min`, packageName)]: [PATHS.entryPoint]
},
output: {
path: PATHS.umd,
filename: '[name].js',
libraryTarget: 'umd',
library: LIB_NAME,
umdNamedDefine: true
},
resolve: {
extensions: ['.ts', '.js']
},
externals: EXTERNALS,
devtool: 'source-map',
plugins: PLUGINS,
module: {
rules: [RULES.ts]
}
}
return [UMDConfig]
}
module.exports = config
// helpers
function camelCaseToDash(myStr) {
return myStr.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
function dashToCamelCase(myStr) {
return myStr.replace(/-([a-z])/g, g => g[1].toUpperCase())
}
function toUpperCase(myStr) {
return `${myStr.charAt(0).toUpperCase()}${myStr.substr(1)}`
}
function pascalCase(myStr) {
return toUpperCase(dashToCamelCase(myStr))
}
function normalizePackageName(rawPackageName) {
const scopeEnd = rawPackageName.indexOf('/') + 1
return rawPackageName.substring(scopeEnd)
}