/
webpack.config.ts
138 lines (130 loc) · 4.68 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
import { AureliaPlugin } from 'aurelia-webpack-plugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import * as path from 'path';
import * as webpack from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import * as merge from 'webpack-merge';
import * as WriteFilePlugin from 'write-file-webpack-plugin';
const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));
const appRoot = process.cwd();
function getBundleAnalyzerPlugin(entryPoints: webpack.Entry): BundleAnalyzerPlugin {
return new BundleAnalyzerPlugin({
reportFilename: `bundle-report-${Object.keys(entryPoints)[0]}.html`,
analyzerMode: 'static',
openAnalyzer: false
});
}
function when(condition: boolean, ...whenTrue: webpack.Plugin[]): webpack.Plugin[] {
return condition ? whenTrue : [];
}
const commonConfig: ({ production }) => webpack.Configuration = (
{ production } = { production: false }
) => ({
mode: production ? 'production' : 'development',
devtool: production ? 'source-map' : 'inline-source-map',
output: { libraryTarget: 'commonjs2' },
optimization: {
minimize: production,
// Aurelia doesn't like this enabled
concatenateModules: false
},
performance: { hints: false },
resolve: {
extensions: ['.ts', '.js'],
modules: ['src', 'node_modules'].map(x => path.resolve(x))
},
module: {
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
issuer: /\.[tj]s$/i
},
{
test: /\.scss$/,
use: ['css-loader', 'sass-loader'],
issuer: /\.html?$/i
},
{ test: /\.ts$/i, use: 'awesome-typescript-loader' },
{ test: /\.html$/i, use: 'html-loader' },
{
// Includes .node binaries in the output
test: /\.node$/,
loader: 'awesome-node-loader',
options: {
name: '[name]-[hash:10].[ext]'
}
},
{
// Ensures our output sourcemap includes sourcemaps from dependencies
test: /\.js$/,
use: 'source-map-loader',
enforce: 'pre',
exclude: [/reflect-metadata/]
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.production': JSON.stringify(production)
}),
// The 'bindings' library finds native binaries dynamically which is
// incompatible with webpack. This replaces 'bindings' with a file
// which has static paths 🤷
new webpack.NormalModuleReplacementPlugin(/^bindings$/, require.resolve('./bindings')),
...when(production, new CleanWebpackPlugin(['dist'], { root: appRoot, verbose: false }))
],
node: {
__dirname: false,
__filename: false,
process: false
},
externals: ['electron']
});
const main = (entryPoints: webpack.Entry) => ({ production } = { production: false }) =>
merge.smart(commonConfig({ production }), {
entry: entryPoints,
target: 'electron-main',
plugins: [
...when(production, getBundleAnalyzerPlugin(entryPoints)),
new CopyWebpackPlugin(['package.json'])
]
});
const preload = (entryPoints: webpack.Entry) => ({ production } = { production: false }) =>
merge.smart(commonConfig({ production }), {
entry: entryPoints,
target: 'electron-renderer',
plugins: [...when(production, getBundleAnalyzerPlugin(entryPoints))]
});
const renderer = (entryPoints: webpack.Entry, nodeIntegration: boolean = true) => (
{ production } = { production: false }
) =>
merge.smart(commonConfig({ production }), {
entry: entryPoints,
target: nodeIntegration ? 'electron-renderer' : 'web',
output: { libraryTarget: nodeIntegration ? 'commonjs2' : 'this' },
plugins: [
new AureliaPlugin({ aureliaApp: undefined, features: { polyfills: 'esnext' } }),
...when(production, getBundleAnalyzerPlugin(entryPoints)),
...when(isDevServer, new webpack.HotModuleReplacementPlugin(), new WriteFilePlugin()),
// Create an HTML file for each entry point
...Object.keys(entryPoints).map(
entry => new HtmlWebpackPlugin({ title: '', filename: `${entry}.html`, chunks: [entry] })
)
]
});
export = [
main({ main: path.resolve(appRoot, 'src/main') }),
renderer({
renderer: path.resolve(appRoot, 'src/renderer')
// Add multiple renderer entry points that can be opened in other windows
// page2: path.resolve(appRoot, 'src/page2')
})
// Bundle for a renderer with nodeIntegration disabled
// renderer({ renderer: path.resolve(appRoot, 'src/renderer') }, false),
//
// Add an extra entry point for a preload script
// preload({ preload: path.resolve(appRoot, 'src/preload') })
];