Skip to content

Commit

Permalink
fix: Correct htmlFilePath detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Bomkamp committed Nov 28, 2024
1 parent 0287329 commit 2075b7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 11 additions & 12 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,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 [
Expand Down Expand Up @@ -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() {
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 2075b7e

Please sign in to comment.