From 0287329cbf0b320721337fcbe1b5d82bc3b07453 Mon Sep 17 00:00:00 2001 From: Robin Bomkamp Date: Tue, 26 Nov 2024 12:44:45 +0100 Subject: [PATCH 1/3] fix: Do not inject code into html files while ssr builds --- src/plugins/pluginAddEntry.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/plugins/pluginAddEntry.ts b/src/plugins/pluginAddEntry.ts index 5cd4465..68a7b18 100644 --- a/src/plugins/pluginAddEntry.ts +++ b/src/plugins/pluginAddEntry.ts @@ -21,6 +21,15 @@ const addEntry = ({ let _command: string; let emitFileId: string; let viteConfig: any; + let _ssrBuild: boolean; + + function injectHtml() { + return inject === 'html' && !_ssrBuild; + } + + function injectEntry() { + return inject === 'entry' || _ssrBuild; + } return [ { @@ -55,7 +64,7 @@ const addEntry = ({ }); }, transformIndexHtml(c) { - if (inject !== 'html') return; + if (!injectHtml()) return; return c.replace( '', ` @@ -127,7 +141,7 @@ const addEntry = ({ } }, transform(code, id) { - if (inject === 'entry' && entryFiles.some((file) => id.endsWith(file))) { + if (injectEntry() && entryFiles.some((file) => id.endsWith(file))) { const injection = ` import ${JSON.stringify(entryPath)}; `; From 2075b7e27fe241def860b4b1ce16fe2188cab415 Mon Sep 17 00:00:00 2001 From: Robin Bomkamp Date: Thu, 28 Nov 2024 08:49:58 +0100 Subject: [PATCH 2/3] fix: Correct htmlFilePath detection --- src/plugins/pluginAddEntry.ts | 23 +++++++++++------------ src/virtualModules/virtualRemoteEntry.ts | 4 ++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/plugins/pluginAddEntry.ts b/src/plugins/pluginAddEntry.ts index 68a7b18..e257cfd 100644 --- a/src/plugins/pluginAddEntry.ts +++ b/src/plugins/pluginAddEntry.ts @@ -9,6 +9,10 @@ interface AddEntryOptions { inject?: 'entry' | 'html'; } +function getFirstHtmlEntryFile(entryFiles: string[]): string | undefined { + return entryFiles.find((file) => file.endsWith('.html')); +} + const addEntry = ({ entryName, entryPath, @@ -17,18 +21,17 @@ const addEntry = ({ }: AddEntryOptions): Plugin[] => { let devEntryPath = entryPath.startsWith('virtual:mf') ? '@id/' + entryPath : entryPath; let entryFiles: string[] = []; - let htmlFilePath: string; + let htmlFilePath: string | undefined; let _command: string; let emitFileId: string; let viteConfig: any; - let _ssrBuild: boolean; function injectHtml() { - return inject === 'html' && !_ssrBuild; + return inject === 'html' && htmlFilePath; } function injectEntry() { - return inject === 'entry' || _ssrBuild; + return inject === 'entry' || !htmlFilePath; } return [ @@ -76,26 +79,22 @@ const addEntry = ({ { name: 'add-entry', enforce: 'post', - config(config, { isSsrBuild }) { - _ssrBuild = isSsrBuild ?? false; - }, configResolved(config) { viteConfig = config; - _ssrBuild = _ssrBuild || config.ssr != null; - const inputOptions = config.build.rollupOptions.input; if (!inputOptions) { htmlFilePath = path.resolve(config.root, 'index.html'); } else if (typeof inputOptions === 'string') { entryFiles = [inputOptions]; - htmlFilePath = path.resolve(config.root, inputOptions); } else if (Array.isArray(inputOptions)) { entryFiles = inputOptions; - htmlFilePath = path.resolve(config.root, inputOptions[0]); } else if (typeof inputOptions === 'object') { entryFiles = Object.values(inputOptions); - htmlFilePath = path.resolve(config.root, Object.values(inputOptions)[0]); + } + + if (entryFiles && entryFiles.length > 0) { + htmlFilePath = getFirstHtmlEntryFile(entryFiles); } }, buildStart() { diff --git a/src/virtualModules/virtualRemoteEntry.ts b/src/virtualModules/virtualRemoteEntry.ts index 075186b..a316e0e 100644 --- a/src/virtualModules/virtualRemoteEntry.ts +++ b/src/virtualModules/virtualRemoteEntry.ts @@ -54,6 +54,7 @@ export function generateLocalSharedImportMap() { ${Array.from(getUsedShares()) .map((key) => { const shareItem = getNormalizeShareItem(key); + if (!shareItem) return null; return ` ${JSON.stringify(key)}: { name: ${JSON.stringify(key)}, @@ -82,11 +83,13 @@ export function generateLocalSharedImportMap() { } `; }) + .filter((x) => x !== null) .join(',')} } const usedRemotes = [${Object.keys(getUsedRemotesMap()) .map((key) => { const remote = options.remotes[key]; + if (!remote) return null; return ` { entryGlobalName: ${JSON.stringify(remote.entryGlobalName)}, @@ -96,6 +99,7 @@ export function generateLocalSharedImportMap() { } `; }) + .filter((x) => x !== null) .join(',')} ] export { From cfa065a49c827433757a1ca88051faf961079335 Mon Sep 17 00:00:00 2001 From: Robin Bomkamp Date: Fri, 29 Nov 2024 10:04:55 +0100 Subject: [PATCH 3/3] fix: Dev mode is not working in SvelteKit --- src/plugins/pluginAddEntry.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/plugins/pluginAddEntry.ts b/src/plugins/pluginAddEntry.ts index e257cfd..83eee64 100644 --- a/src/plugins/pluginAddEntry.ts +++ b/src/plugins/pluginAddEntry.ts @@ -75,6 +75,19 @@ const addEntry = ({ )}>` ); }, + transform(code, id) { + if (id.includes('node_modules') || inject !== 'html' || htmlFilePath) { + return; + } + + if (id.includes('.svelte-kit') && id.includes('internal.js')) { + const src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/'); + return code.replace( + //g, + '' + ); + } + }, }, { name: 'add-entry',