generated from marceloaugusto80/electron-react-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
200 lines (171 loc) · 4.49 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const path = require("path");
const HtmlPlugin = require("html-webpack-plugin");
const HtmlExternalsPlugin = require("html-webpack-externals-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const DefinePlugin = require("webpack").DefinePlugin;
const CopyWebpackPlugin = require("copy-webpack-plugin");
// const to avoid typos
const DEVELOPMENT = "development";
const PRODUCTION = "production";
function createRenderConfig(isDev) {
return {
context: path.join(__dirname, "src"),
target: "electron-renderer", // any other target value makes react-hot-loader stop working
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".json"],
},
mode: isDev ? DEVELOPMENT : PRODUCTION,
devtool: isDev ? "inline-source-map" : "none",
entry: {
polyfill: "@babel/polyfill",
"render-process": "./render-process.tsx",
},
output: {
filename: isDev ? "[name].js" : "[name].[hash].js",
path: path.join(__dirname, "dist"),
},
externals: {
react: "React",
"react-dom": "ReactDOM",
"react-router-dom": "ReactRouterDOM",
fs: "require('fs')", // we must add node native functions as externals to be able to use them. see ./src/views/FooView.tsx.
},
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDev,
},
},
"css-loader",
"sass-loader",
],
},
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-typescript",
"@babel/preset-react",
"@babel/preset-env",
],
plugins: [
"@babel/plugin-proposal-class-properties",
"react-hot-loader/babel",
],
},
},
},
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ["!main-process.*.js"], // config for electron-main deletes this file
}),
new MiniCssExtractPlugin({
filename: "main.css",
}),
new HtmlPlugin({
filename: "index.html",
template: "index.html",
cache: true,
}),
new HtmlExternalsPlugin({
cwpOptions: { context: path.join(__dirname, "node_modules") },
externals: [
{
module: "react",
global: "React",
entry: isDev
? "umd/react.development.js"
: "umd/react.production.min.js",
},
{
module: "react-dom",
global: "ReactDOM",
entry: isDev
? "umd/react-dom.development.js"
: "umd/react-dom.production.min.js",
},
{
module: "react-router-dom",
global: "ReactRouterDOM",
entry: isDev
? "umd/react-router-dom.js"
: "umd/react-router-dom.min.js",
},
],
}),
],
devServer: isDev
? {
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 9000,
}
: undefined,
};
}
function createMainConfig(isDev) {
return {
context: path.join(__dirname, "src"),
target: "electron-main",
mode: isDev ? DEVELOPMENT : PRODUCTION,
entry: {
"main-process": "./main-process.ts",
},
output: {
filename: "[name].js",
path: path.join(__dirname, "dist"),
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-typescript", "@babel/preset-env"],
},
},
},
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ["main-process.*.js"],
}),
// inject this becaus the main process uses different logic for prod and dev.
new DefinePlugin({
ENVIRONMENT: JSON.stringify(isDev ? DEVELOPMENT : PRODUCTION), // this variable name must match the one declared in the main process file.
}),
// electron-packager needs the package.json file. the "../" is because context is set to the ./src folder
new CopyWebpackPlugin(["../package.json"]),
],
};
}
module.exports = function (env) {
// env variable is passed by webpack through the cli. see package.json scripts.
const isDev = env.NODE_ENV == DEVELOPMENT;
const target = env.target;
const configFactory =
target == "main" ? createMainConfig : createRenderConfig;
const config = configFactory(isDev);
console.log(
"\n##\n## BUILDING BUNDLE FOR: " +
(target == "main" ? "main process" : "render process") +
"\n## CONFIGURATION: " +
(isDev ? DEVELOPMENT : PRODUCTION) +
"\n##\n"
);
return config;
};