Skip to content
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
5 changes: 3 additions & 2 deletions ts/lib/commands/precompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export default command({
// addon name from its package name, we use the addon name, since that is
// how it will be written for imports.
let addon = this.project.addons.find((addon) => addon.root === this.project.root);
if (addon && addon.name !== packageName) {
packageName = addon.name;
if (addon) {
let addonName = addon.moduleName ? addon.moduleName() : addon.name;
packageName = addonName;
}

let createdFiles = copyDeclarations(pathRoots, paths, packageName, this.project.root);
Expand Down
48 changes: 48 additions & 0 deletions ts/tests/commands/precompile-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { hook } from 'capture-console';
import ember from 'ember-cli-blueprint-test-helpers/lib/helpers/ember';
import blueprintHelpers from 'ember-cli-blueprint-test-helpers/helpers';
Expand Down Expand Up @@ -109,10 +110,57 @@ describe('Acceptance: ts:precompile command', function () {
pkg.name = '@foo/my-addon'; // addon `name` is `my-addon`
fs.writeJSONSync('package.json', pkg);

// CAUTION! HACKY CODE AHEAD!
// The ember blueprint helper stays in the same node process, so require
// keeps any previously read files cached. We need to clear out these
// caches so it picks up the changes properly.
delete require.cache[path.join(process.cwd(), 'index.js')];
delete require.cache[path.join(process.cwd(), 'package.json')];

return ember(['ts:precompile']).then(() => {
const declaration = file('test-support/test-file.d.ts');
expect(declaration).to.exist;
expect(declaration.content.trim()).to.equal(`export declare const testString: string;`);
});
});

it('generates .d.ts files for components when addon and moduleName do not match', function () {
fs.ensureDirSync('addon/components');
fs.writeFileSync(
'addon/components/my-component.ts',
`export const testString: string = 'hello';`
);
fs.ensureDirSync('addon-test-support');
fs.writeFileSync(
'addon-test-support/test-file.ts',
`export const anotherTestString: string = 'hello';`
);
fs.writeFileSync(
'index.js',
`module.exports = { name: '@foo/my-addon', moduleName() { return 'my-addon'; } };`
);

const pkg = fs.readJSONSync('package.json');
pkg.name = '@foo/my-addon'; // addon `moduleName()` is `my-addon`
fs.writeJSONSync('package.json', pkg);

// CAUTION! HACKY CODE AHEAD!
// The ember blueprint helper stays in the same node process, so require
// keeps any previously read files cached. We need to clear out these
// caches so it picks up the changes properly.
delete require.cache[path.join(process.cwd(), 'index.js')];
delete require.cache[path.join(process.cwd(), 'package.json')];

return ember(['ts:precompile']).then(() => {
const componentDecl = file('components/my-component.d.ts');
expect(componentDecl).to.exist;
expect(componentDecl.content.trim()).to.equal(`export declare const testString: string;`);

const testSupportDecl = file('test-support/test-file.d.ts');
expect(testSupportDecl).to.exist;
expect(testSupportDecl.content.trim()).to.equal(
`export declare const anotherTestString: string;`
);
});
});
});
1 change: 1 addition & 0 deletions ts/types/ember-cli/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare module 'ember-cli/lib/models/addon' {
serverMiddleware(options: { app: Application; options?: TaskOptions }): void | Promise<void>;
testemMiddleware(app: Application, options?: TaskOptions): void;
setupPreprocessorRegistry(type: 'self' | 'parent', registry: PreprocessRegistry): void;
moduleName(): string;
}
}

Expand Down