Skip to content

Commit e73a38d

Browse files
author
Robert Jackson
authored
Merge pull request #598 from ember-cli/backport-isProduction
Pass `isProduction` to Ember template compiler.
2 parents b601f81 + 290db75 commit e73a38d

File tree

6 files changed

+102
-13
lines changed

6 files changed

+102
-13
lines changed

lib/ember-addon-main.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module.exports = {
6060
// ensure that broccoli-ember-hbs-template-compiler is not processing hbs files
6161
registry.remove('template', 'broccoli-ember-hbs-template-compiler');
6262

63+
let isProduction = process.env.EMBER_ENV === 'production';
64+
6365
// when this.parent === this.project, `this.parent.name` is a function 😭
6466
let parentName = typeof this.parent.name === 'function' ? this.parent.name() : this.parent.name;
6567

@@ -73,7 +75,7 @@ module.exports = {
7375
);
7476

7577
let shouldColocateTemplates = this._addon._shouldColocateTemplates();
76-
let htmlbarsOptions = this._addon.htmlbarsOptions();
78+
let htmlbarsOptions = Object.assign({ isProduction }, this._addon.htmlbarsOptions());
7779

7880
let inputTree = debugTree(tree, '01-input');
7981

@@ -87,10 +89,15 @@ module.exports = {
8789
return debugTree(new TemplateCompiler(inputTree, htmlbarsOptions), '03-output');
8890
},
8991

90-
precompile(string, options) {
92+
precompile(string, _options) {
93+
let options = _options;
9194
let htmlbarsOptions = this._addon.htmlbarsOptions();
9295
let templateCompiler = htmlbarsOptions.templateCompiler;
9396

97+
if (isProduction) {
98+
options = Object.assign({ isProduction }, _options);
99+
}
100+
94101
return utils.template(templateCompiler, string, options);
95102
},
96103
});
@@ -145,6 +152,8 @@ module.exports = {
145152
addonOptions.babel.plugins = addonOptions.babel.plugins || [];
146153
let babelPlugins = addonOptions.babel.plugins;
147154

155+
let isProduction = process.env.EMBER_ENV === 'production';
156+
148157
// add the babel-plugin-htmlbars-inline-precompile to the list of plugins
149158
// used by `ember-cli-babel` addon
150159
if (!utils.isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
@@ -156,7 +165,8 @@ module.exports = {
156165

157166
let htmlbarsInlinePrecompilePlugin = utils.buildParalleizedBabelPlugin(
158167
pluginInfo,
159-
templateCompilerPath
168+
templateCompilerPath,
169+
isProduction
160170
);
161171

162172
babelPlugins.push(htmlbarsInlinePrecompilePlugin);
@@ -165,6 +175,7 @@ module.exports = {
165175
this.logger.debug('Prevented by these plugins: ' + pluginInfo.unparallelizableWrappers);
166176

167177
let htmlBarsPlugin = utils.setup(pluginInfo, {
178+
isProduction,
168179
projectConfig: this.projectConfig(),
169180
templateCompilerPath,
170181
});

lib/template-compiler-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TemplateCompiler extends Filter {
7171
'export default ' +
7272
utils.template(this.options.templateCompiler, stripBom(string), {
7373
contents: string,
74+
isProduction: this.options.isProduction,
7475
moduleName: relativePath,
7576
parseOptions: {
7677
srcName: srcName,

lib/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ function isColocatedBabelPluginRegistered(plugins) {
4646
);
4747
}
4848

49-
function buildParalleizedBabelPlugin(pluginInfo, templateCompilerPath) {
49+
function buildParalleizedBabelPlugin(pluginInfo, templateCompilerPath, isProduction) {
5050
let parallelBabelInfo = {
5151
requireFile: require.resolve('./require-from-worker'),
5252
buildUsing: 'build',
5353
params: {
5454
templateCompilerPath,
55+
isProduction,
5556
parallelConfigs: pluginInfo.parallelConfigs,
5657
modules: INLINE_PRECOMPILE_MODULES,
5758
},
@@ -176,6 +177,7 @@ function initializeEmberENV(templateCompiler, EmberENV) {
176177

177178
function template(templateCompiler, string, options) {
178179
let precompiled = templateCompiler.precompile(string, options);
180+
179181
return 'Ember.HTMLBars.template(' + precompiled + ')';
180182
}
181183

@@ -198,7 +200,7 @@ function setup(pluginInfo, options) {
198200

199201
let plugin = [
200202
require.resolve('babel-plugin-htmlbars-inline-precompile'),
201-
{ precompile, modules: INLINE_PRECOMPILE_MODULES },
203+
{ precompile, isProduction: options.isProduction, modules: INLINE_PRECOMPILE_MODULES },
202204
'ember-cli-htmlbars:inline-precompile',
203205
];
204206

node-tests/template_compiler_test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,82 @@ describe('TemplateCompiler', function() {
5959
})
6060
);
6161

62+
it('invokes AST plugins', async function() {
63+
let source = '{{foo-bar}}';
64+
input.write({
65+
'template.hbs': source,
66+
});
67+
let plugin = env => {
68+
return {
69+
name: 'fake-ast-plugin',
70+
71+
visitor: {
72+
MustacheStatement() {
73+
return env.syntax.builders.text('Huzzah!');
74+
},
75+
},
76+
};
77+
};
78+
79+
htmlbarsOptions.plugins = {
80+
ast: [plugin],
81+
};
82+
83+
let tree = new TemplateCompiler(input.path(), htmlbarsOptions);
84+
85+
try {
86+
output = createBuilder(tree);
87+
await output.build();
88+
} finally {
89+
tree.unregisterPlugins();
90+
}
91+
92+
let expected = `export default Ember.HTMLBars.template(${htmlbarsPrecompile(source, {
93+
moduleName: 'template.hbs',
94+
plugins: {
95+
ast: [plugin],
96+
},
97+
})});`;
98+
99+
let outputString = output.readText('template.js');
100+
assert.strictEqual(outputString, expected);
101+
assert.ok(outputString.includes('Huzzah!'));
102+
});
103+
104+
it('AST Plugins have access to `isProduction` status', async function() {
105+
let source = '{{foo-bar}}';
106+
input.write({
107+
'template.hbs': source,
108+
});
109+
110+
let wasProduction = false;
111+
let plugin = env => {
112+
wasProduction = env.isProduction;
113+
114+
return {
115+
name: 'fake-ast-plugin',
116+
117+
visitor: {},
118+
};
119+
};
120+
121+
htmlbarsOptions.isProduction = true;
122+
htmlbarsOptions.plugins = {
123+
ast: [plugin],
124+
};
125+
126+
let tree = new TemplateCompiler(input.path(), htmlbarsOptions);
127+
128+
try {
129+
output = createBuilder(tree);
130+
await output.build();
131+
} finally {
132+
tree.unregisterPlugins();
133+
}
134+
135+
assert.ok(wasProduction);
136+
});
137+
62138
it(
63139
'ignores utf-8 byte order marks',
64140
co.wrap(function*() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"dependencies": {
3535
"@ember/edition-utils": "^1.2.0",
36-
"babel-plugin-htmlbars-inline-precompile": "^3.0.1",
36+
"babel-plugin-htmlbars-inline-precompile": "^3.2.0",
3737
"broccoli-debug": "^0.6.5",
3838
"broccoli-persistent-filter": "^2.3.1",
3939
"broccoli-plugin": "^3.1.0",

yarn.lock

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,10 +1645,10 @@ babel-plugin-htmlbars-inline-precompile@^1.0.0:
16451645
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-1.0.0.tgz#a9d2f6eaad8a3f3d361602de593a8cbef8179c22"
16461646
integrity sha512-4jvKEHR1bAX03hBDZ94IXsYCj3bwk9vYsn6ux6JZNL2U5pvzCWjqyrGahfsGNrhERyxw8IqcirOi9Q6WCo3dkQ==
16471647

1648-
babel-plugin-htmlbars-inline-precompile@^3.0.1:
1649-
version "3.0.1"
1650-
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-3.0.1.tgz#e1e38a4087f446578e419a21c112530c8df02345"
1651-
integrity sha512-ZiFY0nQjtdMPGIDwp/5LYOs6rCr54QfcSV5nPbrA7C++Fv4Vb2Q/qrKYx78t+dwmARJztnOBlObFk4z8veHxNA==
1648+
babel-plugin-htmlbars-inline-precompile@^3.2.0:
1649+
version "3.2.0"
1650+
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-3.2.0.tgz#c4882ea875d0f5683f0d91c1f72e29a4f14b5606"
1651+
integrity sha512-IUeZmgs9tMUGXYu1vfke5I18yYJFldFGdNFQOWslXTnDWXzpwPih7QFduUqvT+awDpDuNtXpdt5JAf43Q1Hhzg==
16521652

16531653
babel-plugin-module-resolver@^3.1.1, babel-plugin-module-resolver@^3.2.0:
16541654
version "3.2.0"
@@ -7098,9 +7098,8 @@ mocha@^7.1.1:
70987098
yargs-unparser "1.6.0"
70997099

71007100
"module-name-inliner@link:./tests/dummy/lib/module-name-inliner":
7101-
version "0.1.0"
7102-
dependencies:
7103-
ember-cli-version-checker "*"
7101+
version "0.0.0"
7102+
uid ""
71047103

71057104
morgan@^1.9.1:
71067105
version "1.9.1"

0 commit comments

Comments
 (0)