Skip to content

Commit 134b967

Browse files
committed
Precompile TS files in addons' app trees
1 parent 8bc3be7 commit 134b967

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ The `ts:precompile` command will put compiled `.js` files in your `addon` direct
507507

508508
The `ts:clean` command will remove the generated `.js` and `.d.ts` files, leaving your working directory back in a pristine state.
509509

510+
**Note**: While `.ts` files from both the `app` and `addon` directories of your addon will be transpiled by `ts:precompile`, only the declaration files from `addon` will be published. Since the final import paths for `app` files will depend on the name of the consuming application, we can't put those declaration files in a meaningful place.
511+
510512
### Linking Addons
511513

512514
Often when developing an addon, it can be useful to run that addon in the context of some other host app so you can make sure it will integrate the way you expect, e.g. using [`yarn link`](https://yarnpkg.com/en/docs/cli/link#search) or [`npm link`](https://docs.npmjs.com/cli/link).

blueprints/ember-cli-typescript/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
let inRepoAddons = (this.project.pkg['ember-addon'] || {}).paths || [];
3232
let hasMirage = 'ember-cli-mirage' in (this.project.pkg.devDependencies || {});
3333
let isAddon = this.project.isEmberCLIAddon();
34-
let includes = [isAddon ? 'addon' : 'app', 'tests'].concat(inRepoAddons);
34+
let includes = ['app', isAddon && 'addon', 'tests'].concat(inRepoAddons).filter(Boolean);
3535

3636
// Mirage is already covered for addons because it's under `tests/`
3737
if (hasMirage && !isAddon) {
@@ -51,7 +51,7 @@ module.exports = {
5151
}
5252

5353
if (isAddon) {
54-
paths[`${appName}/*`] = ['tests/dummy/app/*'];
54+
paths[`${appName}/*`] = ['tests/dummy/app/*', 'app/*'];
5555
} else {
5656
paths[`${appName}/*`] = ['app/*'];
5757
}

lib/commands/precompile.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ const fs = require('fs');
66
const path = require('path');
77
const walkSync = require('walk-sync');
88
const mkdirp = require('mkdirp');
9-
const SilentError = require('silent-error');
109
const Command = require('ember-cli/lib/models/command'); // eslint-disable-line node/no-unpublished-require
1110
const compile = require('../utilities/compile');
12-
const debug = require('debug')('ember-cli-typescript:precompile');
1311

1412
const PRECOMPILE_MANIFEST = 'tmp/.ts-precompile-manifest';
1513

@@ -30,31 +28,32 @@ module.exports = Command.extend({
3028
return compile({ project, outDir, flags }).then(() => {
3129
let output = [];
3230
for (let declSource of walkSync(outDir, { globs: ['**/*.d.ts'] })) {
33-
if (!this._isAddonFile(declSource)) {
34-
debug('skipping non-addon file %s', declSource);
35-
continue;
36-
}
37-
38-
let declDest = declSource.replace(/^addon\//, '');
39-
let compiled = declSource.replace(/\.d\.ts$/, '.js');
31+
if (this._shouldCopy(declSource)) {
32+
let compiled = declSource.replace(/\.d\.ts$/, '.js');
33+
this._copyFile(output, `${outDir}/${compiled}`, compiled);
4034

41-
this._copyFile(output, `${outDir}/${declSource}`, declDest);
42-
this._copyFile(output, `${outDir}/${compiled}`, compiled);
35+
// We can only do anything meaningful with declarations for files in addon/
36+
if (this._isAddonFile(declSource)) {
37+
let declDest = declSource.replace(/^addon\//, '');
38+
this._copyFile(output, `${outDir}/${declSource}`, declDest);
39+
}
40+
}
4341
}
4442

4543
mkdirp.sync(path.dirname(manifestPath));
4644
fs.writeFileSync(manifestPath, JSON.stringify(output.reverse()));
4745
});
4846
},
4947

50-
_isAddonFile(source) {
51-
if (source.indexOf('app') === 0) {
52-
throw new SilentError(
53-
"Including .ts files in your addon's `app` directory is unsupported. " +
54-
'See <link to README or something>.'
55-
);
56-
}
48+
_shouldCopy(source) {
49+
return this._isAppFile(source) || this._isAddonFile(source);
50+
},
51+
52+
_isAppFile(source) {
53+
return source.indexOf('app') === 0;
54+
},
5755

56+
_isAddonFile(source) {
5857
return source.indexOf('addon') === 0;
5958
},
6059

node-tests/blueprints/ember-cli-typescript-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ describe('Acceptance: ember-cli-typescript generator', function() {
6868
const tsconfigJson = JSON.parse(tsconfig.content);
6969
expect(tsconfigJson.compilerOptions.paths).to.deep.equal({
7070
'dummy/tests/*': ['tests/*'],
71-
'dummy/*': ['tests/dummy/app/*'],
71+
'dummy/*': ['tests/dummy/app/*', 'app/*'],
7272
'my-addon': ['addon'],
7373
'my-addon/*': ['addon/*'],
7474
'*': ['types/*'],
7575
});
7676

77-
expect(tsconfigJson.include).to.deep.equal(['addon', 'tests']);
77+
expect(tsconfigJson.include).to.deep.equal(['app', 'addon', 'tests']);
7878

7979
const projectTypes = file('types/dummy/index.d.ts');
8080
expect(projectTypes).to.exist;
@@ -167,13 +167,13 @@ describe('Acceptance: ember-cli-typescript generator', function() {
167167
expect(json.compilerOptions.paths).to.deep.equal({
168168
'dummy/tests/*': ['tests/*'],
169169
'dummy/mirage/*': ['tests/dummy/mirage/*'],
170-
'dummy/*': ['tests/dummy/app/*'],
170+
'dummy/*': ['tests/dummy/app/*', 'app/*'],
171171
'my-addon': ['addon'],
172172
'my-addon/*': ['addon/*'],
173173
'*': ['types/*'],
174174
});
175175

176-
expect(json.include).to.deep.equal(['addon', 'tests']);
176+
expect(json.include).to.deep.equal(['app', 'addon', 'tests']);
177177
});
178178
});
179179
});

0 commit comments

Comments
 (0)