Skip to content

Commit

Permalink
Run importSync transform later, so ember-auto-import can support impo…
Browse files Browse the repository at this point in the history
…rtSync

This runs the transform of importSync in the exit hook, to allow ember-auto-import to do its own transformation *before* us. See embroider-build/ember-auto-import#347
  • Loading branch information
simonihmig committed Mar 2, 2021
1 parent eda697f commit 0a3a148
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 0a3a148

Please sign in to comment.