Skip to content

Commit

Permalink
fix: include importer in illegal import error message (#12820)
Browse files Browse the repository at this point in the history
closes #12550

Uses the resolveId hook to get the importer name before including it in the error message when loading the illegal import in the load hook.
  • Loading branch information
eltigerchino authored Oct 23, 2024
1 parent 723eb8b commit dcbe422
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-cows-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: include importer in illegal import error message
18 changes: 17 additions & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ async function kit({ svelte_config }) {
}
};

/** @type {Map<string, string>} */
const import_map = new Map();

/** @type {import('vite').Plugin} */
const plugin_virtual_modules = {
name: 'vite-plugin-sveltekit-virtual-modules',
Expand All @@ -372,6 +375,8 @@ async function kit({ svelte_config }) {
`Cannot import ${id} into service-worker code. Only the modules $service-worker and $env/static/public are available in service workers.`
);
}

import_map.set(id, importer);
}

// treat $env/static/[public|private] as virtual
Expand All @@ -398,7 +403,18 @@ async function kit({ svelte_config }) {
})
) {
const relative = normalize_id(id, normalized_lib, normalized_cwd);
throw new Error(`Cannot import ${strip_virtual_prefix(relative)} into client-side code`);

const illegal_module = strip_virtual_prefix(relative);

if (import_map.has(illegal_module)) {
const importer = path.relative(
cwd,
/** @type {string} */ (import_map.get(illegal_module))
);
throw new Error(`Cannot import ${illegal_module} into client-side code: ${importer}`);
}

throw new Error(`Cannot import ${illegal_module} into client-side code`);
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/kit/test/apps/dev-only/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test.describe.serial('Illegal imports', () => {
wait_for_started: false
});
expect(await page.textContent('.message-body')).toBe(
'Cannot import $env/dynamic/private into client-side code'
'Cannot import $env/dynamic/private into client-side code: src/routes/illegal-imports/env/dynamic-private/+page.svelte'
);
});

Expand All @@ -26,7 +26,7 @@ test.describe.serial('Illegal imports', () => {
wait_for_started: false
});
expect(await page.textContent('.message-body')).toBe(
'Cannot import $env/dynamic/private into client-side code'
'Cannot import $env/dynamic/private into client-side code: src/routes/illegal-imports/env/dynamic-private-dynamic-import/+page.svelte'
);
});

Expand All @@ -35,7 +35,7 @@ test.describe.serial('Illegal imports', () => {
wait_for_started: false
});
expect(await page.textContent('.message-body')).toBe(
'Cannot import $env/static/private into client-side code'
'Cannot import $env/static/private into client-side code: src/routes/illegal-imports/env/static-private/+page.svelte'
);
});

Expand All @@ -44,7 +44,7 @@ test.describe.serial('Illegal imports', () => {
wait_for_started: false
});
expect(await page.textContent('.message-body')).toBe(
'Cannot import $env/static/private into client-side code'
'Cannot import $env/static/private into client-side code: src/routes/illegal-imports/env/static-private-dynamic-import/+page.svelte'
);
});

Expand Down

0 comments on commit dcbe422

Please sign in to comment.