Skip to content

Commit

Permalink
Move template compiler creation to a method on the addon (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein authored Jun 25, 2020
1 parent 95087db commit 3f7c5fd
Show file tree
Hide file tree
Showing 4 changed files with 514 additions and 23 deletions.
11 changes: 8 additions & 3 deletions lib/ember-addon-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ module.exports = {
return this._cachedShouldColocateTemplates;
},

// This method is monkey patched by CSS Blocks,
// Please coordinate with @chriseppstein if you need to change it.
transpileTree(inputTree, htmlbarsOptions) {
const TemplateCompiler = require('./template-compiler-plugin');
return new TemplateCompiler(inputTree, htmlbarsOptions);
},

setupPreprocessorRegistry(type, registry) {
// ensure that broccoli-ember-hbs-template-compiler is not processing hbs files
registry.remove('template', 'broccoli-ember-hbs-template-compiler');
Expand All @@ -83,10 +90,8 @@ module.exports = {

inputTree = debugTree(new ColocatedTemplateProcessor(inputTree), '02-colocated-output');
}

this._addon.logger.debug(`setup *.hbs compiler with ${htmlbarsOptions.pluginNames}`);
const TemplateCompiler = require('./template-compiler-plugin');
return debugTree(new TemplateCompiler(inputTree, htmlbarsOptions), '03-output');
return debugTree(this._addon.transpileTree(inputTree, htmlbarsOptions), '03-output');
},

precompile(string, options) {
Expand Down
94 changes: 94 additions & 0 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

const co = require('co');
const expect = require('chai').expect;
const MockUI = require('console-ui/mock');
const CoreObject = require('core-object');
const AddonMixin = require('../lib/ember-addon-main');
const BroccoliTestHelper = require('broccoli-test-helper');
const createBuilder = BroccoliTestHelper.createBuilder;
const createTempDir = BroccoliTestHelper.createTempDir;

let Addon = CoreObject.extend(AddonMixin);

describe('ember-cli-htmlbars addon', function () {
const ORIGINAL_EMBER_ENV = process.env.EMBER_ENV;

beforeEach(function () {
this.ui = new MockUI();
let project = {
isEmberCLIProject: () => true,
_addonsInitialized: true,
root: __dirname,
emberCLIVersion: () => '2.16.2',
dependencies() {
return {};
},
addons: [],
targets: {
browsers: ['ie 11'],
},
};

this.addon = new Addon({
project,
parent: project,
ui: this.ui,
});

project.addons.push(this.addon);
});

afterEach(function () {
if (ORIGINAL_EMBER_ENV === undefined) {
delete process.env.EMBER_ENV;
} else {
process.env.EMBER_ENV = ORIGINAL_EMBER_ENV;
}
});

describe('transpileTree', function () {
this.timeout(0);

let input;
let output;
let subject;

beforeEach(
co.wrap(function* () {
input = yield createTempDir();
})
);

afterEach(
co.wrap(function* () {
yield input.dispose();
yield output.dispose();
})
);

it(
'should build',
co.wrap(function* () {
input.write({
'hello.hbs': `<div>Hello, World!</div>`,
});

let htmlbarsOptions = {
isHTMLBars: true,
templateCompiler: require('ember-source/dist/ember-template-compiler.js'),
};

subject = this.addon.transpileTree(input.path(), htmlbarsOptions);
output = createBuilder(subject);

yield output.build();

expect(output.read()).to.deep.equal({
'hello.js':
'export default Ember.HTMLBars.template({"id":"QumOHSmG","block":"{\\"symbols\\":[],\\"statements\\":[[10,\\"div\\"],[12],[2,\\"Hello, World!\\"],[13]],\\"hasEval\\":false,\\"upvars\\":[]}","meta":{"moduleName":"hello.hbs"}});',
});
})
);
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@ember/edition-utils": "^1.2.0",
"babel-plugin-htmlbars-inline-precompile": "^4.1.0",
"broccoli-debug": "^0.6.5",
"broccoli-persistent-filter": "^3.0.0",
"broccoli-persistent-filter": "^3.1.0",
"broccoli-plugin": "^4.0.3",
"common-tags": "^1.8.0",
"ember-cli-babel-plugin-helpers": "^1.1.0",
Expand All @@ -62,6 +62,8 @@
"broccoli-test-helper": "^2.0.0",
"chai": "^4.2.0",
"co": "^4.6.0",
"console-ui": "^2.2.2",
"core-object": "^3.1.5",
"ember-cli": "~3.18.0",
"ember-cli-app-version": "^3.2.0",
"ember-cli-babel": "^7.20.4",
Expand Down
Loading

0 comments on commit 3f7c5fd

Please sign in to comment.