Skip to content

Commit

Permalink
improve & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Mar 22, 2024
1 parent 28fe99b commit c016f1f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default function main(context: typeof Babel): unknown {
if (callee.referencesImport('@embroider/macros', 'importSync')) {
let specifier = path.node.arguments[0];
if (specifier?.type !== 'StringLiteral') {
let relativePath;
let relativePath: string = '';
let property;
if (
specifier.type === 'BinaryExpression' &&
Expand All @@ -136,9 +136,21 @@ export default function main(context: typeof Babel): unknown {
property = specifier.right;
}
if (specifier.type === 'TemplateLiteral' && specifier.expressions[0].type === 'Identifier') {
relativePath = specifier.quasis[0].value.cooked;
relativePath = specifier.quasis[0].value.cooked!;
property = specifier.expressions[0];
}
// babel might transform template form `../my-path/${id}` to '../my-path/'.concat(id)
if (
specifier.type === 'CallExpression' &&
specifier.callee.type === 'MemberExpression' &&
specifier.callee.property.type === 'Identifier' &&
specifier.callee.property.name === 'concat' &&
specifier.callee.object.type === 'StringLiteral' &&
specifier.arguments[0]?.type === 'Identifier'
) {
relativePath = specifier.callee.object.value;
property = specifier.arguments[0];
}
if (property && relativePath && relativePath.startsWith('.')) {
const resolvedPath = resolve(dirname((state as any).filename), relativePath);
let entries: string[] = [];
Expand All @@ -147,10 +159,14 @@ export default function main(context: typeof Babel): unknown {
}
const obj = t.objectExpression(
entries.map(e => {
const key = e.split('.').slice(0, -1).join('.');
let key = e.split('.')[0];
const rest = e.split('.').slice(1, -1);
if (rest.length) {
key += `.${rest}`;
}
const id = t.callExpression(
state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'),
[state.importUtil.import(path, join(resolvedPath, key).replace(/\\/g, '/'), '*')]
[state.importUtil.import(path, join(relativePath, key).replace(/\\/g, '/'), '*')]
);
return t.objectProperty(t.stringLiteral(key), id);
})
Expand All @@ -161,7 +177,7 @@ export default function main(context: typeof Babel): unknown {
return;
} else {
throw new Error(
`importSync eager mode only supports dynamic paths which are relative, must start with a '.'`
`importSync eager mode only supports dynamic paths which are relative, must start with a '.', had ${specifier.type}`
);
}
}
Expand Down
55 changes: 55 additions & 0 deletions packages/macros/tests/babel/import-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,60 @@ describe('importSync', function () {
`);
expect(code).toMatch(/import \* as _importSync\d from "my-plugin"/);
});
test('importSync accepts template argument with dynamic part', () => {
let code = transform(`
import { importSync } from '@embroider/macros';
function getFile(file) {
return importSync(\`../../\${file}\`).default;
}
`);
expect(code).toEqual(`import esc from "../../src/addon/es-compat2";
import * as _importSync0 from "../../";
import * as _importSync20 from "../../README";
import * as _importSync30 from "../../jest.config";
import * as _importSync40 from "../../node_modules";
import * as _importSync50 from "../../package";
import * as _importSync60 from "../../src";
import * as _importSync70 from "../../tests";
function getFile(file) {
return {
"": esc(_importSync0),
"README": esc(_importSync20),
"jest.config": esc(_importSync30),
"node_modules": esc(_importSync40),
"package": esc(_importSync50),
"src": esc(_importSync60),
"tests": esc(_importSync70)
}[file].default;
}`);
});

test('importSync accepts template argument with string binary expression', () => {
let code = transform(`
import { importSync } from '@embroider/macros';
function getFile(file) {
return importSync('../../' + file).default;
}
`);
expect(code).toEqual(`import esc from "../../src/addon/es-compat2";
import * as _importSync0 from "../../";
import * as _importSync20 from "../../README";
import * as _importSync30 from "../../jest.config";
import * as _importSync40 from "../../node_modules";
import * as _importSync50 from "../../package";
import * as _importSync60 from "../../src";
import * as _importSync70 from "../../tests";
function getFile(file) {
return {
"": esc(_importSync0),
"README": esc(_importSync20),
"jest.config": esc(_importSync30),
"node_modules": esc(_importSync40),
"package": esc(_importSync50),
"src": esc(_importSync60),
"tests": esc(_importSync70)
}[file].default;
}`);
});
});
});

0 comments on commit c016f1f

Please sign in to comment.