This repository was archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwebpack.config.js
More file actions
154 lines (141 loc) · 5.71 KB
/
webpack.config.js
File metadata and controls
154 lines (141 loc) · 5.71 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
/* eslint-disable prefer-template */
/* eslint-disable prefer-destructuring */
process.env.NODE_ENV = process.env.NODE_ENV || 'development' // set a default NODE_ENV
const path = require('path')
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')
const LodashWebpackPlugin = require('lodash-webpack-plugin')
const { merge } = require('webpack-merge')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
const pkg = require('./package.json')
const gitRevisionPlugin = new GitRevisionPlugin()
const libraryName = pkg.name
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production' || process.env.NODE_ENV === 'production'
const analyze = !!process.env.BUNDLE_ANALYSIS
const commonConfig = {
mode: isProduction ? 'production' : 'development',
entry: path.join(__dirname, 'src', 'StreamrClient.ts'),
devtool: 'source-map',
output: {
umdNamedDefine: true,
},
optimization: {
minimize: false,
},
module: {
rules: [
{
test: /(\.jsx|\.js|\.ts)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
configFile: path.resolve(__dirname, '.babel.config.js'),
babelrc: false,
cacheDirectory: true,
}
}
},
{
test: /(\.jsx|\.js|\.ts)$/,
loader: 'eslint-loader',
exclude: /(node_modules|streamr-client-protocol|dist)/, // excluding streamr-client-protocol makes build work when 'npm link'ed
},
],
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./vendor'), path.resolve('./src')],
extensions: ['.json', '.js', '.ts'],
},
plugins: [
gitRevisionPlugin,
new webpack.EnvironmentPlugin({
NODE_ENV: process.env.NODE_ENV,
version: pkg.version,
GIT_VERSION: gitRevisionPlugin.version(),
GIT_COMMITHASH: gitRevisionPlugin.commithash(),
GIT_BRANCH: gitRevisionPlugin.branch(),
})
]
}
const clientConfig = merge({}, commonConfig, {
name: 'browser-lib',
target: 'web',
output: {
libraryTarget: 'umd2',
filename: libraryName + '.web.js',
library: 'StreamrClient',
// NOTE:
// exporting the class directly
// `export default class StreamrClient {}`
// becomes:
// `window.StreamrClient === StreamrClient`
// which is correct, but if we define the class and export separately,
// which is required if we do interface StreamrClient extends …:
// `class StreamrClient {}; export default StreamrClient;`
// becomes:
// `window.StreamrClient = { default: StreamrClient, … }`
// which is wrong for browser builds.
// see: https://github.com/webpack/webpack/issues/706#issuecomment-438007763
libraryExport: 'StreamrClient', // This fixes the above.
},
resolve: {
alias: {
stream: 'readable-stream',
util: 'util',
http: path.resolve(__dirname, './src/shim/http-https.js'),
https: path.resolve(__dirname, './src/shim/http-https.js'),
ws: path.resolve(__dirname, './src/shim/ws.js'),
crypto: path.resolve(__dirname, 'node_modules', 'crypto-browserify'),
buffer: path.resolve(__dirname, 'node_modules', 'buffer'),
'node-fetch': path.resolve(__dirname, './src/shim/node-fetch.js'),
'node-webcrypto-ossl': path.resolve(__dirname, 'src/shim/crypto.js'),
'streamr-client-protocol': path.resolve(__dirname, 'node_modules/streamr-client-protocol/dist/src'),
// swap out ServerPersistentStore for BrowserPersistentStore
[path.resolve(__dirname, 'src/stream/encryption/ServerPersistentStore')]: (
path.resolve(__dirname, 'src/stream/encryption/BrowserPersistentStore')
),
}
},
plugins: [
new LodashWebpackPlugin(),
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
...(analyze ? [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
generateStatsFile: true,
}),
] : [])
]
})
let clientMinifiedConfig
if (isProduction) {
clientMinifiedConfig = merge({}, clientConfig, {
name: 'browser-lib-min',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ecma: 2018,
output: {
comments: false,
},
},
}),
],
},
output: {
filename: libraryName + '.web.min.js',
},
})
}
return [clientConfig, clientMinifiedConfig].filter(Boolean)
}