Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: obeys esbuild's external argument #102

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -657,3 +657,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";
Loading