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

Commit

Permalink
Merge pull request #358 from ember-cli/feat/add-template-compiler-pat…
Browse files Browse the repository at this point in the history
…h-option

Adds a `templateCompilerPath` option
  • Loading branch information
rwjblue authored Mar 17, 2021
2 parents 72f6d0e + dc75db8 commit 6f10fc6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions __tests__/mock-precompile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
precompile(value) {
return `precompiledFromPath(${value})`;
},
};
20 changes: 19 additions & 1 deletion __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ describe('htmlbars-inline-precompile', function () {
`);
});

it('supports compilation with templateCompilerPath', function () {
plugins[0][1].templateCompilerPath = require.resolve('./mock-precompile');

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
*/
precompiledFromPath(hello));"
`);
});

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`;"
Expand Down Expand Up @@ -822,7 +840,7 @@ describe('htmlbars-inline-precompile', function () {

expect(optionsReceived).toEqual({
contents: source,
scope: ['foo', 'bar'],
locals: ['foo', 'bar'],
});
});

Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ module.exports = function (babel) {
let propertyName =
property.key.type === 'Identifier' ? property.key.name : property.key.value;

let value;

if (shouldParseScope && propertyName === 'scope') {
value = parseScopeObject(buildError, name, property.value);
result.locals = parseScopeObject(buildError, name, property.value);
} else {
value = parseExpression(buildError, name, property.value);
result[propertyName] = parseExpression(buildError, name, property.value);
}

result[propertyName] = value;
});

return result;
Expand Down Expand Up @@ -169,11 +165,21 @@ module.exports = function (babel) {
}
}

let precompile;

let visitor = {
Program(path, state) {
state.opts.ensureModuleApiPolyfill =
'ensureModuleApiPolyfill' in state.opts ? state.opts.ensureModuleApiPolyfill : true;

if (state.opts.templateCompilerPath) {
let templateCompiler = require(state.opts.templateCompilerPath);

precompile = templateCompiler.precompile;
} else {
precompile = state.opts.precompile;
}

if (state.opts.ensureModuleApiPolyfill) {
// Setup state for the module API polyfill
setupState(t, path, state);
Expand Down Expand Up @@ -376,7 +382,7 @@ module.exports = function (babel) {

let template = path.node.quasi.quasis.map((quasi) => quasi.value.cooked).join('');

let { precompile, isProduction } = state.opts;
let { isProduction } = state.opts;
let scope = shouldUseAutomaticScope(options) ? getScope(path.scope) : null;
let strictMode = shouldUseStrictMode(options);

Expand Down Expand Up @@ -459,7 +465,7 @@ module.exports = function (babel) {
);
}

let { precompile, isProduction } = state.opts;
let { isProduction } = state.opts;

// allow the user specified value to "win" over ours
if (!('isProduction' in compilerOptions)) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"lint": "eslint --cache .",
"test": "jest"
},
"jest": {
"testPathIgnorePatterns": ["mock-precompile"]
},
"dependencies": {
"babel-plugin-ember-modules-api-polyfill": "^3.4.0"
},
Expand Down

0 comments on commit 6f10fc6

Please sign in to comment.