-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·370 lines (344 loc) · 11.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*********************************
* import webpack plugins
********************************/
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const GasPlugin = require("gas-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin");
const DynamicCdnWebpackPlugin = require("dynamic-cdn-webpack-plugin");
const moduleToCdn = require("module-to-cdn");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
/*********************************
* set up environment variables
********************************/
const dotenv = require("dotenv").config();
const parsed = dotenv.error ? {} : dotenv.parsed;
const envVars = parsed || {};
const PORT = envVars.PORT || 3000;
envVars.NODE_ENV = process.env.NODE_ENV;
envVars.PORT = PORT;
const isProd = process.env.NODE_ENV === "production";
/*********************************
* define entrypoints
********************************/
// our destination directory
const destination = path.resolve(__dirname, "dist");
// define server paths
const serverEntry = "./src/server/index.ts";
// define appsscript.json file path
const copyAppscriptEntry = "./appsscript.json";
// define live development dialog paths
const devDialogEntry = "./dev/index.js";
// define client entry points and output names
const clientEntrypoints = [
{
name: "Sidebar",
entry: "./src/client/views/Sidebar/index.js",
filename: "sidebar", // we'll add the .html suffix to these
template: "./src/client/views/Sidebar/index.html",
},
{
name: "CaptionStyleModal",
entry: "./src/client/views/CaptionStyleModal/index.js",
filename: "caption-style-modal", // we'll add the .html suffix to these
template: "./src/client/views/CaptionStyleModal/index.html",
},
];
// define certificate locations
// see "npm run setup:https" script in package.json
const keyPath = path.resolve(__dirname, "./certs/key.pem");
const certPath = path.resolve(__dirname, "./certs/cert.pem");
const pfxPath = path.resolve(__dirname, "./certs/cert.pfx"); // if needed for Windows
/*********************************
* Declare settings
********************************/
// webpack settings for copying files to the destination folder
const copyFilesConfig = {
name: "COPY FILES - appsscript.json",
mode: "production", // unnecessary for this config, but removes console warning
entry: copyAppscriptEntry,
output: {
path: destination,
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: copyAppscriptEntry,
to: destination,
},
],
}),
],
};
// webpack settings used by both client and server
const sharedClientAndServerConfig = {
context: __dirname,
};
// webpack settings used by all client entrypoints
const clientConfig = ({ isDevClientWrapper }) => ({
...sharedClientAndServerConfig,
mode: isProd ? "production" : "development",
output: {
path: destination,
// this file will get added to the html template inline
// and should be put in .claspignore so it is not pushed
filename: "main.js",
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
},
module: {
rules: [
// typescript config
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
// only enable react-refresh for dev builds, and not when building the dev client "wrapper"
options: {
plugins: [
!isProd &&
!isDevClientWrapper &&
require.resolve("react-refresh/babel"),
].filter(Boolean),
},
},
{
loader: "ts-loader",
},
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
// only enable react-refresh for dev builds, and not when building the dev client "wrapper"
options: {
plugins: [
!isProd &&
!isDevClientWrapper &&
require.resolve("react-refresh/babel"),
].filter(Boolean),
},
},
},
// we could add support for scss here
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
});
// DynamicCdnWebpackPlugin settings
// these settings help us load 'react', 'react-dom' and the packages defined below from a CDN
// see https://github.com/enuchi/React-Google-Apps-Script#adding-new-libraries-and-packages
const DynamicCdnWebpackPluginConfig = {
// set "verbose" to true to print console logs on CDN usage while webpack builds
verbose: false,
resolver: (packageName, packageVersion, options) => {
const packageSuffix = isProd ? ".min.js" : ".js";
const moduleDetails = moduleToCdn(packageName, packageVersion, options);
// don't externalize react during development due to issue with react-refresh
// https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/334
if (!isProd && packageName === "react") {
return null;
}
// return defaults if Dynamic CDN plugin finds package
if (moduleDetails) {
return moduleDetails;
}
// define custom CDN configuration for new packages
// "name" should match the package being imported
// FnF
switch (packageName) {
case "semantic-ui-react":
return {
name: packageName,
var: "semanticUIReact",
version: packageVersion,
url: `https://unpkg.com/semantic-ui-react@${packageVersion}/dist/umd/semantic-ui-react.min.js`,
};
// Example for react-transition-group
// case 'react-transition-group':
// return {
// name: packageName,
// var: 'ReactTransitionGroup',
// version: packageVersion,
// url: `https://unpkg.com/react-transition-group@${packageVersion}/dist/react-transition-group${packageSuffix}`,
// };
default:
return null;
}
},
};
// webpack settings used by each client entrypoint defined at top
const clientConfigs = clientEntrypoints.map(clientEntrypoint => {
const isDevClientWrapper = false;
return {
...clientConfig({ isDevClientWrapper }),
name: clientEntrypoint.name,
entry: clientEntrypoint.entry,
plugins: [
!isProd && new webpack.HotModuleReplacementPlugin(),
!isProd && new ReactRefreshWebpackPlugin(),
new webpack.DefinePlugin({
"process.env": JSON.stringify(envVars),
}),
new HtmlWebpackPlugin({
template: clientEntrypoint.template,
filename: `${clientEntrypoint.filename}${isProd ? "" : "-impl"}.html`,
inlineSource: "^[^(//)]+.(js|css)$", // embed all js and css inline, exclude packages with '//' for dynamic cdn insertion
}),
// add the generated js code to the html file inline
new HtmlWebpackInlineSourcePlugin(),
// this plugin allows us to add dynamically load packages from a CDN
new DynamicCdnWebpackPlugin(DynamicCdnWebpackPluginConfig),
].filter(Boolean),
};
});
// webpack settings for devServer https://webpack.js.org/configuration/dev-server/
const devServer = {
hot: true,
port: PORT,
https: true,
};
if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
// use key and cert settings only if they are found
devServer.https = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
};
}
// If mkcert -install cannot be used on Windows machines (in pipeline, for example), the
// script at test/generate-cert.ps1 can be used to create a .pfx cert
if (fs.existsSync(pfxPath)) {
// use pfx file if it's found
devServer.https = {
pfx: fs.readFileSync(pfxPath),
passphrase: "abc123",
};
}
// webpack settings for the development client wrapper
const devClientConfigs = clientEntrypoints.map(clientEntrypoint => {
envVars.FILENAME = clientEntrypoint.filename;
const isDevClientWrapper = true;
return {
...clientConfig({ isDevClientWrapper }),
name: `DEVELOPMENT: ${clientEntrypoint.name}`,
entry: devDialogEntry,
plugins: [
new webpack.DefinePlugin({
"process.env": JSON.stringify(envVars),
}),
new HtmlWebpackPlugin({
template: "./dev/index.html",
// this should match the html files we load in src/server/ui.js
filename: `${clientEntrypoint.filename}.html`,
inlineSource: "^[^(//)]+.(js|css)$", // embed all js and css inline, exclude packages with '//' for dynamic cdn insertion
}),
new HtmlWebpackInlineSourcePlugin(),
new DynamicCdnWebpackPlugin({}),
],
};
});
// webpack settings used by the server-side code
const serverConfig = {
...sharedClientAndServerConfig,
name: "SERVER",
// server config can't use 'development' mode
// https://github.com/fossamagna/gas-webpack-plugin/issues/135
mode: isProd ? "production" : "none",
entry: serverEntry,
output: {
filename: "code.js",
path: destination,
libraryTarget: "this",
},
resolve: {
extensions: [".ts", ".js", ".json"],
},
module: {
rules: [
// typescript config
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
},
{
loader: "ts-loader",
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
// ecma 5 is needed to support Rhino "DEPRECATED_ES5" runtime
// can use ecma 6 if V8 runtime is used
ecma: 5,
warnings: false,
parse: {},
compress: {
properties: false,
},
mangle: false,
module: false,
output: {
beautify: true,
// support custom function autocompletion
// https://developers.google.com/apps-script/guides/sheets/functions
comments: /@customfunction/,
},
},
}),
],
},
plugins: [
new webpack.DefinePlugin({
// replace any env variables in client-side code like PORT and NODE_ENV with actual values
"process.env": JSON.stringify(envVars),
"process.env.NODE_ENV": JSON.stringify(
isProd ? "production" : "development"
),
}),
new GasPlugin({
// removes need for assigning public server functions to "global"
autoGlobalExportsFiles: [serverEntry],
}),
],
};
module.exports = [
// 1. Copy appsscript.json to destination,
// 2. Set up webpack dev server during development
// Note: devServer settings are only read in the first element when module.exports is an array
{ ...copyFilesConfig, ...(isProd ? {} : { devServer }) },
// 3. Create the server bundle
serverConfig,
// 4. Create one client bundle for each client entrypoint.
...clientConfigs,
// 5. Create a development dialog bundle for each client entrypoint during development.
...(isProd ? [] : devClientConfigs),
];