-
Notifications
You must be signed in to change notification settings - Fork 23
/
esbuild.mjs
179 lines (169 loc) · 6.2 KB
/
esbuild.mjs
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
import * as esbuild from 'esbuild';
import { lessLoader } from 'esbuild-plugin-less';
import fs from 'node:fs';
import path from 'node:path';
let entryPoints = [
{ out: 'articleDetails', in: 'assets/article/details/index.js' },
{ out: 'articleArchive', in: 'assets/article/archive/index.js' },
{ out: 'core', in: 'assets/core/index.js' },
{ out: 'dashboard', in: 'assets/dashboard/core/index.js' },
{ out: 'dashboardApproval', in: 'assets/dashboard/approval/index.js' },
{ out: 'dashboardArticle', in: 'assets/dashboard/article/index.js' },
{ out: 'dashboardCareeropportunity', in: 'assets/dashboard/careeropportunity/index.js' },
{ out: 'dashboardEvents', in: 'assets/dashboard/events/index.js' },
{ out: 'dashboardGallery', in: 'assets/dashboard/gallery/index.js' },
{ out: 'dashboardGroups', in: 'assets/dashboard/groups/index.js' },
{ out: 'dashboardHobbies', in: 'assets/dashboard/hobbies/index.jsx' },
{ out: 'dashboardPosters', in: 'assets/dashboard/posters/index.js' },
{ out: 'dashboardWebshop', in: 'assets/dashboard/webshop/index.js' },
{ out: 'stagingTheme', in: 'assets/core/less/staging_theme.less' },
{ out: 'eventsMail', in: 'assets/events/mail/index.js' },
{ out: 'feedback', in: 'assets/feedback/index.js' },
{ out: 'frontpage', in: 'assets/frontpage/index.jsx' },
{ out: 'profiles', in: 'assets/profiles/index.js' },
{ out: 'webshop', in: 'assets/webshop/index.js' },
{ out: 'wiki', in: 'assets/wiki/index.js' },
];
const outdir = "bundles/esbuild/";
let result = await esbuild.build({
entryPoints: entryPoints,
format: 'esm',
bundle: true,
outdir: outdir,
outbase: "assets",
entryNames: "[dir]/[name]-[hash]",
assetNames: "[hash]",
nodePaths: ["assets"],
loader: {
'.ttf': 'dataurl',
'.eot': 'dataurl',
'.woff': 'dataurl',
'.woff2': 'dataurl',
'.gif': 'dataurl',
'.svg': 'dataurl',
},
plugins: [lessLoader()],
platform: "browser",
// we could use https://github.com/marcofugaro/browserslist-to-esbuild
target: ['chrome109', 'firefox109', 'safari16', 'edge109'],
splitting: true,
minify: process.env.NODE_ENV === "production" || false,
sourcemap: process.env.NODE_ENV === "production" || false,
metafile: true,
define: {
'import.meta.env.ENVIRONMENT': JSON.stringify(process.env.ENVIRONMENT ?? "dev"),
}
});
// We are very sneaky here, we used to use webpack, with https://github.com/django-webpack/webpack-bundle-tracker
// which produces a file similar to esbuild's `metafile`
/*
The end result should look like:
{
"status": "done",
"assets": {
"060b2710bdbbe3dfe48b58d59bd5f1fb.svg": {
"name": "060b2710bdbbe3dfe48b58d59bd5f1fb.svg",
"path": "ABSOLUTE/onlineweb4/bundles/webpack/060b2710bdbbe3dfe48b58d59bd5f1fb.svg"
},
"careeropportunity-6aecb6e831ca4099e031.css": {
"name": "careeropportunity-6aecb6e831ca4099e031.css",
"path": "ABSOLUTE/onlineweb4/bundles/webpack/careeropportunity-6aecb6e831ca4099e031.css"
},
"careeropportunity-6aecb6e831ca4099e031.css.map": {
"name": "careeropportunity-6aecb6e831ca4099e031.css.map",
"path": "ABSOLUTE/onlineweb4/bundles/webpack/careeropportunity-6aecb6e831ca4099e031.css.map"
},
"careeropportunity-6aecb6e831ca4099e031.js": {
"name": "careeropportunity-6aecb6e831ca4099e031.js",
"path": "ABSOLUTE/onlineweb4/bundles/webpack/careeropportunity-6aecb6e831ca4099e031.js"
},
"careeropportunity-6aecb6e831ca4099e031.js.map": {
"name": "careeropportunity-6aecb6e831ca4099e031.js.map",
"path": "ABSOLUTE/onlineweb4/bundles/webpack/careeropportunity-6aecb6e831ca4099e031.js.map"
},
},
"chunks": {
"careeropportunity": [
"careeropportunity-6aecb6e831ca4099e031.css",
"careeropportunity-6aecb6e831ca4099e031.js",
"careeropportunity-6aecb6e831ca4099e031.css.map",
"careeropportunity-6aecb6e831ca4099e031.js.map"
],
}
}
esbuild's metafile is defined here: https://esbuild.github.io/api/#metafile
we generate and write it automatically
and has this schema:
interface Metafile {
inputs: {
[path: string]: {
bytes: number
imports: {
path: string
kind: string
external?: boolean
original?: string
}[]
format?: string
}
}
outputs: {
[path: string]: {
bytes: number
inputs: {
[path: string]: {
bytesInOutput: number
}
}
imports: {
path: string
kind: string
external?: boolean
}[]
exports: string[]
entryPoint?: string
cssBundle?: string
}
}
}
*/
let fake_webpack_stats_file = {
status: "done",
assets: {},
chunks: {}
};
for (const [filepath, output] of Object.entries(result.metafile.outputs)) {
let relative_filename = filepath.slice(outdir.length);
let absPath = path.resolve(`./${filepath}`);
fake_webpack_stats_file.assets[relative_filename] = {
name: relative_filename,
path: absPath,
};
// could use binary search, only one of the entrypoints is ever relevant
for (const { out: chunk, in: entryPoint } of entryPoints) {
let chunk_content = [];
if (output.entryPoint && output.entryPoint === entryPoint) {
chunk_content.push(relative_filename);
let map = `${relative_filename}.map`;
if (result.metafile.outputs[map]) {
chunk_content.push(map);
}
if (output.cssBundle) {
let relative_css_filename = output.cssBundle.slice(outdir.length)
chunk_content.push(relative_css_filename);
let map = `${relative_css_filename}.map`;
if (result.metafile.outputs[map]) {
chunk_content.push(map);
}
}
}
if (!Array.isArray(fake_webpack_stats_file.chunks[chunk])) {
fake_webpack_stats_file.chunks[chunk] = [];
}
for (const c of chunk_content) {
fake_webpack_stats_file.chunks[chunk].push(c);
}
}
}
fs.writeFileSync('meta.json', JSON.stringify(result.metafile));
fs.writeFileSync('webpack-stats.json', JSON.stringify(fake_webpack_stats_file));