Skip to content

Implement fast incremental compiler #85

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 46 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
62a81de
implement fast incremental compiler
dwickern Nov 24, 2017
63d5e1e
use serve-ts command instead of preprocessor
dwickern Nov 25, 2017
e43d55b
Update lockfile and ember-try config.
chriskrycho Nov 25, 2017
b8248d9
fix first-time directory creation
dwickern Nov 25, 2017
0362674
add flag when running with serve-ts
dwickern Nov 25, 2017
1db94d5
Merge in `1.0.6` (Ember upgrade) changes.
chriskrycho Dec 17, 2017
0955fc3
Exclude blueprints from build.
chriskrycho Dec 17, 2017
18774d7
Enable node type-checking with @ts-check.
chriskrycho Dec 18, 2017
39a53d3
Fix lint and type errors in typescript-preprocessor.
chriskrycho Dec 18, 2017
3da13f9
Cleanup of `.e-c-ts` dir when done with `ts-serve`.
chriskrycho Dec 18, 2017
64484df
Implement cleanup of `.e-c-ts` dir.
chriskrycho Dec 18, 2017
2a2952b
Clean up tsc process on ^C.
chriskrycho Dec 18, 2017
5acf76c
serve-ts: resolve imports relative to the root project
dwickern Dec 18, 2017
069f3e5
serve-ts: print tsc output
dwickern Dec 18, 2017
87f5054
Add `.e.c-ts` to `.gitignore` on install.
chriskrycho Dec 22, 2017
9f7da6b
Use rimraf to clean up `.e-c-ts`.
chriskrycho Dec 23, 2017
9c64109
Clean up error message on setup failure.
chriskrycho Dec 23, 2017
b03e1c8
Some metadata and a ts-ignore for the `serve-ts` command.
chriskrycho Dec 23, 2017
8339612
Fix an import (`require` not `import`).
chriskrycho Dec 23, 2017
53012fc
Add VS Code exclude (roughly right, I think?).
chriskrycho Dec 23, 2017
f4316dd
“Prettier” error output, better broccoli debugging of trees.
chriskrycho Jan 16, 2018
2feb95a
Try using cross-env for node paths.
chriskrycho Jan 16, 2018
1327866
Drop `async`/`await`.
chriskrycho Jan 16, 2018
d5147d2
Revert "Try using cross-env for node paths."
chriskrycho Jan 16, 2018
59ab4fd
Handle `types` dir by convention.
chriskrycho Jan 22, 2018
8c42b49
Include *all* directories included via `include` as well.
chriskrycho Jan 22, 2018
624b70c
Allow use of environment variable for npm link scenario.
chriskrycho Jan 22, 2018
77f13e5
Solve 'The Mystery of the Missing app.js 🕵️‍♂️'
dfreeman Jan 24, 2018
3f722d9
Merge pull request #89 from dfreeman/fast-incremental-compiler
dwickern Jan 24, 2018
da18bba
Fix cleanup
dfreeman Jan 24, 2018
54edcdc
Move incremental output to OS temp
dfreeman Jan 24, 2018
8fad31c
Minimize rebuild thrashing
dfreeman Jan 24, 2018
0e4b8b5
Fix app URL in serve output
dfreeman Jan 24, 2018
fe5035e
Meltdown aftermath: sudo required in Travis.
chriskrycho Jan 24, 2018
a666892
Merge pull request #90 from dfreeman/fast-incremental-compiler
chriskrycho Jan 24, 2018
0b4b4fe
Better approach for Meltdown mitigation Chrome handling.
chriskrycho Jan 24, 2018
59e2c77
Do not override project-level sourcemap setting.
chriskrycho Jan 24, 2018
ea8a494
Avoid double-including files
dfreeman Feb 4, 2018
b70d390
Spike out a Broccoli-integrated incremental compiler so regular ember…
dfreeman Feb 5, 2018
bdde2e9
Fix addons' `app` trees and cleanup dead code
dfreeman Feb 5, 2018
0cb4e0b
Merge pull request #92 from dfreeman/incremental-with-broccoli
chriskrycho Feb 5, 2018
5476b65
Fix the dummy app
dfreeman Feb 5, 2018
71ac9d0
Merge branch 'master' into fast-incremental-compiler
chriskrycho Feb 5, 2018
365214d
Fix rebuilds triggered by non-TS files.
dfreeman Feb 5, 2018
6f6c5d3
At least give tsc a *chance* to catch up
dfreeman Feb 5, 2018
e96c0ca
`writeErrorLine` -> `writeError`. Also: Prettier.
chriskrycho Feb 5, 2018
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
Empty file removed addon/.gitkeep
Empty file.
Empty file removed app/.gitkeep
Empty file.
8 changes: 3 additions & 5 deletions blueprints/ember-cli-typescript/files/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ inRepoAddons.forEach(function(path) { %>,
]
}
},
"exclude": [
"tmp",
"dist",
"node_modules",
"bower_components"
"include": [
"app",
"tests"
]
}
1 change: 1 addition & 0 deletions blueprints/ember-cli-typescript/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env node */

const { existsSync } = require('fs');
const path = require('path');

module.exports = {
Expand Down
59 changes: 30 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
// @ts-check
/* eslint-env node */

const fs = require('fs');
const path = require('path');
const SilentError = require('silent-error');
const TsPreprocessor = require('./lib/typescript-preprocessor');
const IncrementalTypescriptCompiler = require('./lib/incremental-typescript-compiler');

module.exports = {
name: 'ember-cli-typescript',

setupPreprocessorRegistry(type, registry) {
if (!fs.existsSync(path.join(this.project.root, 'tsconfig.json'))) {
// Do nothing; we just won't have the plugin available. This means that if you
// somehow end up in a state where it doesn't load, the preprocessor *will*
// fail, but this is necessary because the preprocessor depends on packages
// which aren't installed until the default blueprint is run
included(includer) {
this._super.included.apply(this, arguments);

this.ui.writeInfoLine(
'Skipping TypeScript preprocessing as there is no tsconfig.json. ' +
'(If this is during installation of the add-on, this is as expected. If it is ' +
'while building, serving, or testing the application, this is an error.)'
);
return;
if (includer === this.app) {
this.compiler = new IncrementalTypescriptCompiler(this.app, this.project);
this.compiler.launch();
}
},

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

try {
registry.add(
'js',
new TsPreprocessor({
ui: this.ui,
})
);
} catch (ex) {
throw new SilentError(
`Failed to instantiate TypeScript preprocessor, probably due to an invalid tsconfig.json. Please fix or run \`ember generate ember-cli-typescript\`.\n${
ex
}`
);
treeForAddon() {
if (this.compiler) {
// We manually invoke Babel here 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.
let babel = this.project.addons.find(addon => addon.name === 'ember-cli-babel');
let tree = this.compiler.treeForAddons();
return babel.transpileTree(tree);
}
},

treeForTestSupport() {
if (this.compiler) {
let tree = this.compiler.treeForTests();
return this._super.treeForTestSupport.call(this, tree);
}
}
};
242 changes: 242 additions & 0 deletions lib/incremental-typescript-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/* eslint-env node */

const execa = require('execa');
const os = require('os');
const mkdirp = require('mkdirp');
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const symlinkOrCopy = require('symlink-or-copy');
const Plugin = require('broccoli-plugin');
const RSVP = require('rsvp');
const path = require('path');
const fs = require('fs');
const resolve = require('resolve');

const debugCompiler = require('debug')('ember-cli-typescript:compiler');
const debugAutoresolve = require('debug')('ember-cli-typescript:autoresolve');

module.exports = class IncrementalTypescriptCompiler {
constructor(app, project) {
if (project._incrementalTsCompiler) {
throw new Error(
'Multiple IncrementalTypescriptCompiler instances may not be used with the same project.'
);
}

project._incrementalTsCompiler = this;

this.app = app;
this.project = project;
this.addons = this._discoverAddons(project, []);
this.maxBuildCount = 1;
this.autoresolveThreshold = 500;

this._buildDeferred = RSVP.defer();
this._isSynced = false;
this._triggerDir = `${this.outDir()}/.rebuild`;
this._pendingAutoresolve = null;
this._didAutoresolve = false;
}

treeForApp() {
// This could be more efficient, but we can hopefully assume there won't be dozens
// or hundreds of TS addons in dev mode all at once.
// Using node-merge-trees in the TypescriptOutput plugin would allow for mappings
// like { foo: 'out', bar: 'out' } so we'd only need one Broccoli node for this.
let addonAppTrees = this.addons.map(addon => {
return new TypescriptOutput(this, {
[`${this._relativeAddonRoot(addon)}/app`]: 'app',
});
});

let triggerTree = new Funnel(this._triggerDir, { destDir: 'app' });

let appTree = new TypescriptOutput(this, {
[`${this._relativeAppRoot()}/app`]: 'app',
});

let tree = new MergeTrees([...addonAppTrees, triggerTree, appTree], { overwrite: true });
return new Funnel(tree, { srcDir: 'app' });
}

treeForAddons() {
let paths = {};
for (let addon of this.addons) {
paths[`${this._relativeAddonRoot(addon)}/addon`] = addon.name;
}
return new TypescriptOutput(this, paths);
}

treeForTests() {
return new TypescriptOutput(this, {
[`${this._relativeAppRoot()}/tests`]: 'tests',
});
}

buildPromise() {
return this._buildDeferred.promise;
}

outDir() {
if (!this._outDir) {
let outDir = path.join(os.tmpdir(), `e-c-ts-${process.pid}`);
this._outDir = outDir;
mkdirp.sync(outDir);
}

return this._outDir;
}

launch() {
if (!fs.existsSync(`${this.project.root}/tsconfig.json`)) {
this.project.ui.writeWarnLine('No tsconfig.json found; skipping TypeScript compilation.');
return;
}

mkdirp.sync(this._triggerDir);
this._touchRebuildTrigger();

// argument sequence here is meaningful; don't apply prettier.
// prettier-ignore
let tsc = execa('tsc', [
'--watch',
'--outDir', this.outDir(),
'--rootDir', this.project.root,
'--allowJs', 'false',
'--noEmit', 'false',
]);

tsc.stdout.on('data', data => {
this.project.ui.writeLine(
data
.toString()
.trim()
.replace(/\u001bc/g, '')
);

if (data.indexOf('Starting incremental compilation') !== -1) {
debugCompiler('tsc detected a file change');
this.willRebuild();
clearTimeout(this._pendingAutoresolve);
}

if (data.indexOf('Compilation complete') !== -1) {
debugCompiler('rebuild completed');

this.didSync();

if (this._didAutoresolve) {
this._touchRebuildTrigger();
this.maxBuildCount++;
}

clearTimeout(this._pendingAutoresolve);
this._didAutoresolve = false;
}
});

tsc.stderr.on('data', data => {
this.project.ui.writeError(data.toString().trim());
});
}

willRebuild() {
if (this._isSynced) {
this._isSynced = false;
this._buildDeferred = RSVP.defer();

// Schedule a timer to automatically resolve if tsc doesn't pick up any file changes in a
// short period. This may happen if a non-TS file changed, or if the tsc watcher is
// drastically behind watchman. If the latter happens, we'll explicitly touch a file in the
// broccoli output in order to ensure the changes are picked up.
this._pendingAutoresolve = setTimeout(() => {
debugAutoresolve('no tsc rebuild; autoresolving...');

this.didSync();
this._didAutoresolve = true;
}, this.autoresolveThreshold);
}
}

didSync() {
this._isSynced = true;
this._buildDeferred.resolve();
}

_touchRebuildTrigger() {
debugAutoresolve('touching rebuild trigger.');
fs.writeFileSync(`${this._triggerDir}/tsc-delayed-rebuild`, '', 'utf-8');
}

_discoverAddons(node, addons) {
for (let addon of node.addons) {
let devDeps = addon.pkg.devDependencies || {};
let deps = addon.pkg.dependencies || {};
if (
('ember-cli-typescript' in deps || 'ember-cli-typescript' in devDeps) &&
addon.isDevelopingAddon()
) {
addons.push(addon);
}
this._discoverAddons(addon, addons);
}
return addons;
}

_relativeAppRoot() {
// This won't work for apps that have customized their root trees...
if (this.app instanceof this.project.require('ember-cli/lib/broccoli/ember-addon')) {
return 'tests/dummy';
} else {
return '.';
}
}

_relativeAddonRoot(addon) {
let addonRoot = addon.root;
if (addonRoot.indexOf(this.project.root) !== 0) {
let packagePath = resolve.sync(`${addon.pkg.name}/package.json`, {
basedir: this.project.root,
});
addonRoot = path.dirname(packagePath);
}

return addonRoot.replace(this.project.root, '');
}
};

class TypescriptOutput extends Plugin {
constructor(compiler, paths) {
super([]);
this.compiler = compiler;
this.paths = paths;
this.buildCount = 0;
}

build() {
this.buildCount++;

// We use this to keep track of the build state between the various
// Broccoli trees and tsc; when either tsc or broccoli notices a file
// change, we immediately invalidate the previous build output.
if (this.buildCount > this.compiler.maxBuildCount) {
debugCompiler('broccoli detected a file change');
this.compiler.maxBuildCount = this.buildCount;
this.compiler.willRebuild();
}

debugCompiler('waiting for tsc output', this.paths);
return this.compiler.buildPromise().then(() => {
debugCompiler('tsc build complete', this.paths);
for (let relativeSrc of Object.keys(this.paths)) {
let src = `${this.compiler.outDir()}/${relativeSrc}`;
let dest = `${this.outputPath}/${this.paths[relativeSrc]}`;
if (fs.existsSync(src)) {
symlinkOrCopy.sync(src, dest);
} else {
mkdirp.sync(dest);
}
}
});
}
}
63 changes: 0 additions & 63 deletions lib/typescript-preprocessor.js

This file was deleted.

Loading