Skip to content

Set up build-time code for TypeScript development #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
/blueprints/*/files/**/*.js
# unconventional js
/blueprint-files/
/test-fixtures/
/vendor/

# compiled output
/dist/
/tmp/
/js/

# dependencies
/bower_components/
/tests/dummy/lib/*/node_modules/

# misc
/coverage/

# ember-try
/.node_modules.ember-try/
/bower.json.ember-try
/package.json.ember-try
27 changes: 22 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ module.exports = {
browser: true,
},
rules: {},
settings: {
node: {
// Honor both extensions when enforcing e.g. `node/no-missing-require`
tryExtensions: ['.js', '.ts'],
},
},
overrides: [
// node files
{
files: [
'ember-cli-build.js',
'index.js',
'register-ts-node.js',
'testem.js',
'blueprints/*/index.js',
'config/**/*.js',
'tests/dummy/config/**/*.js',
'lib/**/*.js',
'node-tests/**/*.js',
'ts/**/*.js',
],
excludedFiles: ['app/**', 'addon/**', 'tests/dummy/app/**'],
parserOptions: {
Expand All @@ -41,22 +47,33 @@ module.exports = {

// test files
{
files: ['tests/**/*.js'],
excludedFiles: ['tests/dummy/**/*.js'],
files: ['tests/**/*.{js,ts}'],
excludedFiles: ['tests/dummy/**/*.{js,ts}'],
env: {
embertest: true,
},
},

// node test files
{
files: ['node-tests/**/*.js'],
files: ['ts/tests/**/*.{js,ts}'],
env: {
mocha: true,
},
rules: {
'node/no-unpublished-require': 'off',
},
},

// all TypeScript files
{
files: ['**/*.ts'],
parser: 'typescript-eslint-parser',
rules: {
// These are covered by tsc
'no-undef': 'off',
'no-unused-var': 'off'
}
}
],
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# compiled output
/dist
/tmp
/js

# dependencies
/node_modules
Expand Down
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/dist
/tests
/tmp
/test-fixtures
/ts
/js/tests

**/.gitkeep
.bowerrc
.editorconfig
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ install:

script:
- test $MD_ONLY && echo "Skipped!" || yarn lint:js
# Typecheck and compile the addon, then nuke the TS source so we're testing the code we would publish
- test $MD_ONLY && echo "Skipped!" || yarn ci:prepare
# Usually, it's ok to finish the test scenario without reverting
# to the addon's original dependency state, skipping "cleanup".
- test $MD_ONLY && echo "Skipped!" || yarn ember try:one $EMBER_TRY_SCENARIO --skip-cleanup
Expand Down
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ cache:
test_script:
# Output useful info for debugging.
- yarn versions
# Typecheck and compile the addon, then nuke the TS source so we're testing the code we would publish
- cmd: yarn ci:prepare
- cmd: yarn ember try:one defaults

# Don't actually build.
Expand Down
Binary file removed blueprints/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion config/ember-try.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
useYarn: true,
command: 'ember test && yarn nodetest',
command: 'yarn ci:test',
scenarios: [
{
name: 'defaults',
Expand Down
108 changes: 11 additions & 97 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,15 @@
// @ts-check
'use strict';

const IncrementalTypescriptCompiler = require('./lib/incremental-typescript-compiler');
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const stew = require('broccoli-stew');
const fs = require('fs');

module.exports = {
name: 'ember-cli-typescript',
// If transpiled output is present, always default to loading that first.
// Otherwise, register ts-node if necessary and load from source.
if (fs.existsSync(`${__dirname}/js/addon.js`)) {
// eslint-disable-next-line node/no-missing-require
module.exports = require('./js/addon');
} else {
require('./register-ts-node');

included(includer) {
this._super.included.apply(this, arguments);

if (includer === this.app) {
this.compiler = new IncrementalTypescriptCompiler(this.app, this.project);
this.compiler.launch();
}
},

includedCommands() {
// If we're a direct dependency of the app, we cheat and add our instance of the blueprints
// addon to the project, as only top-level addons contribute blueprints. We need to be careful
// with the timing of when we do this, as it has to happen after addon initialization is
// complete, but before blueprint paths are resolved.
// This won't be necessary in 2.x if we shift to adding the blueprints addon as a host
// dependency on install.
if (this.project.addons.includes(this)) {
this.project.addons.push(this.addons.find(addon => addon.name === 'ember-cli-typescript-blueprints'));
}

if (this.project.isEmberCLIAddon()) {
return {
'ts:precompile': require('./lib/commands/precompile'),
'ts:clean': require('./lib/commands/clean'),
};
}
},

shouldIncludeChildAddon(addon) {
// For testing, we have dummy in-repo addons set up, but e-c-ts doesn't depend on them;
// its dummy app does. Otherwise we'd have a circular dependency.
return !['in-repo-a', 'in-repo-b', 'in-repo-c'].includes(addon.name);
},

setupPreprocessorRegistry(type, registry) {
if (type !== 'parent') {
return;
}

registry.add('js', {
name: 'ember-cli-typescript',
ext: 'ts',
toTree: (original, inputPath, outputPath) => {
if (!this.compiler || inputPath !== '/') {
return original;
}

let ts = new Funnel(this.compiler.treeForHost(), { destDir: outputPath });
return new MergeTrees([original, ts], { overwrite: true });
},
});
},

treeForApp() {
if (this.compiler) {
let tree = this.compiler.treeForApp();
return this._super.treeForApp.call(this, tree);
}
},

// We manually invoke Babel for treeForAddon and treeForAddonTestSupport
// rather than calling _super because we're returning content on behalf of addons that aren't
// ember-cli-typescript, and the _super impl would namespace all the files under our own name.
treeForAddon() {
if (this.compiler) {
let babel = this.project.addons.find(addon => addon.name === 'ember-cli-babel');
let tree = this.compiler.treeForAddons();
return babel.transpileTree(tree);
}
},

treeForTestSupport() {
let trees = [];
if (this.compiler) {
trees.push(this.compiler.treeForTests());
trees.push(this.compiler.treeForTestSupport());
}
return this._super.treeForTestSupport.call(this,
stew.mv(new MergeTrees(trees), 'test-support/*', '/')
);
},

treeForAddonTestSupport() {
if (this.compiler) {
let babel = this.project.addons.find(addon => addon.name === 'ember-cli-babel');
let tree = this.compiler.treeForAddonTestSupport();
return babel.transpileTree(tree);
}
},
};
// eslint-disable-next-line node/no-unpublished-require
module.exports = require('./ts/addon');
}
7 changes: 0 additions & 7 deletions lib/utilities/tmpdir.js

This file was deleted.

15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
"homepage": "https://github.com/typed-ember/ember-cli-typescript",
"scripts": {
"build": "ember build",
"lint:js": "eslint ./*.js addon addon-test-support app blueprints config lib server test-support tests",
"lint:js": "eslint --ext js,ts .",
"start": "ember serve",
"test": "ember test",
"nodetest": "mocha node-tests --recursive",
"test:all": "ember try:each"
"ci:prepare": "yarn prepublishOnly && rimraf ts",
"ci:test": "ember test && mocha --recursive js/tests",
"test:node": "mocha -r register-ts-node 'ts/tests/**/*.{ts,js}'",
"test:all": "ember try:each",
"prepublishOnly": "yarn tsc --project ts --noEmit false",
"postpublish": "rimraf js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This will help tremendously with:

This could lead to a pretty frustrating DX if one of us happens to have files sitting around in js/ and doesn't realize it.

},
"dependencies": {
"broccoli-funnel": "^2.0.1",
Expand Down Expand Up @@ -87,8 +91,11 @@
"loader.js": "^4.2.3",
"mktemp": "^0.4.0",
"mocha": "^5.0.0",
"rimraf": "^2.6.2",
"testdouble": "^3.5.0",
"typescript": "^2.7.2"
"ts-node": "^7.0.1",
"typescript": "^2.7.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for follow-up: given this is for us, let's update soon to use latest TS here. It fixes quite a few bugs in the type system from what I've seen, and while we're unlikely to hit most of them outside Ember code.

"typescript-eslint-parser": "^15.0.0"
},
"resolutions": {
"@types/ember": "2.8.13"
Expand Down
9 changes: 9 additions & 0 deletions register-ts-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// eslint-disable-next-line node/no-deprecated-api
if (!require.extensions['.ts']) {
// eslint-disable-next-line node/no-unpublished-require
require('ts-node').register({
project: `${__dirname}/ts/tsconfig.json`
});
}
4 changes: 2 additions & 2 deletions tests/dummy/app/controllers/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Ember from 'ember';
import Controller from '@ember/controller';

export default Ember.Controller.extend({
export default Controller.extend({
// Just a very roundabout way of using some ES6 features
value: ((test = 'Test') => `${test} ${'Value'}`)(),
foo: 'hello'
Expand Down
1 change: 0 additions & 1 deletion tests/dummy/app/helpers/typed-help.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Ember from 'ember';
import { helper } from '@ember/component/helper';

export function typedHelp(/*params, hash*/) {
Expand Down
Loading