mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-23 16:56:50 +08:00
This is already used for more than just themes, and we plan to extend its usage even further
77 lines
2.1 KiB
JavaScript
Vendored
77 lines
2.1 KiB
JavaScript
Vendored
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;
|
|
}
|
|
|
|
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.awaitExpression(
|
|
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);
|
|
},
|
|
},
|
|
};
|
|
}
|