-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathwebpack.config.cjs
270 lines (258 loc) · 7.13 KB
/
webpack.config.cjs
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
require("dotenv").config();
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const { DefinePlugin, optimize } = require("webpack");
const chalk = require("chalk");
const { transform } = require("@formatjs/ts-transformer");
/**
*
* @param {Object} [options]
* @param {string} [options.appEntry=./src/index.tsx]
* @param {string} [options.backendHost]
* @param {Object} [options.devConfig]
* @param {number} [options.devConfig.port]
* @param {boolean} [options.devConfig.enableHmr]
* @param {boolean} [options.devConfig.enableHttps]
* @param {string} [options.devConfig.appOrigin]
* @param {string} [options.devConfig.appId] - Deprecated in favour of appOrigin
* @param {string} [options.devConfig.certFile]
* @param {string} [options.devConfig.keyFile]
* @returns {Object}
*/
function buildConfig({
devConfig,
appEntry = path.join(__dirname, "src", "index.tsx"),
backendHost = process.env.CANVA_BACKEND_HOST,
} = {}) {
const mode = devConfig ? "development" : "production";
if (!backendHost) {
console.error(
chalk.redBright.bold("BACKEND_HOST is undefined."),
`Refer to "Customizing the backend host" in the README.md for more information.`,
);
process.exit(-1);
} else if (backendHost.includes("localhost") && mode === "production") {
console.error(
chalk.redBright.bold(
"BACKEND_HOST should not be set to localhost for production builds!",
),
`Refer to "Customizing the backend host" in the README.md for more information.`,
);
}
return {
mode,
context: path.resolve(__dirname, "./"),
entry: {
app: appEntry,
},
target: "web",
resolve: {
alias: {
assets: path.resolve(__dirname, "assets"),
utils: path.resolve(__dirname, "utils"),
styles: path.resolve(__dirname, "styles"),
src: path.resolve(__dirname, "src"),
},
extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
},
infrastructureLogging: {
level: "none",
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader",
options: {
transpileOnly: true,
getCustomTransformers() {
return {
before: [
transform({
overrideIdFn: "[sha512:contenthash:base64:6]",
}),
],
};
},
},
},
],
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("cssnano")({ preset: "default" })],
},
},
},
],
},
{
test: /\.(png|jpg|jpeg)$/i,
type: "asset/inline",
},
{
test: /\.(woff|woff2)$/,
type: "asset/inline",
},
{
test: /\.svg$/,
oneOf: [
{
issuer: /\.[jt]sx?$/,
resourceQuery: /react/, // *.svg?react
use: ["@svgr/webpack", "url-loader"],
},
{
type: "asset/resource",
parser: {
dataUrlCondition: {
maxSize: 200,
},
},
},
],
},
{
test: /\.css$/,
include: /node_modules/,
use: [
"style-loader",
"css-loader",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("cssnano")({ preset: "default" })],
},
},
},
],
},
],
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
}),
],
},
output: {
filename: `[name].js`,
path: path.resolve(__dirname, "dist"),
clean: true,
},
plugins: [
new DefinePlugin({
BACKEND_HOST: JSON.stringify(backendHost),
}),
// Apps can only submit a single JS file via the developer portal
new optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
],
...buildDevConfig(devConfig),
};
}
/**
*
* @param {Object} [options]
* @param {number} [options.port]
* @param {boolean} [options.enableHmr]
* @param {boolean} [options.enableHttps]
* @param {string} [options.appOrigin]
* @param {string} [options.appId] - Deprecated in favour of appOrigin
* @param {string} [options.certFile]
* @param {string} [options.keyFile]
* @returns {Object|null}
*/
function buildDevConfig(options) {
if (!options) {
return null;
}
const { port, enableHmr, appOrigin, appId, enableHttps, certFile, keyFile } =
options;
let devServer = {
server: enableHttps
? {
type: "https",
options: {
cert: certFile,
key: keyFile,
},
}
: "http",
host: "localhost",
historyApiFallback: {
rewrites: [{ from: /^\/$/, to: "/app.js" }],
},
port,
client: {
logging: "verbose",
},
static: {
directory: path.resolve(__dirname, "assets"),
publicPath: "/assets",
},
};
if (enableHmr && appOrigin) {
devServer = {
...devServer,
allowedHosts: new URL(appOrigin).hostname,
headers: {
"Access-Control-Allow-Origin": appOrigin,
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Private-Network": "true",
},
};
} else if (enableHmr && appId) {
// Deprecated - App ID should not be used to configure HMR in the future and can be safely removed
// after a few months.
console.warn(
"Enabling Hot Module Replacement (HMR) with an App ID is deprecated, please see the README.md on how to update.",
);
const appDomain = `app-${appId.toLowerCase().trim()}.canva-apps.com`;
devServer = {
...devServer,
allowedHosts: appDomain,
headers: {
"Access-Control-Allow-Origin": `https://${appDomain}`,
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Private-Network": "true",
},
};
} else {
if (enableHmr && !appOrigin) {
console.warn(
"Attempted to enable Hot Module Replacement (HMR) without configuring App Origin... Disabling HMR.",
);
}
devServer.webSocketServer = false;
}
return {
devtool: "source-map",
devServer,
};
}
module.exports = () => buildConfig();
module.exports.buildConfig = buildConfig;