Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Allow named exports to be replaced. #92

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ describe('htmlbars-inline-precompile', function() {
);
});

it('throws error when import statement is not using custom specifier', function() {
plugins[0][1].modules = {
'foo-bar': 'baz',
};

expect(() => transform("import hbs from 'foo-bar'")).toThrow(
/Only `import { baz } from 'foo-bar'` is supported/,
'needed import syntax is present'
);

expect(() => transform("import hbs from 'foo-bar'")).toThrow(
/You used: `import hbs from 'foo-bar'`/,
'used import syntax is present'
);
});

it('replaces tagged template expressions with precompiled version', function() {
let transformed = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
Expand All @@ -122,6 +138,18 @@ describe('htmlbars-inline-precompile', function() {
);
});

it('replaces tagged template expressions with precompiled version for custom import paths with named exports', function() {
plugins[0][1].modules = {
'foo-bar': 'baz',
};

let transformed = transform("import { baz } from 'foo-bar';\nvar compiled = baz`hello`;");

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");'
);
});

it('replaces tagged template expressions with precompiled version for custom import paths', function() {
plugins[0][1].modulePaths = ['ember-cli-htmlbars-inline-precompile'];

Expand Down
39 changes: 32 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,46 @@ module.exports = function(babel) {
ImportDeclaration(path, state) {
let node = path.node;

let modulePaths = state.opts.modulePaths || ['htmlbars-inline-precompile'];
let modules = state.opts.modules || {
'htmlbars-inline-precompile': 'default',
};

if (state.opts.modulePaths) {
let modulePaths = state.opts.modulePaths;

modulePaths.forEach(path => (modules[path] = 'default'));
}

let modulePaths = Object.keys(modules);
let matchingModulePath = modulePaths.find(value => t.isLiteral(node.source, { value }));
let modulePathExport = modules[matchingModulePath];

if (matchingModulePath) {
let first = node.specifiers && node.specifiers[0];
if (!t.isImportDefaultSpecifier(first)) {
let input = state.file.code;
let usedImportStatement = input.slice(node.start, node.end);
let msg = `Only \`import hbs from '${matchingModulePath}'\` is supported. You used: \`${usedImportStatement}\``;
throw path.buildCodeFrameError(msg);
let localName = first.local.name;

if (modulePathExport === 'default') {
if (!t.isImportDefaultSpecifier(first)) {
let input = state.file.code;
let usedImportStatement = input.slice(node.start, node.end);
let msg = `Only \`import hbs from '${matchingModulePath}'\` is supported. You used: \`${usedImportStatement}\``;
throw path.buildCodeFrameError(msg);
}
} else {
if (!t.isImportSpecifier(first) || modulePathExport !== first.imported.name) {
let input = state.file.code;
let usedImportStatement = input.slice(node.start, node.end);
let msg = `Only \`import { ${modulePathExport} } from '${matchingModulePath}'\` is supported. You used: \`${usedImportStatement}\``;

throw path.buildCodeFrameError(msg);
}
}

state.importId =
state.importId || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.rename(first.local.name, state.importId.name);

path.scope.rename(localName, state.importId.name);

path.remove();
}
},
Expand Down