Skip to content

Commit

Permalink
resolve npm module imports to cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Oct 18, 2020
1 parent 9709717 commit adc1028
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
18 changes: 17 additions & 1 deletion public/worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/worker.js.map

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ self.addEventListener(
plugins: [
{
name: "repl-plugin",
resolveId(importee: string, importer: string) {
async resolveId(importee: string, importer: string) {
// import x from 'svelte'
if (importee === "svelte") return `${CDN_URL}/svelte/index.mjs`;

// import x from 'svelte/somewhere'
if (importee.startsWith("svelte/")) {
return `${CDN_URL}/svelte/${importee.slice(7)}/index.mjs`;
}

// import x from './file.js'
if (importer && importer.startsWith(`${CDN_URL}/svelte`)) {
const resolved = new URL(importee, importer).href;
Expand All @@ -43,6 +45,27 @@ self.addEventListener(

// repl components
if (component_lookup.has(importee)) return importee;

// relative imports from a remote package
if (importee.startsWith("."))
return new URL(importee, importer).href;

// bare named module imports (importing an npm package)

// get the package.json and load it into memory
const pkg_url = `${CDN_URL}/${importee}/package.json`;
const pkg = JSON.parse(await fetch_package(pkg_url));

// get an entry point from the pkg.json - first try svelte, then modules, then main
if (pkg.svelte || pkg.module || pkg.main) {
// use the aobove url minus `/package.json` to resolve the URL
const url = pkg_url.replace(/\/package\.json$/, "");
return new URL(pkg.svelte || pkg.module || pkg.main, `${url}/`)
.href;
}

// we probably missed stuff, pass it along as is
return importee;
},
async load(id: string) {
if (component_lookup.has(id))
Expand Down

0 comments on commit adc1028

Please sign in to comment.