Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Filter = require('broccoli-persistent-filter');
const crypto = require('crypto');
const stringify = require('json-stable-stringify');
const stripBom = require('strip-bom');
const ExtractPragmaAstTransform = require('ember-build-utilities/dist/lib/compilers/glimmer/extract-pragma-ast-transform');

class TemplateCompiler extends Filter {
constructor(inputTree, _options) {
Expand All @@ -18,13 +19,14 @@ class TemplateCompiler extends Filter {
super(inputTree, options);

this.options = options;
this.options.plugins = this.options.plugins || {};
this.inputTree = inputTree;

this.precompile = this.options.templateCompiler.precompile;
this.registerPlugin = this.options.templateCompiler.registerPlugin;

this.registerPlugins();
this.initializeFeatures();
this.registerFeaturedPlugin('glimmer-custom-component-manager', ExtractPragmaAstTransform);
this.registerPlugins();
}

baseDir() {
Expand All @@ -43,6 +45,16 @@ class TemplateCompiler extends Filter {
}
}

registerFeaturedPlugin(featureName, plugin) {
const featureEnabled = !!this.options.templateCompiler._Ember.FEATURES[featureName];

this.options.plugins.ast = this.options.plugins.ast || [];

if (featureEnabled) {
this.options.plugins.ast.push(plugin);
}
}

initializeFeatures() {
let EmberENV = this.options.EmberENV;
let FEATURES = this.options.FEATURES;
Expand All @@ -57,11 +69,17 @@ class TemplateCompiler extends Filter {
utils.initializeEmberENV(templateCompiler, EmberENV);
}

precompile(string, options) {
return utils.precompile(this.options.templateCompiler, string, options);
}

processString(string, relativePath) {
return 'export default ' + utils.template(this.options.templateCompiler, stripBom(string), {
contents: string,
moduleName: relativePath
}) + ';';
const precompiledTemplate = this.precompile(stripBom(string), {
moduleName: relativePath,
plugins: this.options.plugins
});

return `export default Ember.HTMLBars.template(${precompiledTemplate});`;
}

_buildOptionsForHash() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"broccoli-persistent-filter": "^1.0.3",
"ember-build-utilities": "^0.1.2",
"hash-for-dep": "^1.0.2",
"json-stable-stringify": "^1.0.0",
"strip-bom": "^3.0.0"
Expand Down
49 changes: 49 additions & 0 deletions test/template_compiler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const TemplateCompiler = require('../index');
const co = require('co');

describe('TemplateCompiler', function(){
this.timeout(50000);
const sourcePath = 'test/fixtures';
let builder;

Expand Down Expand Up @@ -88,4 +89,52 @@ describe('TemplateCompiler', function(){

assert.equal(actual,expected,'They dont match!');
}));

it('does not register custom component manager ast transform if flag is disabled', function() {
var options = {
templateCompiler: require('../bower_components/ember/ember-template-compiler'),
EmberENV:{
FEATURES: { }
}
};

var templateCompiler = new TemplateCompiler(sourcePath, options);

assert.equal(templateCompiler.options.plugins.ast.length, 0, 'custom ast trasform was not registered');
});

it('registers custom component manager ast transform if flag is enabled', function() {
var options = {
templateCompiler: require('../bower_components/ember/ember-template-compiler'),
EmberENV:{
FEATURES: {
'glimmer-custom-component-manager': true
}
}
};

var templateCompiler = new TemplateCompiler(sourcePath, options);

assert.equal(templateCompiler.options.plugins.ast.length, 1, 'custom ast trasform was registered');
});

it('sets custom component manager id on a template\'s metadata if feature flag is enabled', function() {
var options = {
templateCompiler: require('../bower_components/ember/ember-template-compiler'),
EmberENV:{
FEATURES: {
'glimmer-custom-component-manager': true
}
}
};
var templateContent = '{{use-component-manager "glimmer"}}hello';

var templateCompiler = new TemplateCompiler(sourcePath, options);
var compiled = JSON.parse(templateCompiler.precompile(templateContent, {
moduleName: '',
plugins: templateCompiler.options.plugins
}));

assert.equal(compiled.meta.managerId, 'glimmer');
});
});
8 changes: 6 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ module.exports = {
}
},

precompile(templateCompiler, string, options) {
return templateCompiler.precompile(string, options);
},

template(templateCompiler, string, options) {
let precompiled = templateCompiler.precompile(string, options);
return 'Ember.HTMLBars.template(' + precompiled + ')';
let precompiled = this.precompile(string, options);
return `Ember.HTMLBars.template(${precompiled})`;
}
};
Loading