Skip to content

Commit f30a7c2

Browse files
author
Robert Jackson
authored
Support as default exports with template colocation (#336)
Support `as default` exports with template colocation
2 parents 3a52507 + f9053bc commit f30a7c2

4 files changed

Lines changed: 186 additions & 8 deletions

File tree

lib/colocated-babel-plugin.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
module.exports = function(babel) {
44
let t = babel.types;
55

6+
function makeSetComponentTemplateMemberExpression() {
7+
return t.memberExpression(t.identifier('Ember'), t.identifier('_setComponentTemplate'));
8+
}
9+
10+
function makeColocatedTemplateIdentifier() {
11+
return t.identifier('__COLOCATED_TEMPLATE__');
12+
}
13+
614
return {
715
name: 'ember-cli-htmlbars-colocation-template',
816

@@ -19,11 +27,8 @@ module.exports = function(babel) {
1927
}
2028

2129
let defaultExportDeclaration = path.node.declaration;
22-
let setComponentTemplateMemberExpression = t.memberExpression(
23-
t.identifier('Ember'),
24-
t.identifier('_setComponentTemplate')
25-
);
26-
let colocatedTemplateIdentifier = t.identifier('__COLOCATED_TEMPLATE__');
30+
let setComponentTemplateMemberExpression = makeSetComponentTemplateMemberExpression();
31+
let colocatedTemplateIdentifier = makeColocatedTemplateIdentifier();
2732

2833
if (defaultExportDeclaration.type === 'ClassDeclaration') {
2934
// when the default export is a ClassDeclaration with an `id`,
@@ -56,6 +61,24 @@ module.exports = function(babel) {
5661
]);
5762
}
5863
},
64+
65+
ExportNamedDeclaration(path, state) {
66+
if (!state.colocatedTemplateFound) {
67+
return;
68+
}
69+
70+
let defaultSpecifier = path.node.specifiers.find(spec => spec.exported.name === 'default');
71+
if (defaultSpecifier) {
72+
path.parent.body.push(
73+
t.expressionStatement(
74+
t.callExpression(makeSetComponentTemplateMemberExpression(), [
75+
makeColocatedTemplateIdentifier(),
76+
defaultSpecifier.local,
77+
])
78+
)
79+
);
80+
}
81+
},
5982
},
6083
};
6184
};

node-tests/babel-plugin-test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const babel = require('@babel/core');
5+
const { stripIndent } = require('common-tags');
6+
const ColocatedBabelPlugin = require('../lib/colocated-babel-plugin');
7+
8+
describe('ColocatedBabelPlugin', function() {
9+
this.slow(500);
10+
11+
it('sets the template for non-class default exports', function() {
12+
let { code } = babel.transformSync(
13+
stripIndent`
14+
import MyComponent from 'other-module';
15+
const __COLOCATED_TEMPLATE__ = 'ok';
16+
export default MyComponent;
17+
`,
18+
{ plugins: [ColocatedBabelPlugin] }
19+
);
20+
21+
assert.strictEqual(
22+
code,
23+
stripIndent`
24+
import MyComponent from 'other-module';
25+
const __COLOCATED_TEMPLATE__ = 'ok';
26+
export default Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, MyComponent);
27+
`
28+
);
29+
});
30+
31+
it('sets the template for named class default exports', function() {
32+
let { code } = babel.transformSync(
33+
stripIndent`
34+
import Component from 'somewhere';
35+
const __COLOCATED_TEMPLATE__ = 'ok';
36+
export default class MyComponent extends Component {}
37+
`,
38+
{ plugins: [ColocatedBabelPlugin] }
39+
);
40+
41+
assert.strictEqual(
42+
code,
43+
stripIndent`
44+
import Component from 'somewhere';
45+
const __COLOCATED_TEMPLATE__ = 'ok';
46+
export default class MyComponent extends Component {}
47+
48+
Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, MyComponent);
49+
`
50+
);
51+
});
52+
53+
it('sets the template for anonymous class default exports', function() {
54+
let { code } = babel.transformSync(
55+
stripIndent`
56+
import Component from 'somewhere';
57+
const __COLOCATED_TEMPLATE__ = 'ok';
58+
export default class extends Component {}
59+
`,
60+
{ plugins: [ColocatedBabelPlugin] }
61+
);
62+
63+
assert.strictEqual(
64+
code,
65+
stripIndent`
66+
import Component from 'somewhere';
67+
const __COLOCATED_TEMPLATE__ = 'ok';
68+
export default Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, class extends Component {});
69+
`
70+
);
71+
});
72+
73+
it('sets the template for identifier `as default` exports', function() {
74+
let { code } = babel.transformSync(
75+
stripIndent`
76+
import Component from 'somewhere';
77+
const __COLOCATED_TEMPLATE__ = 'ok';
78+
const MyComponent = class extends Component {};
79+
export { MyComponent as default };
80+
`,
81+
{ plugins: [ColocatedBabelPlugin] }
82+
);
83+
84+
assert.strictEqual(
85+
code,
86+
stripIndent`
87+
import Component from 'somewhere';
88+
const __COLOCATED_TEMPLATE__ = 'ok';
89+
const MyComponent = class extends Component {};
90+
export { MyComponent as default };
91+
92+
Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, MyComponent);
93+
`
94+
);
95+
});
96+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"walk-sync": "^2.0.2"
4949
},
5050
"devDependencies": {
51+
"@babel/core": "^7.6.4",
5152
"@ember/optional-features": "^1.0.0",
5253
"babel-plugin-debug-macros": "^0.3.3",
5354
"broccoli-merge-trees": "^3.0.2",

yarn.lock

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@
2929
semver "^5.4.1"
3030
source-map "^0.5.0"
3131

32+
"@babel/core@^7.6.4":
33+
version "7.6.4"
34+
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.6.4.tgz#6ebd9fe00925f6c3e177bb726a188b5f578088ff"
35+
integrity sha512-Rm0HGw101GY8FTzpWSyRbki/jzq+/PkNQJ+nSulrdY6gFGOsNseCqD6KHRYe2E+EdzuBdr2pxCp6s4Uk6eJ+XQ==
36+
dependencies:
37+
"@babel/code-frame" "^7.5.5"
38+
"@babel/generator" "^7.6.4"
39+
"@babel/helpers" "^7.6.2"
40+
"@babel/parser" "^7.6.4"
41+
"@babel/template" "^7.6.0"
42+
"@babel/traverse" "^7.6.3"
43+
"@babel/types" "^7.6.3"
44+
convert-source-map "^1.1.0"
45+
debug "^4.1.0"
46+
json5 "^2.1.0"
47+
lodash "^4.17.13"
48+
resolve "^1.3.2"
49+
semver "^5.4.1"
50+
source-map "^0.5.0"
51+
3252
"@babel/generator@^7.6.2":
3353
version "7.6.2"
3454
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.6.2.tgz#dac8a3c2df118334c2a29ff3446da1636a8f8c03"
@@ -39,6 +59,16 @@
3959
lodash "^4.17.13"
4060
source-map "^0.5.0"
4161

62+
"@babel/generator@^7.6.3", "@babel/generator@^7.6.4":
63+
version "7.6.4"
64+
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.6.4.tgz#a4f8437287bf9671b07f483b76e3bb731bc97671"
65+
integrity sha512-jsBuXkFoZxk0yWLyGI9llT9oiQ2FeTASmRFE32U+aaDTfoE92t78eroO7PTpU/OrYq38hlcDM6vbfLDaOLy+7w==
66+
dependencies:
67+
"@babel/types" "^7.6.3"
68+
jsesc "^2.5.1"
69+
lodash "^4.17.13"
70+
source-map "^0.5.0"
71+
4272
"@babel/helper-annotate-as-pure@^7.0.0":
4373
version "7.0.0"
4474
resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0.tgz#323d39dd0b50e10c7c06ca7d7638e6864d8c5c32"
@@ -229,6 +259,11 @@
229259
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.2.tgz#205e9c95e16ba3b8b96090677a67c9d6075b70a1"
230260
integrity sha512-mdFqWrSPCmikBoaBYMuBulzTIKuXVPtEISFbRRVNwMWpCms/hmE2kRq0bblUHaNRKrjRlmVbx1sDHmjmRgD2Xg==
231261

262+
"@babel/parser@^7.6.3", "@babel/parser@^7.6.4":
263+
version "7.6.4"
264+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.4.tgz#cb9b36a7482110282d5cb6dd424ec9262b473d81"
265+
integrity sha512-D8RHPW5qd0Vbyo3qb+YjO5nvUVRTXFLQ/FsDxJU2Nqz4uB5EnUN0ZQSEYpvTIbRuttig1XbHWU5oMeQwQSAA+A==
266+
232267
"@babel/plugin-proposal-async-generator-functions@^7.2.0":
233268
version "7.2.0"
234269
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.2.0.tgz#b289b306669dce4ad20b0252889a15768c9d417e"
@@ -711,6 +746,21 @@
711746
globals "^11.1.0"
712747
lodash "^4.17.13"
713748

749+
"@babel/traverse@^7.6.3":
750+
version "7.6.3"
751+
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.6.3.tgz#66d7dba146b086703c0fb10dd588b7364cec47f9"
752+
integrity sha512-unn7P4LGsijIxaAJo/wpoU11zN+2IaClkQAxcJWBNCMS6cmVh802IyLHNkAjQ0iYnRS3nnxk5O3fuXW28IMxTw==
753+
dependencies:
754+
"@babel/code-frame" "^7.5.5"
755+
"@babel/generator" "^7.6.3"
756+
"@babel/helper-function-name" "^7.1.0"
757+
"@babel/helper-split-export-declaration" "^7.4.4"
758+
"@babel/parser" "^7.6.3"
759+
"@babel/types" "^7.6.3"
760+
debug "^4.1.0"
761+
globals "^11.1.0"
762+
lodash "^4.17.13"
763+
714764
"@babel/types@^7.0.0", "@babel/types@^7.2.0", "@babel/types@^7.4.0", "@babel/types@^7.4.4", "@babel/types@^7.5.5", "@babel/types@^7.6.0":
715765
version "7.6.1"
716766
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.6.1.tgz#53abf3308add3ac2a2884d539151c57c4b3ac648"
@@ -720,6 +770,15 @@
720770
lodash "^4.17.13"
721771
to-fast-properties "^2.0.0"
722772

773+
"@babel/types@^7.6.3":
774+
version "7.6.3"
775+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.6.3.tgz#3f07d96f854f98e2fbd45c64b0cb942d11e8ba09"
776+
integrity sha512-CqbcpTxMcpuQTMhjI37ZHVgjBkysg5icREQIEZ0eG1yCNwg3oy+5AaLiOKmjsCj6nqOsa6Hf0ObjRVwokb7srA==
777+
dependencies:
778+
esutils "^2.0.2"
779+
lodash "^4.17.13"
780+
to-fast-properties "^2.0.0"
781+
723782
"@cnakazawa/watch@^1.0.3":
724783
version "1.0.3"
725784
resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.3.tgz#099139eaec7ebf07a27c1786a3ff64f39464d2ef"
@@ -6773,9 +6832,8 @@ mocha@^6.2.1:
67736832
yargs-unparser "1.6.0"
67746833

67756834
"module-name-inliner@link:./tests/dummy/lib/module-name-inliner":
6776-
version "0.1.0"
6777-
dependencies:
6778-
ember-cli-version-checker "*"
6835+
version "0.0.0"
6836+
uid ""
67796837

67806838
morgan@^1.9.1:
67816839
version "1.9.1"

0 commit comments

Comments
 (0)