Skip to content

Commit 3f7c5fd

Browse files
Move template compiler creation to a method on the addon (#527)
1 parent 95087db commit 3f7c5fd

File tree

4 files changed

+514
-23
lines changed

4 files changed

+514
-23
lines changed

lib/ember-addon-main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ module.exports = {
5757
return this._cachedShouldColocateTemplates;
5858
},
5959

60+
// This method is monkey patched by CSS Blocks,
61+
// Please coordinate with @chriseppstein if you need to change it.
62+
transpileTree(inputTree, htmlbarsOptions) {
63+
const TemplateCompiler = require('./template-compiler-plugin');
64+
return new TemplateCompiler(inputTree, htmlbarsOptions);
65+
},
66+
6067
setupPreprocessorRegistry(type, registry) {
6168
// ensure that broccoli-ember-hbs-template-compiler is not processing hbs files
6269
registry.remove('template', 'broccoli-ember-hbs-template-compiler');
@@ -83,10 +90,8 @@ module.exports = {
8390

8491
inputTree = debugTree(new ColocatedTemplateProcessor(inputTree), '02-colocated-output');
8592
}
86-
8793
this._addon.logger.debug(`setup *.hbs compiler with ${htmlbarsOptions.pluginNames}`);
88-
const TemplateCompiler = require('./template-compiler-plugin');
89-
return debugTree(new TemplateCompiler(inputTree, htmlbarsOptions), '03-output');
94+
return debugTree(this._addon.transpileTree(inputTree, htmlbarsOptions), '03-output');
9095
},
9196

9297
precompile(string, options) {

node-tests/addon-test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const co = require('co');
4+
const expect = require('chai').expect;
5+
const MockUI = require('console-ui/mock');
6+
const CoreObject = require('core-object');
7+
const AddonMixin = require('../lib/ember-addon-main');
8+
const BroccoliTestHelper = require('broccoli-test-helper');
9+
const createBuilder = BroccoliTestHelper.createBuilder;
10+
const createTempDir = BroccoliTestHelper.createTempDir;
11+
12+
let Addon = CoreObject.extend(AddonMixin);
13+
14+
describe('ember-cli-htmlbars addon', function () {
15+
const ORIGINAL_EMBER_ENV = process.env.EMBER_ENV;
16+
17+
beforeEach(function () {
18+
this.ui = new MockUI();
19+
let project = {
20+
isEmberCLIProject: () => true,
21+
_addonsInitialized: true,
22+
root: __dirname,
23+
emberCLIVersion: () => '2.16.2',
24+
dependencies() {
25+
return {};
26+
},
27+
addons: [],
28+
targets: {
29+
browsers: ['ie 11'],
30+
},
31+
};
32+
33+
this.addon = new Addon({
34+
project,
35+
parent: project,
36+
ui: this.ui,
37+
});
38+
39+
project.addons.push(this.addon);
40+
});
41+
42+
afterEach(function () {
43+
if (ORIGINAL_EMBER_ENV === undefined) {
44+
delete process.env.EMBER_ENV;
45+
} else {
46+
process.env.EMBER_ENV = ORIGINAL_EMBER_ENV;
47+
}
48+
});
49+
50+
describe('transpileTree', function () {
51+
this.timeout(0);
52+
53+
let input;
54+
let output;
55+
let subject;
56+
57+
beforeEach(
58+
co.wrap(function* () {
59+
input = yield createTempDir();
60+
})
61+
);
62+
63+
afterEach(
64+
co.wrap(function* () {
65+
yield input.dispose();
66+
yield output.dispose();
67+
})
68+
);
69+
70+
it(
71+
'should build',
72+
co.wrap(function* () {
73+
input.write({
74+
'hello.hbs': `<div>Hello, World!</div>`,
75+
});
76+
77+
let htmlbarsOptions = {
78+
isHTMLBars: true,
79+
templateCompiler: require('ember-source/dist/ember-template-compiler.js'),
80+
};
81+
82+
subject = this.addon.transpileTree(input.path(), htmlbarsOptions);
83+
output = createBuilder(subject);
84+
85+
yield output.build();
86+
87+
expect(output.read()).to.deep.equal({
88+
'hello.js':
89+
'export default Ember.HTMLBars.template({"id":"QumOHSmG","block":"{\\"symbols\\":[],\\"statements\\":[[10,\\"div\\"],[12],[2,\\"Hello, World!\\"],[13]],\\"hasEval\\":false,\\"upvars\\":[]}","meta":{"moduleName":"hello.hbs"}});',
90+
});
91+
})
92+
);
93+
});
94+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@ember/edition-utils": "^1.2.0",
3636
"babel-plugin-htmlbars-inline-precompile": "^4.1.0",
3737
"broccoli-debug": "^0.6.5",
38-
"broccoli-persistent-filter": "^3.0.0",
38+
"broccoli-persistent-filter": "^3.1.0",
3939
"broccoli-plugin": "^4.0.3",
4040
"common-tags": "^1.8.0",
4141
"ember-cli-babel-plugin-helpers": "^1.1.0",
@@ -62,6 +62,8 @@
6262
"broccoli-test-helper": "^2.0.0",
6363
"chai": "^4.2.0",
6464
"co": "^4.6.0",
65+
"console-ui": "^2.2.2",
66+
"core-object": "^3.1.5",
6567
"ember-cli": "~3.18.0",
6668
"ember-cli-app-version": "^3.2.0",
6769
"ember-cli-babel": "^7.20.4",

0 commit comments

Comments
 (0)