-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
160 lines (141 loc) · 3.99 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
160
const path = require('path')
const fs = require('fs')
const { spawn } = require('child_process')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const YAML = require('yaml')
// Pick up Electron version [X.Y] from builder configuration:
const { rendererTarget, mainTarget } = (() => {
const file = fs.readFileSync('./electron-builder.yml', 'utf8')
const configuration = YAML.parse(file)
const version = configuration.electronVersion.match(/(\d+\.\d+)/)[0]
return {
rendererTarget: 'electron' + version + '-renderer',
mainTarget: 'electron' + version + '-main'
}
})()
const RULES = {
javascript: {
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader']
},
css: {
// css-loader: resolve/load required/imported CSS dependencies from JavaScript
// style-loader: wrap CSS string from css-loader with <style> tag
// Note: loaders are applied from right to left, i.e. css-loader -> style-loader
//
test: /\.(scss|css)$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
image: {
test: /\.(png|svg|jpe?g|gif)$/i,
use: [{
loader: 'file-loader',
options: {
name: 'img/[name].[ext]'
}
}]
},
font: {
test: /\.(eot|svg|ttf|woff|woff2)$/,
type: 'asset/resource'
},
sourcemap: {
test: /\.js$/,
enforce: 'pre',
use: [
{
loader: 'source-map-loader',
options: {
filterSourceMappingUrl: (url, resourcePath) => {
// Consume own (@syncpoint) sourcemaps; remove others.
if (/@syncpoint/g.test(resourcePath)) return 'consume'
else return 'remove'
}
}
}
]
}
}
const rules = () => Object.values(RULES)
const mode = env => env.production ? 'production' : 'development'
const rendererConfig = (env, argv) => ({
context: path.resolve(__dirname, 'src/renderer'),
target: rendererTarget,
// In production mode webpack applies internal optimization/minification:
// no additional plugins necessary.
// For advanced options: babel-minify-webpack-plugin: https://webpack.js.org/plugins/babel-minify-webpack-plugin
mode: mode(env),
stats: 'errors-only',
module: { rules: rules() },
entry: {
renderer: ['./index.js']
},
plugins: [
// Title is managed by BrowserWindow title option.
new HtmlWebpackPlugin(),
new webpack.ExternalsPlugin('commonjs', ['leveldown'])
],
externals: {
// unused dependencies referenced by jsPDF
canvg: 'canvg',
html2canvas: 'html2canvas',
dompurify: 'dompurify'
}
})
const mainConfig = (env, argv) => ({
context: path.resolve(__dirname, 'src/main'),
target: mainTarget,
mode: mode(env),
stats: 'errors-only',
entry: {
main: './main.js'
},
plugins: [
// NOTE: Required. Else "Error: No native build was found for ..."
new webpack.ExternalsPlugin('commonjs', ['leveldown'])
],
externals: {
// unused dependencies referenced by jsPDF
canvg: 'canvg',
html2canvas: 'html2canvas',
dompurify: 'dompurify'
}
})
const devServer = env => {
if (env.production) return ({}) // no development server for production
return ({
devServer: {
static: {
directory: path.resolve(__dirname, 'dist')
},
setupMiddlewares: (middlewares, devServer) => {
spawn(
'electron',
['.'],
{ shell: true, env: process.env, stdio: 'inherit' }
)
.on('close', code => process.exit(code))
.on('error', error => console.error(error))
return middlewares
}
}
})
}
const devtool = env => {
if (env.production) return ({ devtool: 'source-map' })
else return ({ devtool: 'eval-source-map' })
}
module.exports = (env, argv) => {
env = env || {}
// Merge development server and devtool to renderer configuration when necessary:
const renderer = Object.assign(
{},
rendererConfig(env, argv),
devServer(env),
devtool(env)
)
const main = mainConfig(env, argv)
return [renderer, main]
}