Skip to content

Commit 783b041

Browse files
authored
Merge pull request #1018 from typed-ember/v4-no-babel-plugins
[v4] allow ember-cli-babel to manage TS transpilation
2 parents b2520fe + 2a6236a commit 783b041

File tree

4 files changed

+24
-117
lines changed

4 files changed

+24
-117
lines changed

package.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@
3939
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -u"
4040
},
4141
"dependencies": {
42-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
43-
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
44-
"@babel/plugin-transform-typescript": "~7.7.0",
4542
"ansi-to-html": "^0.6.6",
4643
"debug": "^4.0.0",
47-
"ember-cli-babel-plugin-helpers": "^1.0.0",
4844
"execa": "^3.0.0",
4945
"fs-extra": "^8.0.0",
5046
"resolve": "^1.5.0",
@@ -122,7 +118,7 @@
122118
"in-repo-b": "link:tests/dummy/lib/in-repo-b",
123119
"loader.js": "4.7.0",
124120
"mocha": "6.2.2",
125-
"prettier": "1.18.2",
121+
"prettier": "1.19.1",
126122
"qunit-dom": "0.9.2",
127123
"testdouble": "3.12.4",
128124
"tmp": "0.1.0",
@@ -140,8 +136,7 @@
140136
"ember-addon": {
141137
"configPath": "tests/dummy/config",
142138
"before": [
143-
"broccoli-watcher",
144-
"ember-cli-babel"
139+
"broccoli-watcher"
145140
]
146141
},
147142
"husky": {

tests/unit/build-test.ts

+12-17
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,19 @@ module('Unit | Build', function() {
2828
assert.equal(fromTs, 'From test-support');
2929
});
3030

31-
test('property initialization occurs in the right order', function(assert) {
32-
class TestClass {
33-
// we shouldn't encourage folks to write code like this, but tsc ensures
34-
// that constructor param fields are set before field initializers run
35-
field = this.constructorParam;
36-
constructor(private constructorParam: string) {}
31+
test('optional chaining and nullish coalescing are transpiled correctly', function(assert) {
32+
let value = { a: 'hello' } as { a?: string; b?: string };
33+
assert.equal(value?.a, 'hello');
34+
assert.equal(value?.b, undefined);
35+
assert.equal(value?.a ?? 'ok', 'hello');
36+
assert.equal(value?.b ?? 'ok', 'ok');
37+
});
38+
39+
test('class field declarations work', function(assert) {
40+
class MyClass {
41+
declare foo: string;
3742
}
3843

39-
let instance = new TestClass('hello');
40-
assert.equal(instance.field, 'hello');
44+
assert.notOk('foo' in new MyClass());
4145
});
42-
43-
// TODO: enable once a release of Prettier comes out that supports this syntax
44-
// test('optional chaining and nullish coalescing are transpiled correctly', function(assert) {
45-
// let value = { a: 'hello' } as { a?: string; b?: string };
46-
// assert.equal(value?.a, 'hello');
47-
// assert.equal(value?.b, undefined);
48-
// assert.equal(value?.a ?? 'ok', 'hello');
49-
// assert.equal(value?.b ?? 'ok', 'ok');
50-
// });
5146
});

ts/addon.ts

+2-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import semver from 'semver';
22
import { Remote } from 'stagehand';
33
import { connect } from 'stagehand/lib/adapters/child-process';
4-
import { hasPlugin, addPlugin, AddPluginOptions } from 'ember-cli-babel-plugin-helpers';
54
import Addon from 'ember-cli/lib/models/addon';
65
import { addon } from './lib/utilities/ember-cli-entities';
76
import fork from './lib/utilities/fork';
@@ -69,28 +68,6 @@ export default addon({
6968
}
7069
},
7170

72-
setupPreprocessorRegistry(type) {
73-
if (type !== 'parent') return;
74-
75-
// Normally this is the sort of logic that would live in `included()`, but
76-
// ember-cli-babel reads the configured extensions when setting up the
77-
// preprocessor registry, so we need to beat it to the punch.
78-
this._registerBabelExtension();
79-
80-
// As of 3.7, TS supports the optional chaining and nullish coalescing proposals.
81-
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-beta/
82-
// Since we can't necessarily know what version of TS an addon was developed with,
83-
// we unconditionally add the Babel plugins for both proposals.
84-
this._addBabelPluginIfNotPresent('@babel/plugin-proposal-optional-chaining');
85-
this._addBabelPluginIfNotPresent('@babel/plugin-proposal-nullish-coalescing-operator');
86-
87-
// Needs to come after the class properties plugin (see tests/unit/build-test.ts -
88-
// "property initialization occurs in the right order")
89-
this._addBabelPluginIfNotPresent('@babel/plugin-transform-typescript', {
90-
after: ['@babel/plugin-proposal-class-properties'],
91-
});
92-
},
93-
9471
shouldIncludeChildAddon(addon) {
9572
// For testing, we have dummy in-repo addons set up, but e-c-ts doesn't depend on them;
9673
// its dummy app does. Otherwise we'd have a circular dependency.
@@ -100,6 +77,8 @@ export default addon({
10077
_checkBabelVersion() {
10178
let babel = this.parent.addons.find(addon => addon.name === 'ember-cli-babel');
10279
let version = babel && babel.pkg.version;
80+
81+
// TODO update this check and warning message once we have a Babel version to target
10382
if (!babel || !(semver.gte(version!, '7.7.3') && semver.lt(version!, '8.0.0'))) {
10483
let versionString = babel ? `version ${babel.pkg.version}` : `no instance of ember-cli-babel`;
10584
this.ui.writeWarnLine(
@@ -164,31 +143,6 @@ export default addon({
164143
}
165144
},
166145

167-
_getConfigurationTarget() {
168-
// If `this.app` isn't present, we know `this.parent` is an addon
169-
return this.app || (this.parent as Addon);
170-
},
171-
172-
_registerBabelExtension() {
173-
let target = this._getConfigurationTarget();
174-
let options: Record<string, any> = target.options || (target.options = {});
175-
let babelAddonOptions: Record<string, any> =
176-
options['ember-cli-babel'] || (options['ember-cli-babel'] = {});
177-
let extensions: string[] =
178-
babelAddonOptions.extensions || (babelAddonOptions.extensions = ['js']);
179-
180-
if (!extensions.includes('ts')) {
181-
extensions.push('ts');
182-
}
183-
},
184-
185-
_addBabelPluginIfNotPresent(pluginName: string, pluginOptions?: AddPluginOptions) {
186-
let target = this._getConfigurationTarget();
187-
if (!hasPlugin(target, pluginName)) {
188-
addPlugin(target, require.resolve(pluginName), pluginOptions);
189-
}
190-
},
191-
192146
_addTypecheckMiddleware(app: Application) {
193147
let workerPromise = this._getTypecheckWorker();
194148
let middleware = new TypecheckMiddleware(this.project, workerPromise);

yarn.lock

+8-45
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,6 @@
441441
"@babel/helper-plugin-utils" "^7.0.0"
442442
"@babel/plugin-syntax-json-strings" "^7.2.0"
443443

444-
"@babel/plugin-proposal-nullish-coalescing-operator@^7.4.4":
445-
version "7.4.4"
446-
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-nullish-coalescing-operator/-/plugin-proposal-nullish-coalescing-operator-7.4.4.tgz#41c360d59481d88e0ce3a3f837df10121a769b39"
447-
integrity sha512-Amph7Epui1Dh/xxUxS2+K22/MUi6+6JVTvy3P58tja3B6yKTSjwwx0/d83rF7551D6PVSSoplQb8GCwqec7HRw==
448-
dependencies:
449-
"@babel/helper-plugin-utils" "^7.0.0"
450-
"@babel/plugin-syntax-nullish-coalescing-operator" "^7.2.0"
451-
452444
"@babel/plugin-proposal-object-rest-spread@^7.3.4":
453445
version "7.3.4"
454446
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.3.4.tgz#47f73cf7f2a721aad5c0261205405c642e424654"
@@ -473,14 +465,6 @@
473465
"@babel/helper-plugin-utils" "^7.0.0"
474466
"@babel/plugin-syntax-optional-catch-binding" "^7.2.0"
475467

476-
"@babel/plugin-proposal-optional-chaining@^7.6.0":
477-
version "7.6.0"
478-
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.6.0.tgz#e9bf1f9b9ba10c77c033082da75f068389041af8"
479-
integrity sha512-kj4gkZ6qUggkprRq3Uh5KP8XnE1MdIO0J7MhdDX8+rAbB6dJ2UrensGIS+0NPZAaaJ1Vr0PN6oLUgXMU1uMcSg==
480-
dependencies:
481-
"@babel/helper-plugin-utils" "^7.0.0"
482-
"@babel/plugin-syntax-optional-chaining" "^7.2.0"
483-
484468
"@babel/plugin-proposal-unicode-property-regex@^7.2.0":
485469
version "7.2.0"
486470
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.2.0.tgz#abe7281fe46c95ddc143a65e5358647792039520"
@@ -526,13 +510,6 @@
526510
dependencies:
527511
"@babel/helper-plugin-utils" "^7.0.0"
528512

529-
"@babel/plugin-syntax-nullish-coalescing-operator@^7.2.0":
530-
version "7.2.0"
531-
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-nullish-coalescing-operator/-/plugin-syntax-nullish-coalescing-operator-7.2.0.tgz#f75083dfd5ade73e783db729bbd87e7b9efb7624"
532-
integrity sha512-lRCEaKE+LTxDQtgbYajI04ddt6WW0WJq57xqkAZ+s11h4YgfRHhVA/Y2VhfPzzFD4qeLHWg32DMp9HooY4Kqlg==
533-
dependencies:
534-
"@babel/helper-plugin-utils" "^7.0.0"
535-
536513
"@babel/plugin-syntax-object-rest-spread@^7.2.0":
537514
version "7.2.0"
538515
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.2.0.tgz#3b7a3e733510c57e820b9142a6579ac8b0dfad2e"
@@ -547,13 +524,6 @@
547524
dependencies:
548525
"@babel/helper-plugin-utils" "^7.0.0"
549526

550-
"@babel/plugin-syntax-optional-chaining@^7.2.0":
551-
version "7.2.0"
552-
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.2.0.tgz#a59d6ae8c167e7608eaa443fda9fa8fa6bf21dff"
553-
integrity sha512-HtGCtvp5Uq/jH/WNUPkK6b7rufnCPLLlDAFN7cmACoIjaOOiXxUt3SswU5loHqrhtqTsa/WoLQ1OQ1AGuZqaWA==
554-
dependencies:
555-
"@babel/helper-plugin-utils" "^7.0.0"
556-
557527
"@babel/plugin-syntax-top-level-await@^7.7.0":
558528
version "7.7.0"
559529
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.7.0.tgz#f5699549f50bbe8d12b1843a4e82f0a37bb65f4d"
@@ -986,15 +956,6 @@
986956
"@babel/helper-plugin-utils" "^7.0.0"
987957
"@babel/plugin-syntax-typescript" "^7.2.0"
988958

989-
"@babel/plugin-transform-typescript@~7.7.0":
990-
version "7.7.0"
991-
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.7.0.tgz#182be03fa8bd2ffd0629791a1eaa4373b7589d38"
992-
integrity sha512-y3KYbcfKe+8ziRXiGhhnGrVysDBo5+aJdB+x8sanM0K41cnmK7Q5vBlQLMbOnW/HPjLG9bg7dLgYDQZZG9T09g==
993-
dependencies:
994-
"@babel/helper-create-class-features-plugin" "^7.7.0"
995-
"@babel/helper-plugin-utils" "^7.0.0"
996-
"@babel/plugin-syntax-typescript" "^7.2.0"
997-
998959
"@babel/plugin-transform-unicode-regex@^7.2.0":
999960
version "7.2.0"
1000961
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.2.0.tgz#4eb8db16f972f8abb5062c161b8b115546ade08b"
@@ -9747,10 +9708,12 @@ imurmurhash@^0.1.4:
97479708
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
97489709

97499710
"in-repo-a@link:tests/dummy/lib/in-repo-a":
9750-
version "1.0.0"
9711+
version "0.0.0"
9712+
uid ""
97519713

97529714
"in-repo-b@link:tests/dummy/lib/in-repo-b":
9753-
version "1.0.0"
9715+
version "0.0.0"
9716+
uid ""
97549717

97559718
include-path-searcher@^0.1.0:
97569719
version "0.1.0"
@@ -13395,10 +13358,10 @@ prettier-linter-helpers@^1.0.0:
1339513358
dependencies:
1339613359
fast-diff "^1.1.2"
1339713360

13398-
prettier@1.18.2:
13399-
version "1.18.2"
13400-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
13401-
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
13361+
prettier@1.19.1:
13362+
version "1.19.1"
13363+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
13364+
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
1340213365

1340313366
pretty-hrtime@^1.0.3:
1340413367
version "1.0.3"

0 commit comments

Comments
 (0)