Skip to content

Use <basename> for default export name #95

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions src/dts-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from "path";
import isThere from "is-there";
import * as mkdirp from 'mkdirp';
import * as util from "util";
import camelcase from "camelcase";

const writeFile = util.promisify(fs.writeFile);

Expand Down Expand Up @@ -46,11 +47,12 @@ export class DtsContent {

public get formatted(): string {
if(!this.resultList || !this.resultList.length) return '';
const exportName = this.buildExportName();
return [
'declare const styles: {',
`declare const ${exportName}: {`,
...this.resultList.map(line => ' ' + line),
'};',
'export = styles;',
`export default ${exportName};`,
''
].join(os.EOL) + this.EOL;
}
Expand Down Expand Up @@ -78,6 +80,16 @@ export class DtsContent {

await writeFile(this.outputFilePath, finalOutput, 'utf8');
}

private buildExportName() {
const outputFileNameParts = removeExtension(this.rInputPath).split('/');
const outputStyleName = camelcase(
outputFileNameParts[outputFileNameParts.length - 1]
);
return `${outputStyleName.charAt(0).toUpperCase()}${outputStyleName.slice(
1
)}`;
}
}

function removeExtension(filePath: string): string {
Expand Down
16 changes: 8 additions & 8 deletions test/dts-creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ describe('DtsContent', () => {
assert.equal(
content.formatted,
`\
declare const styles: {
declare const TestStyle: {
readonly "myClass": string;
};
export = styles;
export default TestStyle;

`
);
Expand All @@ -130,10 +130,10 @@ export = styles;
assert.equal(
content.formatted,
`\
declare const styles: {
declare const Kebabed: {
readonly "myClass": string;
};
export = styles;
export default Kebabed;

`
);
Expand All @@ -148,10 +148,10 @@ export = styles;
assert.equal(
content.formatted,
`\
declare const styles: {
declare const KebabedUpperCase: {
readonly "myClass": string;
};
export = styles;
export default KebabedUpperCase;

`
);
Expand All @@ -166,10 +166,10 @@ export = styles;
assert.equal(
content.formatted,
`\
declare const styles: {
declare const KebabedUpperCase: {
readonly "MyClass": string;
};
export = styles;
export default KebabedUpperCase;

`
);
Expand Down
4 changes: 2 additions & 2 deletions test/testStyle.css.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare const styles: {
declare const TestStyle: {
readonly "myClass": string;
};
export = styles;
export default TestStyle;