-
Notifications
You must be signed in to change notification settings - Fork 109
/
webpack.config.js
159 lines (144 loc) · 5.06 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
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
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const FixDefaultImportPlugin = require('webpack-fix-default-import-plugin')
const nodeExternals = require('webpack-node-externals')
const production = process.env.NODE_ENV === 'production'
const useDevServer = !process.env.NODE_ENV
console.log('Building in ' + (production ? 'production' : 'dev') + ' mode (' + (useDevServer ? 'for' : 'no') + ' dev server)')
const commonPlugins = [
new FixDefaultImportPlugin()
]
const commonConfig = {
mode: production ? 'production' : 'development',
node: {
__dirname: false,
__filename: false,
},
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '',
filename: '[name].js',
},
resolve: {
modules: [
// directories where to look for modules
path.resolve(__dirname, 'src'),
'node_modules'
],
extensions: [ '.mjs', '.ts', '.tsx', '.js', '.jsx', '.json' ]
},
// Use sourcemaps only in dev build
// Must be 'source-map' or 'inline-source-map'
// (see: https://webpack.js.org/loaders/less-loader/#sourcemaps)
devtool: production ? false : 'source-map',
externals: (() => {
const modules = [ 'fs', 'net', 'os', 'readline', 'path' ]
let customExternals = {}
for (const module of modules) {
customExternals[module] = `(function() { var mod = require('${module}'); if (!mod.default) mod.default = mod; return mod; })()`
}
const externals = [
nodeExternals({
whitelist: [ /^font-awesome(\/|$)/ ]
}),
customExternals
]
return externals
})(),
plugins: commonPlugins
}
const typescriptRule = {
test: new RegExp('(^' + escapeRegExp(path.resolve(__dirname, 'src')) + '\/.*\.jsx?|\.tsx?)$'),
loader: 'awesome-typescript-loader',
options: {
useCache: true,
reportFiles: [
'src/**/*.{ts,tsx}'
]
}
}
module.exports = [
{
...commonConfig,
target: 'electron-main',
entry: {
background: './src/background/entry.ts'
},
module: {
rules: [
typescriptRule
]
},
},
{
...commonConfig,
target: 'electron-renderer',
entry: () => {
const devServerEntry = useDevServer ? [ 'webpack/hot/only-dev-server' ] : []
entry = {
app: devServerEntry.concat([
'./src/app/entry.tsx'
]),
'test-ui': './src/test-ui/entry.tsx'
}
if (!production) {
//entry['test-ui'] = devServerEntry.concat([
// './src/test-ui/entry.tsx'
//])
}
return entry
},
plugins: [
...commonPlugins,
new CopyPlugin({
patterns: [ { from: 'src/static' } ]
}),
// Extract CSS into separate file
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: 'assets/[id].css'
})
],
module: {
rules: [
typescriptRule,
{
test: /\.(less|css)$/,
// We don't extract our CSS in dev mode in order to make hot-reload work
use: useDevServer ?
[
{ loader: 'style-loader', options: {
sourceMap: true,
singleton: true
// Ensures CSS is applied before JS is executed.
// See: https://stackoverflow.com/questions/39400038/how-to-ensure-that-hot-css-loads-before-js-in-webpack-dev-server
} },
{ loader: 'css-loader', options: { sourceMap: true, importLoaders: 2 } },
{ loader: 'less-loader', options: { sourceMap: true, noIeCompat: true } }
] :
[
MiniCssExtractPlugin.loader,
'css-loader',
'less-loader'
]
},
{
test: /\.(png|gif|jpe?g|ico|svg|ttf|woff2?|eot|)(\?.*)?$/i,
// If the asset is smaller than 10kb inline it,
// else, fallback to the file-loader and reference it
loader: 'url-loader',
options: { name: 'assets/[contenthash].[ext]', limit: 10000 }
}
]
},
devServer: {
host: '0.0.0.0', // Make dev-server accessible from local network
port: 3030,
hot: true
}
}
]
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}