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

Avoid sharing list of previously added imports. #355

Merged
merged 3 commits into from
Mar 12, 2021
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
30 changes: 30 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ describe('htmlbars-inline-precompile', function () {
`);
});

it('does not error when transpiling multiple modules with a single plugin config', function () {
let transpiled = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);

expect(transpiled).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";

var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);

transpiled = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);

expect(transpiled).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";

var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

it('passes options when used as a call expression', function () {
let source = 'hello';
transform(`import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('${source}');`);
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ module.exports = function (babel) {
}
}

let allAddedImports = Object.create(null);

function processModuleApiPolyfill(state) {
for (let module in allAddedImports) {
let addedImports = allAddedImports[module];
for (let module in state.allAddedImports) {
let addedImports = state.allAddedImports[module];

for (let addedImport in addedImports) {
let { path } = addedImports[addedImport];
Expand Down Expand Up @@ -187,8 +185,11 @@ module.exports = function (babel) {
let useEmberModule = Boolean(options.useEmberModule);
let moduleOverrides = options.moduleOverrides;

state.allAddedImports = Object.create(null);

state.ensureImport = (exportName, moduleName) => {
let addedImports = (allAddedImports[moduleName] = allAddedImports[moduleName] || {});
let addedImports = (state.allAddedImports[moduleName] =
state.allAddedImports[moduleName] || {});

if (addedImports[exportName]) return addedImports[exportName].id;

Expand Down
4 changes: 3 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports.registerRefs = (newPath, getRefPaths) => {

for (let ref of refPaths) {
let binding = ref.scope.getBinding(ref.node.name);
binding.reference(ref);
if (binding !== undefined) {
binding.reference(ref);
}
}
};