forked from Munawwar/preact-mpa-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
84 lines (79 loc) · 2.2 KB
/
build.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
import { build } from 'esbuild';
import glob from 'tiny-glob';
import rimraf from 'rimraf';
import {
publicDirectoryRelative,
ssrDirectoryRelative,
publicURLPath,
publicDirectory,
ssrDirectory
} from './server/paths.js';
import { promises as fs } from 'node:fs';
import { parseArgs } from 'node:util';
const {
values: {
dev: isDevMode
}
} = parseArgs({
options: {
dev: {
type: 'boolean',
default: false
}
}
});
const [entryPoints] = await Promise.all([
glob('./client/pages/**/*.page.jsx'),
// clean current dist/
rimraf('dist/')
]);
// console.log('entryPoints', entryPoints);
const commonConfig = {
entryPoints,
entryNames: '[dir]/[name]-[hash]',
outbase: 'client/',
publicPath: publicURLPath,
format: 'esm',
bundle: true,
metafile: true,
sourcesContent: isDevMode,
loader: {
'.svg': 'file',
'.png': 'file',
'.jpg': 'file',
'.jpeg': 'file',
'.webp': 'file'
},
resolveExtensions: ['.jsx', '.ts', '.tsx'],
jsxImportSource: 'preact',
jsx: 'automatic'
};
// Why 2 builds?
// 1. Client side build - JS, CSS, images etc for the client side
// 2. SSR build - Non-minified full page JS for server side for better stack traces
// And also because preact-render-to-string includes node.js copy of preact. If you don't exclude preact from the build,
// you would cause two preact copies (one in the bundled JS and one from preact-render-to-string)
// Also note, using hashed SSR files just like client build, so that server can dynamic import() changes without restarting full server
const [publicBuildResult, ssrBuildResult] = await Promise.all([
build({
outdir: publicDirectoryRelative,
splitting: true,
minify: true,
sourcemap: true,
...commonConfig
}),
build({
outdir: ssrDirectoryRelative,
splitting: false,
minify: false,
sourcemap: 'inline',
external: ['preact', 'preact-render-to-string'],
...commonConfig
})
]);
if (publicBuildResult && publicBuildResult.metafile) {
await Promise.all([
fs.writeFile(`${publicDirectory}/metafile.json`, JSON.stringify(publicBuildResult.metafile, 0, 2)),
fs.writeFile(`${ssrDirectory}/metafile.json`, JSON.stringify(ssrBuildResult.metafile, 0, 2))
]);
}