Skip to content

Commit 2a3eed2

Browse files
author
Robert Jackson
authored
Ensure inline precompile and colocated templates run template AS… (#309)
Ensure inline precompile and colocated templates run template AST plugins.
2 parents 7b88419 + 2998af6 commit 2a3eed2

18 files changed

Lines changed: 430 additions & 109 deletions

File tree

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
'blueprints/*/index.js',
2525
'config/**/*.js',
2626
'tests/dummy/config/**/*.js',
27+
'tests/dummy/lib/**/*.js',
2728
],
2829
excludedFiles: ['addon/**', 'addon-test-support/**', 'app/**', 'tests/dummy/app/**'],
2930
parserOptions: {

config/ember-try.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ module.exports = function() {
101101
devDependencies: {},
102102
},
103103
},
104+
{
105+
name: 'ember-octane',
106+
ENV: {
107+
// need to add a convienient API for this to @ember/edition-utils
108+
EMBER_EDITION: 'octane',
109+
},
110+
npm: {
111+
devDependencies: {},
112+
},
113+
},
104114
{
105115
name: 'with-ember-cli-htmlbars-inline-precompile',
106116
npm: {

ember-cli-build.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
'use strict';
22

33
const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
4+
const MergeTree = require('broccoli-merge-trees');
5+
const { has } = require('@ember/edition-utils');
46

57
module.exports = function(defaults) {
8+
let hasOctane = has('octane');
9+
let appTree = 'tests/dummy/app';
10+
if (hasOctane) {
11+
appTree = new MergeTree(['tests/dummy/app', 'tests/colocation/app']);
12+
}
13+
614
let app = new EmberAddon(defaults, {
715
// Add options here
816
throwUnlessParallelizable: true,
17+
18+
trees: {
19+
app: appTree,
20+
},
21+
22+
babel: {
23+
plugins: [
24+
[
25+
require.resolve('babel-plugin-debug-macros'),
26+
{
27+
flags: [
28+
{
29+
name: '@ember/edition-fake-module',
30+
source: '@ember/edition-fake-module',
31+
flags: {
32+
HAS_OCTANE: hasOctane,
33+
},
34+
},
35+
],
36+
},
37+
'debug macros - octane flag',
38+
],
39+
],
40+
},
941
});
1042

1143
/*

lib/colocated-broccoli-plugin.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const copyFileSync = require('fs-copy-file-sync');
66
const path = require('path');
77
const walkSync = require('walk-sync');
88
const Plugin = require('broccoli-plugin');
9+
const logger = require('heimdalljs-logger')('ember-cli-htmlbars:colocated-broccoli-plugin');
910

1011
function detectRootName(files) {
1112
let [first] = files;
@@ -79,8 +80,19 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
7980
let templateContents = fs.readFileSync(inputPath, { encoding: 'utf8' });
8081
let jsContents = null;
8182

82-
// TODO: deal with hygiene?
83-
let prefix = `import { hbs } from 'ember-cli-htmlbars';\nconst __COLOCATED_TEMPLATE__ = hbs\`${templateContents}\`;\n`;
83+
let hbsInvocationOptions = {
84+
contents: templateContents,
85+
moduleName: filePath,
86+
parseOptions: {
87+
srcName: filePath,
88+
},
89+
};
90+
let hbsInvocation = `hbs(${JSON.stringify(templateContents)}, ${JSON.stringify(
91+
hbsInvocationOptions
92+
)})`;
93+
let prefix = `import { hbs } from 'ember-cli-htmlbars';\nconst __COLOCATED_TEMPLATE__ = ${hbsInvocation};\n`;
94+
95+
logger.debug(`processing colocated template: ${filePath} (template-only: ${hasJSFile})`);
8496

8597
if (hasJSFile) {
8698
// add the template, call setComponentTemplate
@@ -118,9 +130,13 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
118130
return;
119131
}
120132

133+
logger.debug(`copying unchanged file: ${filePath}`);
134+
121135
// TODO: don't speculatively mkdirSync (likely do in a try/catch with ENOENT)
122136
mkdirp.sync(path.dirname(outputPath));
123137
copyFileSync(inputPath, outputPath);
124138
});
139+
140+
logger.info(`copied over (unchanged): ${filesToCopy.length} files`);
125141
}
126142
};

lib/ember-addon-main.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ module.exports = {
3636
this._cachedShouldColocateTemplates =
3737
hasOctane && hasValidBabelVersion && hasValidEmberCLIVersion;
3838

39+
logger.info(
40+
`Colocation processing: ${this._cachedShouldColocateTemplates} (hasOctane: ${hasOctane}; hasValidEmberCLIVersion: ${hasValidEmberCLIVersion}; hasVali
41+
dBabelVersion: ${hasValidBabelVersion};`
42+
);
43+
3944
return this._cachedShouldColocateTemplates;
4045
},
4146

@@ -94,9 +99,8 @@ module.exports = {
9499
// add the babel-plugin-htmlbars-inline-precompile to the list of plugins
95100
// used by `ember-cli-babel` addon
96101
if (!this._isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
97-
let pluginWrappers = this.astPlugins();
102+
let pluginInfo = this.astPlugins();
98103
let templateCompilerPath = this.templateCompilerPath();
99-
let pluginInfo = utils.setupPlugins(pluginWrappers);
100104

101105
let modules = {
102106
'ember-cli-htmlbars': 'hbs',
@@ -137,16 +141,7 @@ module.exports = {
137141
});
138142
} else {
139143
logger.debug('NOT using parallel API with for babel inline precompilation plugin');
140-
141-
let blockingPlugins = pluginWrappers
142-
.map(wrapper => {
143-
if (wrapper.parallelBabel === undefined) {
144-
return wrapper.name;
145-
}
146-
})
147-
.filter(Boolean);
148-
149-
logger.debug('Prevented by these plugins: ' + blockingPlugins);
144+
logger.debug('Prevented by these plugins: ' + pluginInfo.unparallelizableWrappers);
150145

151146
let htmlBarsPlugin = utils.setup(pluginInfo, {
152147
projectConfig: this.projectConfig(),

lib/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ function template(templateCompiler, string, options) {
120120
}
121121

122122
function setup(pluginInfo, options) {
123-
// borrowed from ember-cli-htmlbars http://git.io/vJDrW
124123
let projectConfig = options.projectConfig || {};
125124
let templateCompilerPath = options.templateCompilerPath;
126125

@@ -129,7 +128,9 @@ function setup(pluginInfo, options) {
129128

130129
let cacheKey = makeCacheKey(templateCompilerPath, pluginInfo);
131130

132-
registerPlugins(templateCompiler, pluginInfo.plugins);
131+
registerPlugins(templateCompiler, {
132+
ast: pluginInfo.plugins,
133+
});
133134

134135
let { precompile } = templateCompiler;
135136
precompile.baseDir = () => path.resolve(__dirname, '..');
@@ -156,6 +157,7 @@ function setupPlugins(wrappers) {
156157
let plugins = [];
157158
let cacheKeys = [];
158159
let parallelConfigs = [];
160+
let unparallelizableWrappers = [];
159161
let dependencyInvalidation = false;
160162
let canParallelize = true;
161163

@@ -170,6 +172,7 @@ function setupPlugins(wrappers) {
170172
if (wrapper.parallelBabel) {
171173
parallelConfigs.push(wrapper.parallelBabel);
172174
} else {
175+
unparallelizableWrappers.push(wrapper.name);
173176
canParallelize = false;
174177
}
175178

@@ -203,6 +206,7 @@ function setupPlugins(wrappers) {
203206
cacheKeys,
204207
parallelConfigs,
205208
canParallelize,
209+
unparallelizableWrappers,
206210
hasDependencyInvalidation: !!dependencyInvalidation,
207211
};
208212
}

node-tests/colocated-plugin-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('ColocatedTemplateCompiler', function() {
4949
'foo.js':
5050
stripIndent`
5151
import { hbs } from 'ember-cli-htmlbars';
52-
const __COLOCATED_TEMPLATE__ = hbs\`{{yield}}\`;
52+
const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}});
5353
import templateOnly from '@ember/component/template-only';
5454
5555
export default templateOnly();` + '\n',
@@ -92,7 +92,7 @@ describe('ColocatedTemplateCompiler', function() {
9292
components: {
9393
'foo.js': stripIndent`
9494
import { hbs } from 'ember-cli-htmlbars';
95-
const __COLOCATED_TEMPLATE__ = hbs\`{{yield}}\`;
95+
const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}});
9696
import Component from '@glimmer/component';
9797
9898
export default class FooComponent extends Component {}
@@ -135,7 +135,7 @@ describe('ColocatedTemplateCompiler', function() {
135135
'foo.js':
136136
stripIndent`
137137
import { hbs } from 'ember-cli-htmlbars';
138-
const __COLOCATED_TEMPLATE__ = hbs\`{{yield}}\`;
138+
const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"@scope-name/addon-name-here/components/foo.hbs","parseOptions":{"srcName":"@scope-name/addon-name-here/components/foo.hbs"}});
139139
import templateOnly from '@ember/component/template-only';
140140
141141
export default templateOnly();` + '\n',
@@ -182,7 +182,7 @@ describe('ColocatedTemplateCompiler', function() {
182182
components: {
183183
'foo.js': stripIndent`
184184
import { hbs } from 'ember-cli-htmlbars';
185-
const __COLOCATED_TEMPLATE__ = hbs\`{{yield}}\`;
185+
const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"@scope-name/addon-name-here/components/foo.hbs","parseOptions":{"srcName":"@scope-name/addon-name-here/components/foo.hbs"}});
186186
import Component from '@glimmer/component';
187187
188188
export default class FooComponent extends Component {}

node-tests/utils_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('utils', function() {
4545
cacheKeys: [],
4646
parallelConfigs: [],
4747
canParallelize: true,
48+
unparallelizableWrappers: [],
4849
hasDependencyInvalidation: false,
4950
});
5051
});
@@ -74,20 +75,23 @@ describe('utils', function() {
7475
cacheKeys: ['something', 'something else'],
7576
parallelConfigs: ['something', 'something else'],
7677
canParallelize: true,
78+
unparallelizableWrappers: [],
7779
hasDependencyInvalidation: false,
7880
});
7981
});
8082

8183
it('canParallelize is false for 1+ plugins without "parallelBabel" property', function() {
8284
let pluginWrappers = [
8385
{
86+
name: 'first',
8487
plugin() {},
8588
cacheKey() {
8689
return this.parallelBabel;
8790
},
8891
parallelBabel: 'something',
8992
},
9093
{
94+
name: 'second',
9195
plugin() {},
9296
cacheKey() {
9397
return 'something else';
@@ -102,6 +106,7 @@ describe('utils', function() {
102106
cacheKeys: ['something', 'something else'],
103107
parallelConfigs: ['something'],
104108
canParallelize: false,
109+
unparallelizableWrappers: ['second'],
105110
hasDependencyInvalidation: false,
106111
});
107112
});

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@
4949
},
5050
"devDependencies": {
5151
"@ember/optional-features": "^1.0.0",
52+
"babel-plugin-debug-macros": "^0.3.3",
53+
"broccoli-merge-trees": "^3.0.2",
5254
"broccoli-test-helper": "^2.0.0",
5355
"chai": "^4.2.0",
5456
"co": "^4.6.0",
55-
"ember-cli": "~3.12.0",
57+
"ember-cli": "~3.13.0",
5658
"ember-cli-app-version": "^3.2.0",
5759
"ember-cli-babel": "^7.11.1",
5860
"ember-cli-dependency-checker": "^3.2.0",
5961
"ember-cli-inject-live-reload": "^2.0.1",
6062
"ember-cli-template-lint": "^1.0.0-beta.3",
63+
"ember-cli-version-checker": "^3.1.3",
6164
"ember-export-application-global": "^2.0.0",
6265
"ember-load-initializers": "^2.1.0",
6366
"ember-maybe-import-regenerator": "^0.1.6",
@@ -75,6 +78,7 @@
7578
"fixturify": "^1.2.0",
7679
"loader.js": "^4.7.0",
7780
"mocha": "^6.2.0",
81+
"module-name-inliner": "link:./tests/dummy/lib/module-name-inliner",
7882
"prettier": "^1.18.2",
7983
"qunit-dom": "^0.9.0",
8084
"release-it": "^12.2.1",

0 commit comments

Comments
 (0)