Skip to content

Commit 8051f56

Browse files
author
Chris Garrett
committed
Update babel-plugin-htmlbars-inline-precompile
Update to the latest version of `babel-plugin-htmlbars-inline-precompile` and add `precompileTemplate` to the default options. Also expose a private setting for using custom modules (for `ember-template-imports`).
1 parent 8c46757 commit 8051f56

File tree

7 files changed

+135
-20
lines changed

7 files changed

+135
-20
lines changed

ember-cli-build.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ module.exports = function (defaults) {
4040
],
4141
],
4242
},
43+
44+
'ember-cli-htmlbars': {
45+
// This is an option intended to be used only be `ember-template-imports`.
46+
// DO NOT USE THIS
47+
_customInlineModules: {
48+
'ember-template-imports': {
49+
export: 'hbs',
50+
useTemplateLiteralProposalSemantics: 1,
51+
},
52+
53+
'TEMPLATE-TAG-MODULE': {
54+
export: 'GLIMMER_TEMPLATE',
55+
debugName: '<template>',
56+
useTemplateTagProposalSemantics: 1,
57+
},
58+
},
59+
},
4360
});
4461

4562
/*

lib/ember-addon-main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ module.exports = {
169169

170170
let isProduction = process.env.EMBER_ENV === 'production';
171171

172+
// This is an option intended to be used only be `ember-template-imports`.
173+
// DO NOT USE THIS
174+
let customModules =
175+
addonOptions['ember-cli-htmlbars'] && addonOptions['ember-cli-htmlbars']._customInlineModules;
176+
172177
// add the babel-plugin-htmlbars-inline-precompile to the list of plugins
173178
// used by `ember-cli-babel` addon
174179
if (!utils.isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
@@ -184,7 +189,8 @@ module.exports = {
184189
pluginInfo,
185190
this.projectConfig(),
186191
templateCompilerPath,
187-
isProduction
192+
isProduction,
193+
customModules
188194
);
189195

190196
babelPlugins.push(htmlbarsInlinePrecompilePlugin);
@@ -198,6 +204,7 @@ module.exports = {
198204
isProduction,
199205
projectConfig: this.projectConfig(),
200206
templateCompilerPath,
207+
modules: customModules,
201208
});
202209

203210
babelPlugins.push(htmlBarsPlugin);

lib/utils.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const INLINE_PRECOMPILE_MODULES = Object.freeze({
1212
'ember-cli-htmlbars': 'hbs',
1313
'ember-cli-htmlbars-inline-precompile': 'default',
1414
'htmlbars-inline-precompile': 'default',
15+
'@ember/template-compilation': {
16+
export: 'precompileTemplate',
17+
disableTemplateLiteral: true,
18+
shouldParseScope: true,
19+
},
1520
});
1621

1722
function isInlinePrecompileBabelPluginRegistered(plugins) {
@@ -51,7 +56,8 @@ function buildParalleizedBabelPlugin(
5156
pluginInfo,
5257
projectConfig,
5358
templateCompilerPath,
54-
isProduction
59+
isProduction,
60+
customModules
5561
) {
5662
let parallelBabelInfo = {
5763
requireFile: require.resolve('./require-from-worker'),
@@ -61,7 +67,7 @@ function buildParalleizedBabelPlugin(
6167
isProduction,
6268
projectConfig,
6369
parallelConfigs: pluginInfo.parallelConfigs,
64-
modules: INLINE_PRECOMPILE_MODULES,
70+
modules: Object.assign({}, customModules, INLINE_PRECOMPILE_MODULES),
6571
},
6672
};
6773

@@ -208,7 +214,25 @@ function setup(pluginInfo, options) {
208214
ast: pluginInfo.plugins,
209215
});
210216

211-
let { precompile } = templateCompiler;
217+
let { precompile: templatePrecompile } = templateCompiler;
218+
219+
let precompile = (template, _options) => {
220+
let options = {};
221+
222+
for (let option in _options) {
223+
if (option === 'scope') {
224+
// The template compiler expects this option to be named `locals`, but
225+
// we want users to pass it in as `scope`. In the future, we should update
226+
// the template compiler to accept scope as well and remove this.
227+
options.locals = _options.scope;
228+
} else {
229+
options[option] = _options[option];
230+
}
231+
}
232+
233+
return templatePrecompile(template, options);
234+
};
235+
212236
precompile.baseDir = () => path.resolve(__dirname, '..');
213237

214238
let cacheKey;
@@ -221,7 +245,12 @@ function setup(pluginInfo, options) {
221245

222246
let plugin = [
223247
require.resolve('babel-plugin-htmlbars-inline-precompile'),
224-
{ precompile, isProduction: options.isProduction, modules: INLINE_PRECOMPILE_MODULES },
248+
{
249+
precompile,
250+
isProduction: options.isProduction,
251+
ensureModuleApiPolyfill: true,
252+
modules: Object.assign({}, options.modules, INLINE_PRECOMPILE_MODULES),
253+
},
225254
'ember-cli-htmlbars:inline-precompile',
226255
];
227256

node-tests/addon-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('ember-cli-htmlbars addon', function () {
8686

8787
expect(output.read()).to.deep.equal({
8888
'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"}});',
89+
'export default Ember.HTMLBars.template({"id":"pb4oG9l/","block":"[[[10,0],[12],[1,\\"Hello, World!\\"],[13]],[],false,[]]","moduleName":"hello.hbs","isStrictMode":false});',
9090
});
9191
})
9292
);

package.json

Lines changed: 3 additions & 2 deletions
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": "^4.2.1",
36+
"babel-plugin-htmlbars-inline-precompile": "^4.4.1",
3737
"broccoli-debug": "^0.6.5",
3838
"broccoli-persistent-filter": "^3.1.2",
3939
"broccoli-plugin": "^4.0.3",
@@ -70,12 +70,13 @@
7070
"ember-cli-dependency-checker": "^3.2.0",
7171
"ember-cli-inject-live-reload": "^2.0.2",
7272
"ember-cli-version-checker": "^5.1.1",
73+
"ember-compatibility-helpers": "^1.2.2",
7374
"ember-export-application-global": "^2.0.1",
7475
"ember-load-initializers": "^2.1.1",
7576
"ember-maybe-import-regenerator": "^0.1.6",
7677
"ember-qunit": "^4.6.0",
7778
"ember-resolver": "^8.0.0",
78-
"ember-source": "~3.22.0",
79+
"ember-source": "~3.25.0",
7980
"ember-source-channel-url": "^2.0.1",
8081
"ember-template-lint": "^2.9.1",
8182
"ember-try": "^1.4.0",

tests/integration/components/test-inline-precompile-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { render } from '@ember/test-helpers';
44
import hbsOne from 'htmlbars-inline-precompile';
55
import hbsTwo from 'ember-cli-htmlbars-inline-precompile';
66
import { hbs as hbsThree } from 'ember-cli-htmlbars';
7+
import { precompileTemplate } from '@ember/template-compilation';
8+
import { hbs } from 'ember-template-imports';
9+
import { gte } from 'ember-compatibility-helpers';
710

811
module('tests/integration/components/test-inline-precompile', function (hooks) {
912
setupRenderingTest(hooks);
@@ -26,6 +29,30 @@ module('tests/integration/components/test-inline-precompile', function (hooks) {
2629
assert.equal(this.element.textContent.trim(), 'Wheeeee');
2730
});
2831

32+
test('precompileTemplate', async function (assert) {
33+
await render(precompileTemplate(`Wheeeee`, {}));
34+
35+
assert.equal(this.element.textContent.trim(), 'Wheeeee');
36+
});
37+
38+
if (gte('3.25.0')) {
39+
test('template literal proposal works', async function (assert) {
40+
// eslint-disable-next-line no-undef
41+
const Bar = [GLIMMER_TEMPLATE('world')];
42+
43+
const Foo = hbs`Hello`;
44+
45+
await render(
46+
precompileTemplate(`<Foo/>, <Bar/>!`, {
47+
strictMode: true,
48+
scope: { Foo, Bar },
49+
})
50+
);
51+
52+
assert.equal(this.element.textContent.trim(), 'Hello, world!');
53+
});
54+
}
55+
2956
test('inline templates have "legacy" AST plugins ran', async function (assert) {
3057
await render(hbsThree('{{module-name-reverser}}', { moduleName: 'hello-template.hbs' }));
3158

yarn.lock

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,13 @@
10751075
"@glimmer/interfaces" "^0.54.1"
10761076
"@simple-dom/interface" "^1.4.0"
10771077

1078+
"@glimmer/[email protected]":
1079+
version "0.74.2"
1080+
resolved "https://registry.yarnpkg.com/@glimmer/vm-babel-plugins/-/vm-babel-plugins-0.74.2.tgz#479774d5885201538245f4cba76cabd4b0dee3a5"
1081+
integrity sha512-DLIKzim7Fg3o54EPgz/wsEiLOUcg24P4WJ9AAv4xsXfBmFndkEDzq1oJ3SPhB167Vp1m+GKOlfKzFm9mxX5rdA==
1082+
dependencies:
1083+
babel-plugin-debug-macros "^0.3.4"
1084+
10781085
10791086
version "2.2.5"
10801087
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
@@ -2018,7 +2025,7 @@ babel-plugin-check-es2015-constants@^6.22.0:
20182025
dependencies:
20192026
babel-runtime "^6.22.0"
20202027

2021-
babel-plugin-debug-macros@^0.2.0-beta.6:
2028+
babel-plugin-debug-macros@^0.2.0, babel-plugin-debug-macros@^0.2.0-beta.6:
20222029
version "0.2.0"
20232030
resolved "https://registry.yarnpkg.com/babel-plugin-debug-macros/-/babel-plugin-debug-macros-0.2.0.tgz#0120ac20ce06ccc57bf493b667cf24b85c28da7a"
20242031
integrity sha512-Wpmw4TbhR3Eq2t3W51eBAQSdKlr+uAyF0GI4GtPfMCD12Y4cIdpKC9l0RjNTH/P9isFypSqqewMPm7//fnZlNA==
@@ -2032,6 +2039,13 @@ babel-plugin-debug-macros@^0.3.3:
20322039
dependencies:
20332040
semver "^5.3.0"
20342041

2042+
babel-plugin-debug-macros@^0.3.4:
2043+
version "0.3.4"
2044+
resolved "https://registry.yarnpkg.com/babel-plugin-debug-macros/-/babel-plugin-debug-macros-0.3.4.tgz#22961d0cb851a80654cece807a8b4b73d85c6075"
2045+
integrity sha512-wfel/vb3pXfwIDZUrkoDrn5FHmlWI96PCJ3UCDv2a86poJ3EQrnArNW5KfHSVJ9IOgxHbo748cQt7sDU+0KCEw==
2046+
dependencies:
2047+
semver "^5.3.0"
2048+
20352049
babel-plugin-dynamic-import-node@^2.3.3:
20362050
version "2.3.3"
20372051
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz#84fda19c976ec5c6defef57f9427b3def66e17a3"
@@ -2060,6 +2074,13 @@ babel-plugin-ember-modules-api-polyfill@^3.2.1:
20602074
dependencies:
20612075
ember-rfc176-data "^0.3.16"
20622076

2077+
babel-plugin-ember-modules-api-polyfill@^3.4.0:
2078+
version "3.4.0"
2079+
resolved "https://registry.yarnpkg.com/babel-plugin-ember-modules-api-polyfill/-/babel-plugin-ember-modules-api-polyfill-3.4.0.tgz#3f5e0457e135f8a29b3a8b6910806bb5b524649e"
2080+
integrity sha512-nVu/LqbZBAup1zLij6xGvQwVLWVk4XYu2fl4vIOUR3S6ukdonMLhKAb0d4QXSzH30Pd7OczVTlPffWbiwahdJw==
2081+
dependencies:
2082+
ember-rfc176-data "^0.3.16"
2083+
20632084
babel-plugin-filter-imports@^4.0.0:
20642085
version "4.0.0"
20652086
resolved "https://registry.yarnpkg.com/babel-plugin-filter-imports/-/babel-plugin-filter-imports-4.0.0.tgz#068f8da15236a96a9602c36dc6f4a6eeca70a4f4"
@@ -2073,10 +2094,12 @@ babel-plugin-htmlbars-inline-precompile@^1.0.0:
20732094
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-1.0.0.tgz#a9d2f6eaad8a3f3d361602de593a8cbef8179c22"
20742095
integrity sha512-4jvKEHR1bAX03hBDZ94IXsYCj3bwk9vYsn6ux6JZNL2U5pvzCWjqyrGahfsGNrhERyxw8IqcirOi9Q6WCo3dkQ==
20752096

2076-
babel-plugin-htmlbars-inline-precompile@^4.2.1:
2077-
version "4.2.1"
2078-
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-4.2.1.tgz#9a367f8d7ecb9fb2c2e886edfe285caf7cb9766d"
2079-
integrity sha512-MCJXk+1R0YjlF/F52eDbhJTpsnqRVYsPYVP9d0jEu7E46AcRPEWDL5tfSweiQWHLKG017BIedATb91KcIoT3zA==
2097+
babel-plugin-htmlbars-inline-precompile@^4.4.1:
2098+
version "4.4.1"
2099+
resolved "https://registry.yarnpkg.com/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-4.4.1.tgz#2ec5282cdc92bd5b5ad918a8ddc4cd0a261b5187"
2100+
integrity sha512-OCdeSw08LkMhRWE4n35BZX/lt2bKdDHUfZlqjSd0sJN0zkm0KG6JyWMlqnq8h52pNiKkgYvq/gUqONkM0JqcmQ==
2101+
dependencies:
2102+
babel-plugin-ember-modules-api-polyfill "^3.4.0"
20802103

20812104
babel-plugin-module-resolver@^3.1.1:
20822105
version "3.2.0"
@@ -4153,7 +4176,7 @@ ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.12.0, ember-cli-babel@^6.16.0,
41534176
ember-cli-version-checker "^2.1.2"
41544177
semver "^5.5.0"
41554178

4156-
ember-cli-babel@^7.1.2, ember-cli-babel@^7.11.0, ember-cli-babel@^7.12.0, ember-cli-babel@^7.18.0, ember-cli-babel@^7.19.0, ember-cli-babel@^7.23.1, ember-cli-babel@^7.7.3:
4179+
ember-cli-babel@^7.1.2, ember-cli-babel@^7.11.0, ember-cli-babel@^7.12.0, ember-cli-babel@^7.18.0, ember-cli-babel@^7.23.0, ember-cli-babel@^7.23.1, ember-cli-babel@^7.7.3:
41574180
version "7.23.1"
41584181
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-7.23.1.tgz#d1517228ede08a5d4b045c78a7429728e956b30b"
41594182
integrity sha512-qYggmt3hRs6QJ6cRkww3ahMpyP8IEV2KFrIRO/Z6hu9MkE/8Y28Xd5NjQl6fPV3oLoG0vwuHzhNe3Jr7Wec8zw==
@@ -4413,6 +4436,15 @@ ember-cli@~3.21.2:
44134436
workerpool "^6.0.0"
44144437
yam "^1.0.0"
44154438

4439+
ember-compatibility-helpers@^1.2.2:
4440+
version "1.2.2"
4441+
resolved "https://registry.yarnpkg.com/ember-compatibility-helpers/-/ember-compatibility-helpers-1.2.2.tgz#839e0c24190b7a2ec8c39b80e030811b1a95b6d3"
4442+
integrity sha512-EKyCGOGBvKkBsk6wKfg3GhjTvTTkcEwzl/cv4VYvZM18cihmjGNpliR4BymWsKRWrv4VJLyq15Vhk3NHkSNBag==
4443+
dependencies:
4444+
babel-plugin-debug-macros "^0.2.0"
4445+
ember-cli-version-checker "^5.1.1"
4446+
semver "^5.4.1"
4447+
44164448
ember-export-application-global@^2.0.1:
44174449
version "2.0.1"
44184450
resolved "https://registry.yarnpkg.com/ember-export-application-global/-/ember-export-application-global-2.0.1.tgz#b120a70e322ab208defc9e2daebe8d0dfc2dcd46"
@@ -4494,23 +4526,24 @@ ember-source-channel-url@^2.0.1:
44944526
dependencies:
44954527
got "^8.0.1"
44964528

4497-
ember-source@~3.22.0:
4498-
version "3.22.0"
4499-
resolved "https://registry.yarnpkg.com/ember-source/-/ember-source-3.22.0.tgz#aa09db2cc8e4f78de4bf9a12ce9ff499d416adc2"
4500-
integrity sha512-6F/fWA5et4AMFXm+siCIhpM2XrO8Emwqln71qK67JyUhvD3MJJtvwtBoKq7bzK9I/86LLw13JYm4o6T3d2gXBw==
4529+
ember-source@~3.25.0:
4530+
version "3.25.1"
4531+
resolved "https://registry.yarnpkg.com/ember-source/-/ember-source-3.25.1.tgz#7621fe7d471d08045b95c79fc760c3ca44efce4f"
4532+
integrity sha512-WCQV3FqbXRkYAwrwLZ6QcHZcTjT9ESa9H8Il+5H0QmDxLPiFnaj/UW4YLgZZ64X9PBT9WCUzLeLcccIFoFFm7w==
45014533
dependencies:
45024534
"@babel/helper-module-imports" "^7.8.3"
45034535
"@babel/plugin-transform-block-scoping" "^7.8.3"
45044536
"@babel/plugin-transform-object-assign" "^7.8.3"
45054537
"@ember/edition-utils" "^1.2.0"
4538+
"@glimmer/vm-babel-plugins" "0.74.2"
45064539
babel-plugin-debug-macros "^0.3.3"
45074540
babel-plugin-filter-imports "^4.0.0"
45084541
broccoli-concat "^4.2.4"
45094542
broccoli-debug "^0.6.4"
45104543
broccoli-funnel "^2.0.2"
45114544
broccoli-merge-trees "^4.2.0"
45124545
chalk "^4.0.0"
4513-
ember-cli-babel "^7.19.0"
4546+
ember-cli-babel "^7.23.0"
45144547
ember-cli-get-component-path-option "^1.0.0"
45154548
ember-cli-is-package-missing "^1.0.0"
45164549
ember-cli-normalize-entity-name "^1.0.0"
@@ -7709,7 +7742,8 @@ mocha@^8.2.1:
77097742
yargs-unparser "2.0.0"
77107743

77117744
"module-name-inliner@link:./tests/dummy/lib/module-name-inliner":
7712-
version "0.1.0"
7745+
version "0.0.0"
7746+
uid ""
77137747

77147748
morgan@^1.10.0:
77157749
version "1.10.0"

0 commit comments

Comments
 (0)