discourse/app/assets/javascripts/asset-processor/add-theme-globals.js
David Taylor 585ba17f56
DEV: Rename theme-transpiler to asset-processor (#35498)
This is already used for more than just themes, and we plan to extend
its usage even further
2025-10-20 14:16:46 +01:00

63 lines
1.7 KiB
JavaScript
Vendored

export default function (babel) {
const { types: t } = babel;
const visitor = {
Program(path) {
const importDeclarations = [];
if (path.scope.bindings.themePrefix) {
const themePrefix = path.scope.bindings.themePrefix;
if (themePrefix.kind !== "module") {
throw new Error(
"`themePrefix` is already defined. Unable to add import."
);
} else if (themePrefix.path.parent.source.value !== "virtual:theme") {
throw new Error(
"`themePrefix` is already imported. Unable to add import from `virtual:theme`."
);
}
} else {
importDeclarations.push(
t.importSpecifier(
t.identifier("themePrefix"),
t.identifier("themePrefix")
)
);
}
if (path.scope.bindings.settings) {
const settings = path.scope.bindings.settings;
if (settings.kind !== "module") {
throw new Error(
"`settings` is already defined. Unable to add import."
);
} else if (settings.path.parent.source.value !== "virtual:theme") {
throw new Error(
"`settings` is already imported. Unable to add import from `virtual:theme`."
);
}
} else {
importDeclarations.push(
t.importSpecifier(t.identifier("settings"), t.identifier("settings"))
);
}
if (importDeclarations.length > 0) {
path.node.body.unshift(
t.importDeclaration(
importDeclarations,
t.stringLiteral("virtual:theme")
)
);
}
},
};
return {
pre(file) {
babel.traverse(file.ast, visitor, file.scope);
file.scope.crawl();
},
};
}