deno.land / x / lume@v2.1.4 / plugins / esbuild.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410import { 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", }, });}
Version Info