Skip to content

Commit

Permalink
don't preload dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dburles committed Oct 18, 2023
1 parent 2e3729d commit 53ee6aa
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions resolveImports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default async function resolveImports(module, root = true) {
let modules = [];

await Promise.all(
imports.map(async ({ n: specifier }) => {
if (specifier) {
imports.map(async ({ n: specifier, d }) => {
const dynamic = d > -1;
if (specifier && !dynamic) {
const resolvedModule = path.resolve(path.dirname(module), specifier);
const preloadable = await exists(resolvedModule);

Expand All @@ -37,7 +38,7 @@ export default async function resolveImports(module, root = true) {

const graph = await resolveImports(resolvedModule, false);

if (graph) {
if (graph?.length > 0) {
graph.forEach((module) => modules.push(module));
}
}
Expand Down
2 changes: 1 addition & 1 deletion resolveImportsCached.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async function resolveImportsCached(module) {
} else {
const graph = await resolveImports(module);

if (graph) {
if (graph?.length > 0) {
cache.set(module, graph);
return graph;
}
Expand Down
8 changes: 6 additions & 2 deletions resolveLinkRelations.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ test("resolveLinkRelations", async (t) => {
assert.ok(resolvedModules.includes("/d.mjs"));
assert.ok(resolvedModules.includes("/lib/aa.mjs"));
assert.ok(resolvedModules.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModules.includes("/lib/cc.mjs"));

assert.equal(resolvedModules.length, 5);
assert.equal(resolvedModules.length, 4);

const resolvedModulesCached = await resolveLinkRelations({
appPath: "test-fixtures",
Expand All @@ -25,8 +27,10 @@ test("resolveLinkRelations", async (t) => {
assert.ok(resolvedModulesCached.includes("/d.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/aa.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModulesCached.includes("/lib/cc.mjs"));

assert.equal(resolvedModulesCached.length, 5);
assert.equal(resolvedModulesCached.length, 4);
});

await t.test("can't reach outside of appPath", async () => {
Expand Down
4 changes: 1 addition & 3 deletions test-fixtures/d.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const message = "Hello World";

export default message;
import "./missing-import.mjs";
1 change: 0 additions & 1 deletion test-fixtures/lib/cc.mjs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
import "./missing-import.mjs";

0 comments on commit 53ee6aa

Please sign in to comment.