Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix side effecting ember cli htmlbars import #7

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

it('removes no-op imports from ember-cli-htmlbars', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
buildOptions({
enableLegacyModules: ['ember-cli-htmlbars'],
}),
],
];

// This occurs with rollup builds when it does is import hoisting
// (which is done to improve overall speed of loading module graphs)
// Since this package shouldn't exist at runtime, it must be removed entirely
let transformed = transform("import 'ember-cli-htmlbars';\nlet hello = `world`;");

expect(transformed).toMatchInlineSnapshot(`"let hello = \`world\`;"`);
});

it('leaves tagged template expressions alone when ember-cli-htmlbars is disabled', function () {
let transformed = transform(
"import { hbs as baz } from 'ember-cli-htmlbars';\nvar compiled = baz`hello`;"
Expand Down
17 changes: 17 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ export default function makePlugin<O>(
},
},

ImportDeclaration(path: NodePath<t.ImportDeclaration>, state: State) {
/**
* These modules don't actually exist, so if a build tool leaves them in
* (without specifiers), we need to remove those (otherwise the app/library
* author will run in to a runtime error due to the module not existing).
*/
if (path.node.specifiers.length > 0) return;

let importPath = path.node.source.value;

for (let module of configuredModules(state)) {
if (importPath === module.moduleName) {
state.util.removeAllImports(importPath);
}
}
},

TaggedTemplateExpression(path: NodePath<t.TaggedTemplateExpression>, state: State) {
let tagPath = path.get('tag');

Expand Down