Skip to content

Commit 2264c0d

Browse files
committed
Enhance inline Svelte plugin to support module script exports detection, improving import handling for components based on export presence.
1 parent 8616230 commit 2264c0d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hvniel/vite-plugin-svelte-inline-component",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"license": "MIT",
55
"author": "Haniel Ubogu <https://github.com/HanielU>",
66
"repository": {

src/lib/plugin/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default function inlineSveltePlugin({
6666
`^const\\s+([a-zA-Z0-9_$]+)\\s*=\\s*(?:${tagGroup})\\s*(\`[\\s\\S]*?\`)`,
6767
"gm"
6868
);
69+
const moduleScriptExportRE = /<script\s+module\s*>[^<]*\bexport\b/;
6970

7071
const VIRT = "virtual:inline-svelte/";
7172
const RSLV = "\0" + VIRT;
@@ -197,8 +198,14 @@ export default function inlineSveltePlugin({
197198
local = `Inline_${hash}`;
198199
hashToLocal.set(hash, local);
199200
if (!cache.has(virt)) cache.set(virt, markupWithDeps);
200-
const ns = `__InlineNS_${hash}`;
201-
importsToAdd += `import * as ${ns} from '${virt}';\nconst ${local}=Object.assign(${ns}.default, ${ns});\n`;
201+
202+
const hasModuleExports = moduleScriptExportRE.test(rawMarkup);
203+
if (hasModuleExports) {
204+
const ns = `__InlineNS_${hash}`;
205+
importsToAdd += `import * as ${ns} from '${virt}';\nconst ${local}=Object.assign(${ns}.default, ${ns});\n`;
206+
} else {
207+
importsToAdd += `import ${local} from '${virt}';\n`;
208+
}
202209
}
203210

204211
componentInfo.set(compName, { local, virt });
@@ -262,8 +269,14 @@ export default function inlineSveltePlugin({
262269
hashToLocal.set(hash, local);
263270
const virt = `${VIRT}${hash}.js`;
264271
if (!cache.has(virt)) cache.set(virt, markupWithGlobals);
265-
const ns = `__InlineNS_${hash}`;
266-
importsToAdd += `import * as ${ns} from '${virt}';\nconst ${local}=Object.assign(${ns}.default, ${ns});\n`;
272+
273+
const hasModuleExports = moduleScriptExportRE.test(rawMarkup);
274+
if (hasModuleExports) {
275+
const ns = `__InlineNS_${hash}`;
276+
importsToAdd += `import * as ${ns} from '${virt}';\nconst ${local}=Object.assign(${ns}.default, ${ns});\n`;
277+
} else {
278+
importsToAdd += `import ${local} from '${virt}';\n`;
279+
}
267280
}
268281

269282
ms.overwrite(m.index, tplRE.lastIndex, local);

0 commit comments

Comments
 (0)