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: esmsh provider es module shims url #354

Merged
merged 2 commits into from
May 19, 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
15 changes: 15 additions & 0 deletions src/common/wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ts-ignore
import { fetch } from '#fetch';
import { parse, init } from 'es-module-lexer';

export async function getMaybeWrapperUrl (moduleUrl, fetchOpts) {
await init;
const source = await (await fetch(moduleUrl, fetchOpts)).text();
const [imports,, facade] = parse(source);
if (facade && imports.length) {
try {
return new URL(imports[0].n, moduleUrl).href;
} catch {}
}
return moduleUrl;
}
5 changes: 5 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { LockResolutions } from "./install/lock.js";
import { getDefaultProviderStrings, type Provider } from "./providers/index.js";
import * as nodemodules from "./providers/nodemodules.js";
import { Resolver } from "./trace/resolver.js";
import { getMaybeWrapperUrl } from './common/wrapper.js';

// Utility exports for users:
export { analyzeHtml };
Expand Down Expand Up @@ -729,6 +730,10 @@ export class Generator {
esmsPkg,
this.traceMap.installer.defaultProvider
)) + "dist/es-module-shims.js";

// detect esmsUrl as a wrapper URL
esmsUrl = await getMaybeWrapperUrl(esmsUrl, this.traceMap.resolver.fetchOpts);

if (htmlUrl || rootUrl)
esmsUrl = relativeUrl(
new URL(esmsUrl),
Expand Down
25 changes: 25 additions & 0 deletions test/html/esmsh.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Generator } from "@jspm/generator";
import assert from "assert";
import { SemverRange } from "sver";

const generator = new Generator({
mapUrl: new URL("./local/page.html", import.meta.url),
env: ["production", "browser"],
defaultProvider: 'esm.sh'
});

const esmsPkg = await generator.traceMap.resolver.resolveLatestTarget(
{ name: "es-module-shims", registry: "npm", ranges: [new SemverRange("*")] },
generator.traceMap.installer.defaultProvider
);
let pins, html

html = `
<!doctype html>
<script type="module">
import 'react';
</script>
`;
pins = await generator.addMappings(html);

assert((await generator.htmlInject(html, { pins })).includes('https://esm.sh/v'));
Loading