Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Dec 18, 2024
1 parent 11487cd commit c5dff80
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,23 @@ export default function main(context: typeof Babel): unknown {
// our getConfig and getOwnConfig macros are supposed to be able to absorb
// optional chaining. To make that work we need to see the optional chaining
// before preset-env compiles them away.

function inGetConfig(path: NodePath<any> | null) {
while (path) {
if (path.isCallExpression()) {
const callee = path.get('callee');
if (callee.referencesImport('@embroider/macros', 'getOwnConfig') || callee.referencesImport('@embroider/macros', 'getGlobalConfig') || callee.referencesImport('@embroider/macros', 'getConfig')) {
return true;
}
}
path = path.parentPath;
}
return false;
}

(visitor as any).OptionalMemberExpression = {
enter(path: NodePath<t.OptionalMemberExpression>, state: State) {
if (state.opts.mode === 'compile-time') {
if (state.opts.mode === 'compile-time' && inGetConfig(path as any)) {
let result = new Evaluator({ state }).evaluate(path);
if (result.confident) {
path.replaceWith(buildLiterals(result.value, context));
Expand Down

0 comments on commit c5dff80

Please sign in to comment.