-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: esmsh provider es module shims url (#354)
- Loading branch information
1 parent
dea1bbb
commit afdac03
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |