discourse/frontend/asset-processor/babel-replace-imports.js
Jarek Radosz b27530cdb1
DEV: Remove (again) the old non-rollup plugin compiler (#39419)
This reverts commit 7633f0d0ac, cleans up
the tests workflow, and works around the Chrome crash issue. (see:
https://issues.chromium.org/issues/504886986)

Closes #39050
Closes #39051
Closes #39070
Closes #39100
Closes #39158
Closes #39195
Closes #39255
Closes #39308
2026-04-21 21:07:46 +02:00

113 lines
3.6 KiB
JavaScript
Vendored

import { federatedExportNameFor } from "./federated-modules-helper";
import rollupVirtualImports from "./rollup-virtual-imports";
export default function (babel) {
const { types: t } = babel;
return {
visitor: {
ImportDeclaration(path) {
const moduleName = path.node.source.value;
if (
moduleName.startsWith(".") ||
rollupVirtualImports[moduleName] ||
moduleName.startsWith("discourse/theme-")
) {
return;
}
if (moduleName.startsWith("discourse/plugins/")) {
const parts = moduleName.split("/");
path.node.source = t.stringLiteral(`discourse/plugins/${parts[2]}`);
const getFederatedExportName = (exportedName) => {
const localModuleName = parts.slice(3).join("/");
return federatedExportNameFor(localModuleName, exportedName);
};
const newImportSpecifiers = path.node.specifiers.map((specifier) => {
if (specifier.type === "ImportDefaultSpecifier") {
const federatedExportName = getFederatedExportName("default");
return t.importSpecifier(
t.identifier(specifier.local.name),
t.identifier(federatedExportName)
);
} else if (specifier.type === "ImportNamespaceSpecifier") {
const federatedExportName = getFederatedExportName("*");
return t.importSpecifier(
t.identifier(specifier.local.name),
t.identifier(federatedExportName)
);
} else {
const federatedExportName = getFederatedExportName(
specifier.imported.name
);
return t.importSpecifier(
t.identifier(specifier.local.name),
t.identifier(federatedExportName)
);
}
});
path.node.specifiers = newImportSpecifiers;
return;
}
const namespaceImports = [];
const properties = path.node.specifiers
.map((specifier) => {
if (specifier.type === "ImportDefaultSpecifier") {
return t.objectProperty(
t.identifier("default"),
t.identifier(specifier.local.name)
);
} else if (specifier.type === "ImportNamespaceSpecifier") {
namespaceImports.push(t.identifier(specifier.local.name));
} else {
return t.objectProperty(
t.identifier(specifier.imported.name),
t.identifier(specifier.local.name)
);
}
})
.filter(Boolean);
const replacements = [];
const moduleBrokerLookup = t.callExpression(
t.memberExpression(
t.memberExpression(
t.identifier("window"),
t.identifier("moduleBroker")
),
t.identifier("lookup")
),
[t.stringLiteral(moduleName)]
);
if (properties.length) {
replacements.push(
t.variableDeclaration("const", [
t.variableDeclarator(
t.objectPattern(properties),
moduleBrokerLookup
),
])
);
}
if (namespaceImports.length) {
for (const namespaceImport of namespaceImports) {
replacements.push(
t.variableDeclaration("const", [
t.variableDeclarator(namespaceImport, moduleBrokerLookup),
])
);
}
}
path.replaceWithMultiple(replacements);
},
},
};
}