Skip to content

Commit ecc515f

Browse files
authored
Merge pull request #1476 from timlindvall/module-name
fix: Use `moduleName()` to determine addon's referenced name.
2 parents 873109c + ab11567 commit ecc515f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

ts/lib/commands/precompile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ export default command({
5353
// addon name from its package name, we use the addon name, since that is
5454
// how it will be written for imports.
5555
let addon = this.project.addons.find((addon) => addon.root === this.project.root);
56-
if (addon && addon.name !== packageName) {
57-
packageName = addon.name;
56+
if (addon) {
57+
let addonName = addon.moduleName ? addon.moduleName() : addon.name;
58+
packageName = addonName;
5859
}
5960

6061
let createdFiles = copyDeclarations(pathRoots, paths, packageName, this.project.root);

ts/tests/commands/precompile-test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'fs-extra';
2+
import * as path from 'path';
23
import { hook } from 'capture-console';
34
import ember from 'ember-cli-blueprint-test-helpers/lib/helpers/ember';
45
import blueprintHelpers from 'ember-cli-blueprint-test-helpers/helpers';
@@ -109,10 +110,57 @@ describe('Acceptance: ts:precompile command', function () {
109110
pkg.name = '@foo/my-addon'; // addon `name` is `my-addon`
110111
fs.writeJSONSync('package.json', pkg);
111112

113+
// CAUTION! HACKY CODE AHEAD!
114+
// The ember blueprint helper stays in the same node process, so require
115+
// keeps any previously read files cached. We need to clear out these
116+
// caches so it picks up the changes properly.
117+
delete require.cache[path.join(process.cwd(), 'index.js')];
118+
delete require.cache[path.join(process.cwd(), 'package.json')];
119+
112120
return ember(['ts:precompile']).then(() => {
113121
const declaration = file('test-support/test-file.d.ts');
114122
expect(declaration).to.exist;
115123
expect(declaration.content.trim()).to.equal(`export declare const testString: string;`);
116124
});
117125
});
126+
127+
it('generates .d.ts files for components when addon and moduleName do not match', function () {
128+
fs.ensureDirSync('addon/components');
129+
fs.writeFileSync(
130+
'addon/components/my-component.ts',
131+
`export const testString: string = 'hello';`
132+
);
133+
fs.ensureDirSync('addon-test-support');
134+
fs.writeFileSync(
135+
'addon-test-support/test-file.ts',
136+
`export const anotherTestString: string = 'hello';`
137+
);
138+
fs.writeFileSync(
139+
'index.js',
140+
`module.exports = { name: '@foo/my-addon', moduleName() { return 'my-addon'; } };`
141+
);
142+
143+
const pkg = fs.readJSONSync('package.json');
144+
pkg.name = '@foo/my-addon'; // addon `moduleName()` is `my-addon`
145+
fs.writeJSONSync('package.json', pkg);
146+
147+
// CAUTION! HACKY CODE AHEAD!
148+
// The ember blueprint helper stays in the same node process, so require
149+
// keeps any previously read files cached. We need to clear out these
150+
// caches so it picks up the changes properly.
151+
delete require.cache[path.join(process.cwd(), 'index.js')];
152+
delete require.cache[path.join(process.cwd(), 'package.json')];
153+
154+
return ember(['ts:precompile']).then(() => {
155+
const componentDecl = file('components/my-component.d.ts');
156+
expect(componentDecl).to.exist;
157+
expect(componentDecl.content.trim()).to.equal(`export declare const testString: string;`);
158+
159+
const testSupportDecl = file('test-support/test-file.d.ts');
160+
expect(testSupportDecl).to.exist;
161+
expect(testSupportDecl.content.trim()).to.equal(
162+
`export declare const anotherTestString: string;`
163+
);
164+
});
165+
});
118166
});

ts/types/ember-cli/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare module 'ember-cli/lib/models/addon' {
4040
serverMiddleware(options: { app: Application; options?: TaskOptions }): void | Promise<void>;
4141
testemMiddleware(app: Application, options?: TaskOptions): void;
4242
setupPreprocessorRegistry(type: 'self' | 'parent', registry: PreprocessRegistry): void;
43+
moduleName(): string;
4344
}
4445
}
4546

0 commit comments

Comments
 (0)