-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
ultra.ts
272 lines (225 loc) · 6.07 KB
/
ultra.ts
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
import {
Builder,
crayon,
deepMerge,
EntrypointConfig,
FileBag,
fromFileUrl,
join,
relative,
resolve,
sprintf,
VirtualFile,
} from "./deps.ts";
import { Logger } from "../logger.ts";
import type { BuildOptions, BuildResult, DenoConfig } from "./types.ts";
type DefaultBuildOptions = Omit<
BuildOptions,
"browserEntrypoint" | "serverEntrypoint" | "plugin"
>;
const defaultOptions: DefaultBuildOptions = {
root: Deno.cwd(),
output: ".ultra",
importMapPath: "./importMap.json",
ignored: [".git", join("**", ".DS_Store")],
};
export class UltraBuilder extends Builder {
public options: BuildOptions;
public browserEntrypoint?: string;
public serverEntrypoint: string;
constructor(
options: Partial<BuildOptions>,
private onSuccessCallback?: (
context: UltraBuilder,
result: BuildResult,
) => Promise<void> | void,
) {
const resolvedOptions = deepMerge<BuildOptions>(defaultOptions, options);
const root = resolvedOptions.root;
const output = resolvedOptions.output
? resolve(root, resolvedOptions.output)
: resolve(root, ".ultra");
super({
root,
output,
name: "ultra",
importMapPath: resolvedOptions.importMapPath,
logLevel: "INFO",
compiler: {
minify: true,
jsxImportSource: resolvedOptions.jsxImportSource,
sourceMaps: resolvedOptions.sourceMaps,
},
});
// Override the logger
this.log = new Logger("INFO");
this.options = resolvedOptions;
this.browserEntrypoint = this.options.browserEntrypoint
? this.makeRelative(this.options.browserEntrypoint)
: undefined;
this.serverEntrypoint = this.makeRelative(this.options.serverEntrypoint);
this.#initEntrypoints();
this.#initIgnored();
this.#initHashed();
}
entrypoint(path: string, config: EntrypointConfig) {
this.entrypoints.set(path, config);
return this;
}
ignore(path: string) {
this.ignored.push(super.globToRegExp(path));
return this;
}
dynamicImportIgnore(path: string) {
this.dynamicImportIgnored.push(super.globToRegExp(path));
return this;
}
contentHash(path: string) {
this.hashed.push(super.globToRegExp(path));
return this;
}
async build(): Promise<BuildResult> {
/**
* Clean the output directory
*/
await this.cleanOutput();
/**
* Gather all sources from root
*/
const sources = await this.gatherSources();
/**
* Copy sources to output
*/
const buildSources = await this.copySources(sources);
/**
* Execute the build
*/
const result = await super.build(buildSources);
/**
* Remove the dev importMap
*/
await buildSources.get(this.options.importMapPath).then((source) =>
source.remove()
);
/**
* Generate ./asset-manifest.json
*/
await this.#generateAssetManifest(buildSources);
/**
* Patch deno.json
*/
await this.#patchDenoJsonConfig(buildSources);
/**
* If a build plugin is provided, execute it's build hooks
*/
if (this.options.plugin) {
const plugin = this.options.plugin;
try {
this.log.info(
sprintf(
"Starting build plugin %s",
crayon.lightBlue(plugin.name),
),
);
await plugin.onBuild(this, result);
} catch (error) {
this.log.error(
sprintf(
"Build plugin %s failed, see below for details",
crayon.lightBlue(plugin.name),
),
);
this.log.error(error);
Deno.exit(1);
}
}
this.log.success("Build complete");
if (this.onSuccessCallback) {
await this.onSuccessCallback(this, result);
}
return result;
}
#initEntrypoints() {
if (this.browserEntrypoint) {
this.entrypoint(this.browserEntrypoint, {
vendorOutputDir: "browser",
target: "browser",
});
}
this.entrypoint(this.serverEntrypoint, {
vendorOutputDir: "server",
target: "deno",
});
}
#initIgnored() {
/**
* Deno.mainModule will most definitely be a build.ts file in the project
* We always exclude this.
*/
const mainModule = this.makeRelative(Deno.mainModule);
this.setIgnored([
mainModule,
...(this.options.ignored || []),
]);
// Exclude the compiler middleware from the build output
this.dynamicImportIgnore(import.meta.resolve("../middleware/compiler.ts"));
}
#initHashed() {
const hashed = [
"./src/**/*.+(ts|tsx|js|jsx|css)",
"./public/**/*.+(css|ico|webp|avif|jpg|png|svg|gif|otf|ttf|woff)",
];
if (this.browserEntrypoint) {
hashed.push(this.browserEntrypoint);
}
this.setHashed(hashed);
this.setCompiled([
"./**/*.+(ts|tsx|js|jsx)",
]);
}
async #generateAssetManifest(sources: FileBag) {
this.log.info("Generating asset-manifest.json");
const manifest = this.toManifest(sources, {
ignore: [
"./deno.json",
"./importMap*.json",
],
prefix: "/",
});
const assetManifest = manifest.map(([relative, absolute]) => {
return [
relative.replace("./public/", "./"),
absolute.replace("/public/", "/"),
];
});
const assetManifestSource = new VirtualFile(
"./asset-manifest.json",
this.context.output,
JSON.stringify(assetManifest, null, 2),
);
await this.copySource(assetManifestSource, this.context.output);
}
async #patchDenoJsonConfig(sources: FileBag) {
this.log.info("Patching deno.json");
const source = await sources.get("./deno.json");
if (source) {
const denoConfig = await source.readAsJson<DenoConfig>();
if (denoConfig) {
if (denoConfig.compilerOptions?.jsx) {
denoConfig.compilerOptions.jsx = "react-jsx";
}
denoConfig.importMap = "./importMap.server.json";
await source.writeJson(denoConfig, true);
}
}
return source;
}
makeRelative(path: string) {
return `./${
relative(
this.context.root,
fromFileUrl(path),
)
}`;
}
}