Skip to content

Commit

Permalink
BREAKING refactor: Don't allow root imports without prefixed / ./ or ../
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
jespertheend committed Apr 30, 2022
1 parent af4a696 commit f407e05
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Importer {
* [dynamic `import()` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports).
* Except with the faked modules applied.
* @template T
* @param {string | URL} url
* @param {string} url
* @returns {Promise<T>}
*/
async import(url) {
Expand Down
12 changes: 7 additions & 5 deletions src/ImportResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,18 @@ export class ImportResolver {
* Once every file has loaded and its import urls replaced with blobs,
* the entry point is imported with a regular async import call.
* @template T
* @param {string | URL} url
* @param {string} url
* @returns {Promise<T>}
*/
async import(url) {
this.#hasMadeImportCall = true;
if (typeof url === "string") {
url = new URL(url, this.#importMeta);
}
const newUrl = resolveModuleSpecifier(
this.#parsedImportMap,
new URL(this.#importMeta),
url,
);
await this.loadImportMap();
const collectedImport = this.createCollectedImport(url.href);
const collectedImport = this.createCollectedImport(newUrl.href);
let module;
try {
module = await import(await collectedImport.getBlobUrl());
Expand Down
18 changes: 8 additions & 10 deletions test/integration/importMap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Deno.test({
},
});

const module = await importer.import("main.js");
const module = await importer.import("./main.js");
assertEquals(module.foo, "foo");
} finally {
await cleanup();
Expand Down Expand Up @@ -83,7 +83,7 @@ Deno.test({
const importer = new Importer(basePath);
importer.setImportMap("./importmap.json");

const module = await importer.import("main.js");
const module = await importer.import("./main.js");
assertEquals(module.foo, "foo");
} finally {
await cleanup();
Expand Down Expand Up @@ -114,7 +114,7 @@ Deno.test({
},
});

const module = await importer.import("main.js");
const module = await importer.import("./main.js");
assertEquals(module.foo, "foo");
} finally {
await cleanup();
Expand Down Expand Up @@ -146,7 +146,7 @@ Deno.test({
},
});

const module = await importer.import("main.js");
const module = await importer.import("./main.js");
assertEquals(module.foo, "foo");
} finally {
await cleanup();
Expand All @@ -156,7 +156,6 @@ Deno.test({

Deno.test({
name: "importing a bare specifier directly without an import map",
ignore: true,
async fn() {
const { cleanup, basePath } = await simpleReplacementDir();

Expand Down Expand Up @@ -191,7 +190,7 @@ Deno.test({
const importer = new Importer(basePath);
await assertRejects(
async () => {
await importer.import("main.js");
await importer.import("./main.js");
},
TypeError,
`Relative import path "bare" not prefixed with / or ./ or ../`,
Expand All @@ -204,7 +203,6 @@ Deno.test({

Deno.test({
name: "importing a bare specifier directly that is not in the import map",
ignore: true,
async fn() {
const { cleanup, basePath } = await simpleReplacementDir();

Expand Down Expand Up @@ -249,7 +247,7 @@ Deno.test({
});
await assertRejects(
async () => {
await importer.import("main.js");
await importer.import("./main.js");
},
TypeError,
`Relative import path "bare" not prefixed with / or ./ or ../`,
Expand All @@ -272,7 +270,7 @@ Deno.test({
const fullImportPath = new URL(importMapFileName, basePath);
await assertRejects(
async () => {
await importer.import("main.js");
await importer.import("./main.js");
},
TypeError,
`Failed install import map from "${fullImportPath}". A network error occurred while fetching the module.`,
Expand Down Expand Up @@ -300,7 +298,7 @@ Deno.test({
const fullImportPath = new URL(importMapFileName, basePath);
await assertRejects(
async () => {
await importer.import("main.js");
await importer.import("./main.js");
},
TypeError,
`Failed install import map from "${fullImportPath}". The resource did not respond with an ok status code (404).`,
Expand Down

0 comments on commit f407e05

Please sign in to comment.