deno.land / x / lume@v2.1.4 / plugins / esbuild.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { isAbsolutePath, isUrl, normalizePath, replaceExtension,} from "../core/utils/path.ts";import { merge } from "../core/utils/object.ts";import { readDenoConfig } from "../core/utils/deno_config.ts";import { log } from "../core/utils/log.ts";import { read } from "../core/utils/read.ts";import { concurrent } from "../core/utils/concurrent.ts";import { build, BuildOptions, OutputFile, stop } from "../deps/esbuild.ts";import { extname, fromFileUrl, posix, toFileUrl } from "../deps/path.ts";import { prepareAsset, saveAsset } from "./source_maps.ts";import { Page } from "../core/file.ts";import textLoader from "../core/loaders/text.ts";
import type Site from "../core/site.ts";import type { DenoConfig } from "../core/utils/deno_config.ts";
export interface Options { /** The list of extensions this plugin applies to */ extensions?: string[];
/** * Global options for esm.sh CDN used to fetch NPM packages * @see https://esm.sh/#docs */ esm?: EsmOptions;
/** * The options for esbuild * @see https://esbuild.github.io/api/#general-options */ options?: BuildOptions;}
export interface EsmOptions { /** To include the ?dev option to all packages */ dev?: boolean;
/** Configure the cjs-exports option for each package */ cjsExports?: Record<string, string[] | string>;
/** Configure the deps for each package */ deps?: Record<string, string[] | string>;}
const denoConfig = await readDenoConfig();
// Default optionsexport const defaults: Options = { extensions: [".ts", ".js"], esm: {}, options: { plugins: [], bundle: true, format: "esm", minify: true, keepNames: true, platform: "browser", target: "esnext", treeShaking: true, },};
const contentSymbol = Symbol.for("contentSymbol");
interface LumeBuildOptions extends BuildOptions { [contentSymbol]: Record<string, string>;}
export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
// Configure jsx automatically if ( options.extensions.some((ext) => ext.endsWith(".tsx") || ext.endsWith(".jsx") ) ) { options.options = { ...buildJsxConfig(denoConfig?.config), ...options.options, }; }
// Sync the jsxDev option with esm.dev if (options.esm.dev) { options.options.jsxDev = true; } else if (options.options.jsxDev) { options.esm.dev = true; }
return (site: Site) => { site.loadAssets(options.extensions);
function resolve(path: string) { // "npm:" specifiers are currently not supported in import.meta.resolve() // https://github.com/denoland/deno/issues/21298 if (!path.startsWith("npm:")) { path = import.meta.resolve(path); }
if (!path.startsWith("npm:")) { return { path, namespace: "deno", }; }
const name = path.replace(/^npm:/, ""); const url = new URL(`https://esm.sh/${name}`);
if (options.esm.dev) { url.searchParams.set("dev", "true"); }
// cjs exports const cjs_exports = options.esm.cjsExports?.[name];
if (cjs_exports) { url.searchParams.set( "cjs-exports", Array.isArray(cjs_exports) ? cjs_exports.join(",") : cjs_exports, ); }
// deps const deps = options.esm.deps?.[name];
if (deps) { url.searchParams.set( "deps", Array.isArray(deps) ? deps.join(",") : deps, ); }
return { path: url.href, namespace: "deno", }; }
const prefix = toFileUrl(site.src()).href; const lumeLoaderPlugin = { name: "lumeLoader", // deno-lint-ignore no-explicit-any setup(build: any) { const { initialOptions } = build; build.onResolve({ filter: /.*/ }, (args: ResolveArguments) => { const { path, importer } = args;
// Absolute url if (isUrl(path)) { return resolve(path); }
// Resolve the relative url if (isUrl(importer) && path.match(/^[./]/)) { return resolve(new URL(path, importer).href); }
// It's a npm package if (path.startsWith("npm:")) { return resolve(path); }
if (!isUrl(path)) { return resolve(isAbsolutePath(path) ? toFileUrl(path).href : path); }
return resolve(path); });
build.onLoad({ filter: /.*/ }, async (args: LoadArguments) => { let { path, namespace } = args;
// It's one of the entry point files if (initialOptions[contentSymbol][path]) { return { contents: initialOptions[contentSymbol][path], loader: getLoader(path), }; }
// Read files from Lume if (namespace === "deno") { if (path.startsWith(prefix)) { const file = path.replace(prefix, ""); const content = await site.getContent(file, textLoader);
if (content) { return { contents: content, loader: getLoader(path), }; } } }
// Convert file:// urls to paths if (path.startsWith("file://")) { path = normalizePath(fromFileUrl(path)); }
// Read other files from the filesystem/url const content = await readFile(path); return { contents: content, loader: getLoader(path), }; }); }, }; options.options.plugins?.push(lumeLoaderPlugin);
site.hooks.addEsbuildPlugin = (plugin) => { options.options.plugins?.unshift(plugin); };
site.addEventListener("beforeSave", stop);
/** Run esbuild and returns the output files */ async function runEsbuild( pages: Page[], extraOptions: BuildOptions = {}, ): Promise<[OutputFile[], boolean]> { let enableAllSourceMaps = false; const entryContent: Record<string, string> = {}; const entryPoints: string[] = [];
pages.forEach((page) => { const { content, filename, enableSourceMap } = prepareAsset(site, page); if (enableSourceMap) { enableAllSourceMaps = true; } entryPoints.push(filename); entryContent[toFileUrl(filename).href] = content; });
const buildOptions: LumeBuildOptions = { ...options.options, write: false, metafile: false, entryPoints, sourcemap: enableAllSourceMaps ? "external" : undefined, ...extraOptions, [contentSymbol]: entryContent, };
const { outputFiles, warnings, errors } = await build( // @ts-expect-error: esbuild uses a SameShape type to prevent the passing // of extra options (which we use to pass the entryContent) buildOptions, );
if (errors.length) { log.error(`[esbuild plugin] Build errors: \n${errors.join("\n")}`); }
if (warnings.length) { log.warn( `[esbuild plugin] Build warnings: \n${warnings.join("\n")}`, ); }
return [outputFiles || [], enableAllSourceMaps]; }
// Splitting mode needs to run esbuild with all pages at the same time if (options.options.splitting) { // Define default options for splitting mode options.options.absWorkingDir ||= site.src(); options.options.outdir ||= "./"; options.options.outbase ||= "."; const basePath = options.options.absWorkingDir;
site.process(options.extensions, async (pages, allPages) => { const [outputFiles, enableSourceMap] = await runEsbuild(pages);
// Save the output code for (const file of outputFiles) { if (file.path.endsWith(".map")) { continue; }
// Search the entry point of this output file const url = normalizePath( normalizePath(file.path).replace(basePath, ""), ); const urlWithoutExt = pathWithoutExtension(url); const entryPoint = pages.find((page) => { const outdir = posix.join( "/", options.options.outdir || ".", pathWithoutExtension(page.data.url), );
return outdir === urlWithoutExt; });
// Get the associated source map const map = enableSourceMap ? outputFiles.find((f) => f.path === `${file.path}.map`) : undefined;
// The page is an entry point if (entryPoint) { entryPoint.data.url = url; // Update the url to .js extension saveAsset(site, entryPoint, file.text, map?.text); } else { // The page is a chunk const page = Page.create({ url }); saveAsset(site, page, file.text, map?.text); allPages.push(page); } } }); } else { // Normal mode runs esbuild for each page site.process( options.extensions, (pages) => concurrent(pages, async (page) => { const [outputFiles] = await runEsbuild([page], { outfile: replaceExtension(page.outputPath, ".js"), });
let mapFile: OutputFile | undefined; let jsFile: OutputFile | undefined;
for (const file of outputFiles) { if (file.path.endsWith(".map")) { mapFile = file; } else { jsFile = file; } }
saveAsset(site, page, jsFile?.text!, mapFile?.text); page.data.url = replaceExtension(page.data.url, ".js"); }), ); } };}
function getLoader(path: string) { const ext = extname(path).toLowerCase();
switch (ext) { case ".ts": case ".mts": return "ts"; case ".tsx": return "tsx"; case ".jsx": return "jsx"; case ".json": return "json"; default: return "js"; }}
interface LoadArguments { path: string; namespace: string; suffix: string; pluginData: unknown;}
interface ResolveArguments { path: string; importer: string; namespace: string; resolveDir: string; kind: string; pluginData: unknown;}
function buildJsxConfig(config?: DenoConfig): BuildOptions | undefined { if (!config) { return; }
const { compilerOptions } = config;
if (compilerOptions?.jsxImportSource) { return { jsx: "automatic", jsxImportSource: compilerOptions.jsxImportSource, }; }}
function pathWithoutExtension(path: string): string { return path.replace(/\.\w+$/, "");}
export async function readFile(path: string): Promise<string> { return await read(path, false, { headers: { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0", }, });}
lume

Version Info

Tagged at
6 months ago