Skip to content

Commit

Permalink
fix: obeys esbuild's external argument (#102)
Browse files Browse the repository at this point in the history
Co-authored-by: Luca Casonato <[email protected]>
  • Loading branch information
BlackAsLight and lucacasonato authored Jan 22, 2024
1 parent 1efbec1 commit b4cc95a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
24 changes: 23 additions & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "./mod.ts";
import { denoLoaderPlugin } from "./src/plugin_deno_loader.ts";
import { esbuildNative, esbuildWasm, join } from "./test_deps.ts";
import { assert, assertEquals } from "./test_deps.ts";
import { assert, assertEquals, assertStringIncludes } from "./test_deps.ts";

await esbuildNative.initialize({});
await esbuildWasm.initialize({});
Expand Down Expand Up @@ -709,3 +709,25 @@ Deno.test("uncached data url", async (t) => {
assertEquals(value, rand);
});
});

Deno.test("externals", async (t) => {
await testLoader(t, LOADERS, async (esbuild, loader) => {
const configPath = join(Deno.cwd(), "testdata", "config_ref.json");
const res = await esbuild.build({
...DEFAULT_OPTS,
plugins: [
...denoPlugins({ configPath, loader }),
],
bundle: true,
platform: "neutral",
entryPoints: ["./testdata/externals.ts"],
external: ["foo:bar", "foo:baz/*", "bar"],
});
assertEquals(res.warnings, []);
assertEquals(res.errors, []);
assertEquals(res.outputFiles.length, 1);
const output = res.outputFiles[0];
assertStringIncludes(output.text, "foo:bar");
assertStringIncludes(output.text, "foo:baz/bar");
});
});
20 changes: 20 additions & 0 deletions src/plugin_deno_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export function denoResolverPlugin(
let importMap: ImportMap | null = null;
let nodeModulesPaths: Set<string>;

const externalRegexps: RegExp[] = (build.initialOptions.external ?? [])
.map((external) => {
const regexp = new RegExp(
"^" + external.replace(/[-/\\^$+?.()|[\]{}]/g, "\\$&").replace(
/\*/g,
".*",
) + "$",
);
return regexp;
});

build.onStart(async function onStart() {
nodeModulesPaths = new Set<string>();

Expand Down Expand Up @@ -139,6 +150,15 @@ export function denoResolverPlugin(
resolved = new URL(args.path, referrer);
}

for (const externalRegexp of externalRegexps) {
if (externalRegexp.test(resolved.href)) {
return {
path: resolved.href,
external: true,
};
}
}

// Now pass the resolved specifier back into the resolver, for a second
// pass. Now plugins can perform any resolution they want on the fully
// resolved specifier.
Expand Down
1 change: 1 addition & 0 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { esbuildNative, esbuildWasm };
export {
assert,
assertEquals,
assertStringIncludes,
assertThrows,
} from "https://deno.land/[email protected]/assert/mod.ts";
export { join } from "https://deno.land/[email protected]/path/mod.ts";
2 changes: 2 additions & 0 deletions testdata/externals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { a } from "foo:bar";
export { b } from "foo:baz/bar";

0 comments on commit b4cc95a

Please sign in to comment.