Skip to content

Commit

Permalink
Merge pull request #694 from simonihmig/import-sync-later
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Mar 2, 2021
2 parents eda697f + 0a3a148 commit 8e5f1f1
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ export default function main(context: unknown): unknown {
return;
}

// importSync doesn't evaluate to a static value, so it's implemented
// directly here, not in evaluateMacroCall.
if (callee.referencesImport('@embroider/macros', 'importSync')) {
let r = identifier('require');
state.generatedRequires.add(r);
callee.replaceWith(r);
// we handle importSync in the exit hook
return;
}

Expand Down Expand Up @@ -131,6 +127,23 @@ export default function main(context: unknown): unknown {
path.replaceWith(buildLiterals(result.value));
}
},
exit(path: NodePath<CallExpression>, state: State) {
let callee = path.get('callee');
if (!callee.isIdentifier()) {
return;
}
// importSync doesn't evaluate to a static value, so it's implemented
// directly here, not in evaluateMacroCall.
// We intentionally do this on exit here, to allow other transforms to handle importSync before we do
// For example ember-auto-import needs to do some custom transforms to enable use of dynamic template strings,
// so its babel plugin needs to see and handle the importSync call first!
if (callee.referencesImport('@embroider/macros', 'importSync')) {
let r = identifier('require');
state.generatedRequires.add(r);
callee.replaceWith(r);
return;
}
},
},
ReferencedIdentifier(path: NodePath<Identifier>, state: State) {
for (let candidate of [
Expand All @@ -139,7 +152,8 @@ export default function main(context: unknown): unknown {
'getConfig',
'getOwnConfig',
'failBuild',
'importSync',
// we cannot check importSync, as the babel transform runs on exit, so *after* this check
// 'importSync',
'isDevelopingApp',
'isDevelopingThisPackage',
'isTesting',
Expand Down

0 comments on commit 8e5f1f1

Please sign in to comment.