forked from ethersphere/bee-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
157 lines (147 loc) · 4.51 KB
/
webpack.config.ts
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
/* eslint-disable no-console */
import Path from 'path'
import { DefinePlugin, Configuration, WebpackPluginInstance, NormalModuleReplacementPlugin } from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import TerserPlugin from 'terser-webpack-plugin'
import PackageJson from './package.json'
import { getBrowserPathMapping } from './jest.config'
interface WebpackEnvParams {
target: 'web' | 'node'
globalWindow: boolean
debug: boolean
mode: 'production' | 'development'
fileName: string
}
const base = async (env?: Partial<WebpackEnvParams>): Promise<Configuration> => {
const isProduction = env?.mode === 'production'
const isWindow = env?.globalWindow
const isBrowser = env?.target === 'web'
const filename = env?.fileName || [
'index',
isBrowser ? '.browser' : null,
isWindow ? '.global' : null,
isProduction ? '.min' : null,
'.js',
]
.filter(Boolean)
.join('')
const entry = isWindow ? Path.resolve(__dirname, 'test', 'testpage', 'testpage') : Path.resolve(__dirname, 'src')
const path = isWindow ? Path.resolve(__dirname, 'test', 'testpage') : Path.resolve(__dirname, 'dist')
const target = env?.target || 'web' // 'node' or 'web'
const plugins: WebpackPluginInstance[] = [
new DefinePlugin({
'process.env.ENV': env?.mode || 'development',
'process.env.IS_WEBPACK_BUILD': 'true',
}),
]
if (target === 'web') {
const browserPathMapping = await getBrowserPathMapping();
// eslint-disable-next-line guard-for-in
for (const nodeReference in browserPathMapping) {
plugins.push(
new NormalModuleReplacementPlugin(new RegExp(`\\${nodeReference}`), browserPathMapping[nodeReference]),
)
}
// change node modules to browser modules according to packageJson.browser mapping
// eslint-disable-next-line guard-for-in
const browserModuleMapping = PackageJson.browser as { [key: string]: string}
// eslint-disable-next-line guard-for-in
for (const nodeReference in browserModuleMapping) {
const browserReference: string = browserModuleMapping[nodeReference]
plugins.push(
new NormalModuleReplacementPlugin(new RegExp(`^${nodeReference}$`), browserReference),
)
}
}
if(isWindow) {
plugins.push(new DefinePlugin({
__BEE_URL__: JSON.stringify(process.env.BEE_URL || 'http://localhost:1633'),
}))
}
return {
bail: Boolean(isProduction),
mode: env?.mode || 'development',
devtool: isProduction ? 'source-map' : 'cheap-module-source-map',
entry,
output: {
path,
filename,
sourceMapFilename: filename + '.map',
library: PackageJson.name,
libraryTarget: 'umd',
globalObject: 'this',
},
module: {
rules: [
{
test: /\.(ts|js)$/,
// include: entry,
use: {
loader: 'babel-loader',
},
},
],
},
resolve: {
extensions: ['.ts', '.js'],
fallback: {
path: false,
fs: false,
},
},
optimization: {
minimize: isProduction,
minimizer: [
// This is only used in production mode
new TerserPlugin({
terserOptions: {
parse: {
// we want terser to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 2018,
},
compress: {
ecma: 5,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
},
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
}),
],
},
plugins,
target,
node: {
global: true,
__filename: 'mock',
__dirname: 'mock',
},
performance: {
hints: false,
},
}
}
export default async (env?: Partial<WebpackEnvParams>): Promise<Configuration> => {
// eslint-disable-next-line no-console
console.log('env', env)
if (env?.debug) {
const config = {
... await base(env),
plugins: [new BundleAnalyzerPlugin()],
profile: true,
}
return config
}
return base(env)
}