Skip to content

Commit

Permalink
fix: ensure shared file is resolved properly in all situations (svelt…
Browse files Browse the repository at this point in the history
…ejs#566)

using a relative import means it may be relatively imported from within a node module, which leads to wrong results

fixes sveltejs#555
  • Loading branch information
dummdidumm authored Oct 23, 2024
1 parent 32f9b17 commit c931f0e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/repl/src/lib/workers/bundler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ async function get_bundle(
}
}

if (importee === shared_file) return importee;

// importing from another file in REPL
if (local_files_lookup.has(importee) && (!importer || local_files_lookup.has(importer)))
return importee;
Expand Down Expand Up @@ -394,7 +396,7 @@ async function get_bundle(
result.js.code +=
'\n\n' +
`
import { styles as $$_styles } from './__shared.js';
import { styles as $$_styles } from '${shared_file}';
const $$__style = document.createElement('style');
$$__style.textContent = ${JSON.stringify(result.css.code)};
document.head.append($$__style);
Expand Down Expand Up @@ -476,6 +478,8 @@ async function get_bundle(

export type BundleResult = ReturnType<typeof bundle>;

const shared_file = '$$__shared__.js';

async function bundle({
uid,
files,
Expand All @@ -500,7 +504,7 @@ async function bundle({
version.split('.')[0] >= '5'
? `
import { unmount as u } from 'svelte';
import { styles } from './__shared.js';
import { styles } from '${shared_file}';
export { mount, untrack } from 'svelte';
export {default as App} from './App.svelte';
export function unmount(component) {
Expand All @@ -509,7 +513,7 @@ async function bundle({
}
`
: `
import { styles } from './__shared.js';
import { styles } from '${shared_file}';
export {default as App} from './App.svelte';
export function mount(component, options) {
return new component(options);
Expand All @@ -525,10 +529,10 @@ async function bundle({
text: true
});

lookup.set('./__shared.js', {
lookup.set(shared_file, {
type: 'file',
name: '__shared.js',
basename: '__shared.js',
name: shared_file,
basename: shared_file,
contents: `
export let styles = [];
`,
Expand Down

0 comments on commit c931f0e

Please sign in to comment.