Skip to content

Commit

Permalink
fix: Inject hostInit as entry if there are no html entry files (#193)
Browse files Browse the repository at this point in the history
* fix: Do not inject code into html files while ssr builds

* fix: Correct htmlFilePath detection

---------

Co-authored-by: Robin Bomkamp <[email protected]>
  • Loading branch information
RobinBomkamp and Robin Bomkamp authored Nov 30, 2024
1 parent 30ab658 commit 4ae3c09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/plugins/pluginAddEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,11 +21,19 @@ 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;

function injectHtml() {
return inject === 'html' && htmlFilePath;
}

function injectEntry() {
return inject === 'entry' || !htmlFilePath;
}

return [
{
name: 'add-entry',
Expand Down Expand Up @@ -55,7 +67,7 @@ const addEntry = ({
});
},
transformIndexHtml(c) {
if (inject !== 'html') return;
if (!injectHtml()) return;
return c.replace(
'<head>',
`<head><script type="module" src=${JSON.stringify(
Expand All @@ -75,13 +87,14 @@ const addEntry = ({
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() {
Expand All @@ -108,7 +121,7 @@ const addEntry = ({
}
},
generateBundle(options, bundle) {
if (inject !== 'html') return;
if (!injectHtml()) return;
const file = this.getFileName(emitFileId);
const scriptContent = `
<script type="module" src="${viteConfig.base + file}"></script>
Expand All @@ -127,7 +140,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)};
`;
Expand Down
4 changes: 4 additions & 0 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down Expand Up @@ -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)},
Expand All @@ -96,6 +99,7 @@ export function generateLocalSharedImportMap() {
}
`;
})
.filter((x) => x !== null)
.join(',')}
]
export {
Expand Down

0 comments on commit 4ae3c09

Please sign in to comment.