fix(electron): force main bundle cjs output

This commit is contained in:
wiiiii123
2026-05-09 10:51:22 +07:00
parent 1b81c2dc40
commit 92f7476c64
3 changed files with 162 additions and 2 deletions
+82
View File
@@ -49,6 +49,49 @@ function convertImportLine(line) {
return null;
}
function convertNamedExports(namedSpec, indent = "") {
const statements = [];
for (const rawSpecifier of namedSpec.split(",")) {
const specifier = rawSpecifier.trim();
if (!specifier) {
continue;
}
const aliasMatch = specifier.match(
/^([A-Za-z_$][A-Za-z0-9_$]*)\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)$/,
);
const localName = aliasMatch ? aliasMatch[1] : specifier;
const exportName = aliasMatch ? aliasMatch[2] : specifier;
if (
!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localName) ||
!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(exportName)
) {
return null;
}
statements.push(`${indent}exports.${exportName} = ${localName};`);
}
return statements;
}
function convertExportLine(line) {
const singleLineMatch = line.match(
/^([ \t]*)export\s*\{\s*([^}]*)\s*\}\s*;?[ \t]*$/,
);
if (singleLineMatch) {
const [, indent, namedSpec] = singleLineMatch;
return convertNamedExports(namedSpec, indent);
}
const blockStartMatch = line.match(/^([ \t]*)export\s*\{\s*$/);
if (blockStartMatch) {
return { indent: blockStartMatch[1], specifiers: [] };
}
return null;
}
function updateLexicalState(line, state) {
let mode = state.mode;
let escaped = false;
@@ -296,8 +339,29 @@ export function normalizeElectronMainCjsSource(source) {
const lines = source.split(/\r?\n/);
const normalizedLines = [];
let state = { mode: null };
let exportBlock = null;
for (const line of lines) {
if (exportBlock) {
if (/^[ \t]*\}\s*;?[ \t]*$/.test(line)) {
const statements = convertNamedExports(
exportBlock.specifiers.join(","),
exportBlock.indent,
);
if (statements === null) {
normalizedLines.push(`export {\n${exportBlock.specifiers.join("\n")}\n};`);
} else {
normalizedLines.push(...statements);
changed = true;
}
exportBlock = null;
continue;
}
exportBlock.specifiers.push(line.trim().replace(/,$/, ""));
continue;
}
if (state.mode === null) {
const converted = convertImportLine(line);
if (converted !== null) {
@@ -305,6 +369,17 @@ export function normalizeElectronMainCjsSource(source) {
changed = true;
continue;
}
const convertedExport = convertExportLine(line);
if (convertedExport !== null) {
if (Array.isArray(convertedExport)) {
normalizedLines.push(...convertedExport);
changed = true;
} else {
exportBlock = convertedExport;
}
continue;
}
}
const normalized = replaceImportMetaUrlInCode(line, state);
@@ -348,6 +423,13 @@ export function findElectronMainCjsEsmSyntax(source) {
});
continue;
}
if (/^[ \t]*export\b/.test(line)) {
matches.push({
line: index + 1,
text: line.trim(),
});
continue;
}
}
state = updateLexicalState(line, state);