-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwebpack.config.js
More file actions
163 lines (143 loc) · 4.62 KB
/
webpack.config.js
File metadata and controls
163 lines (143 loc) · 4.62 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* All the settings to build variants using webpack
*/
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')
const glob = require('glob')
const pkg = require('./package')
const fs = require('fs-extra')
// ------------------------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------------------------
const LIB_PATH = path.join(__dirname, '.')
const DIST_PATH = path.join(__dirname, 'dist/')
const PKG_NAME = pkg.name.split('/').pop()
const LIBRARY_NAME = 'nimble'
const VERSION_PLUGIN = new webpack.DefinePlugin({ VERSION: JSON.stringify(pkg.version) })
const VARIANT_BROWSER_PLUGIN = new webpack.DefinePlugin({ VARIANT: JSON.stringify('browser') })
const VARIANT_NODE_PLUGIN = new webpack.DefinePlugin({ VARIANT: JSON.stringify('node') })
fs.removeSync(DIST_PATH)
fs.mkdirSync(DIST_PATH)
// ------------------------------------------------------------------------------------------------
// Terser options
// ------------------------------------------------------------------------------------------------
// Run library terser settings
const terserPluginConfig = {
terserOptions: {
ecma: 2015,
mangle: {
reserved: ['P2PKHLockScript'],
properties: false
}
},
// Leave license comments intact
extractComments: false
}
// ------------------------------------------------------------------------------------------------
// Browser Minified
// ------------------------------------------------------------------------------------------------
const browserMin = {
entry: LIB_PATH,
output: {
filename: `${PKG_NAME}.browser.min.js`,
path: DIST_PATH,
library: LIBRARY_NAME,
libraryTarget: 'umd'
},
resolve: {
mainFields: ['browser', 'main', 'module'],
extensions: ['.js', '.mjs', '.wasm', '.json']
},
optimization: {
minimizer: [
new TerserPlugin(terserPluginConfig)
]
},
plugins: [
VERSION_PLUGIN,
VARIANT_BROWSER_PLUGIN
],
stats: 'errors-only'
}
// ------------------------------------------------------------------------------------------------
// Node Minified
// ------------------------------------------------------------------------------------------------
const nodeMin = {
...browserMin,
target: 'node',
output: {
filename: `${PKG_NAME}.node.min.js`,
path: DIST_PATH,
libraryTarget: 'commonjs2'
},
resolve: {
mainFields: ['main', 'module'],
extensions: ['.js', '.mjs', '.wasm', '.json']
},
plugins: [
VERSION_PLUGIN,
VARIANT_NODE_PLUGIN
]
}
// ------------------------------------------------------------------------------------------------
// Browser Original
// ------------------------------------------------------------------------------------------------
const browser = {
...browserMin,
output: {
filename: `${PKG_NAME}.browser.js`,
path: DIST_PATH,
library: LIBRARY_NAME
},
plugins: [
VERSION_PLUGIN,
VARIANT_BROWSER_PLUGIN
],
optimization: { minimize: false }
}
// ------------------------------------------------------------------------------------------------
// Node Original
// ------------------------------------------------------------------------------------------------
const node = {
...nodeMin,
output: {
filename: `${PKG_NAME}.node.js`,
path: DIST_PATH,
libraryTarget: 'commonjs2'
},
plugins: [
VERSION_PLUGIN,
VARIANT_NODE_PLUGIN
],
optimization: { minimize: false }
}
// ------------------------------------------------------------------------------------------------
// Browser Tests
// ------------------------------------------------------------------------------------------------
const patterns = process.env.SPECS ? JSON.parse(process.env.SPECS) : ['test']
const paths = new Set()
patterns.forEach(x => glob.sync(x).forEach(y => paths.add(y)))
const entries = Array.from(paths).map(x => path.join(process.cwd(), x))
if (!entries.length) throw new Error(`no test files found: ${patterns}`)
const browserTests = {
target: 'web',
entry: entries,
output: {
filename: `${PKG_NAME}.browser.tests.js`,
path: DIST_PATH
},
node: { fs: 'empty' },
externals: {
mocha: 'mocha.Mocha',
chai: 'chai',
jsdom: 'jsdom',
bsv: 'bsv',
target: LIBRARY_NAME
},
optimization: { minimize: false },
plugins: [new webpack.EnvironmentPlugin(process.env), VARIANT_BROWSER_PLUGIN],
stats: 'errors-only'
}
// ------------------------------------------------------------------------------------------------
module.exports = [browserMin, nodeMin, browser, node, browserTests]