Skip to content

Commit

Permalink
Merge pull request #1081 from embroider-build/fix-importSync-scope
Browse files Browse the repository at this point in the history
fix importSync scope collision
  • Loading branch information
ef4 committed Jan 19, 2022
2 parents 47a1455 + 89b0457 commit 631eb5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/macros/src/babel/macros-babel-plugin.ts
Expand Up @@ -130,6 +130,9 @@ export default function main(context: typeof Babel): unknown {
path.replaceWith(state.importUtil.import(path, specifier.value, '*'));
state.calledIdentifiers.add(callee.node);
} else {
if (path.scope.hasBinding('require')) {
path.scope.rename('require');
}
let r = t.identifier('require');
state.generatedRequires.add(r);
path.replaceWith(
Expand Down
11 changes: 11 additions & 0 deletions packages/macros/tests/babel/import-sync.test.ts
Expand Up @@ -15,6 +15,17 @@ describe('importSync', function () {
expect(code).toMatch(/esc\(require\(['"]foo['"]\)\)/);
expect(code).not.toMatch(/window/);
});
test('importSync leaves existing binding for require alone', () => {
let code = transform(`
import { importSync } from '@embroider/macros';
import require from 'require';
importSync('foo');
require('x');
`);
expect(code).toMatch(/esc\(require\(['"]foo['"]\)\)/);
expect(code).toMatch(/import _require from 'require'/);
expect(code).toMatch(/_require\(['"]x['"]\)/);
});
test('aliased importSync becomes require', () => {
let code = transform(`
import { importSync as i } from '@embroider/macros';
Expand Down

0 comments on commit 631eb5d

Please sign in to comment.