Skip to content

Ts export behavior #506

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 3 commits 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ With `-a` or `--allowArbitraryExtensions`, output filenames will be compatible w

In essence, the `*.css.d.ts` extension now becomes `*.d.css.ts` so that you can import CSS modules in projects using ESM module resolution.

#### ESM style exports

With `-m` or `--esModule`, the default exports will follow the ESM style of exports/imports.

```typescript
declare const styles: {
readonly SomeComponent: string;
};
export default styles;
```

## API

```sh
Expand Down
8 changes: 7 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const yarg = yargs(hideBin(process.argv))
alias: 'watch',
},
c: {
desc: "Watch input directory's css files or pattern",
desc: 'Convert CSS class tokens to camelcase',
type: 'boolean',
alias: 'camelCase',
},
Expand All @@ -57,6 +57,11 @@ const yarg = yargs(hideBin(process.argv))
alias: 'silent',
desc: 'Silent output. Do not show "files written" messages',
},
m: {
type: 'boolean',
alias: 'esModule',
desc: 'sets target module style to ESM',
},
})
.alias('h', 'help')
.help('h')
Expand Down Expand Up @@ -92,5 +97,6 @@ async function main(): Promise<void> {
allowArbitraryExtensions: argv.a,
silent: argv.s,
listDifferent: argv.l,
esModule: argv.m,
});
}
16 changes: 16 additions & 0 deletions src/dts-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ declare const styles: {
};
export = styles;

`,
);
});
});

describe('#esModule option', () => {
it('esModule = true: returns esm style default export', async () => {
const content = await new DtsCreator({ esModule: true }).create('fixtures/testStyle.css');
assert.equal(
content.formatted,
`\
declare const styles: {
readonly "myClass": string;
};
export default styles;

`,
);
});
Expand Down
13 changes: 10 additions & 3 deletions src/dts-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface DtsContentOptions {
allowArbitraryExtensions: boolean;
camelCase: CamelCaseOption;
EOL: string;
esModule: boolean;
}

export class DtsContent {
Expand All @@ -33,6 +34,7 @@ export class DtsContent {
private camelCase: CamelCaseOption;
private resultList: string[];
private EOL: string;
private esModule: boolean;

constructor(options: DtsContentOptions) {
this.dropExtension = options.dropExtension;
Expand All @@ -45,6 +47,7 @@ export class DtsContent {
this.allowArbitraryExtensions = options.allowArbitraryExtensions;
this.camelCase = options.camelCase;
this.EOL = options.EOL;
this.esModule = options.esModule;

// when using named exports, camelCase must be enabled by default
// (see https://webpack.js.org/loaders/css-loader/#namedexport)
Expand Down Expand Up @@ -72,9 +75,13 @@ export class DtsContent {
}

return (
['declare const styles: {', ...this.resultList.map(line => ' ' + line), '};', 'export = styles;', ''].join(
this.EOL,
) + this.EOL
[
'declare const styles: {',
...this.resultList.map(line => ' ' + line),
'};',
this.esModule ? 'export default styles;' : 'export = styles;',
'',
].join(this.EOL) + this.EOL
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/dts-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface DtsCreatorOptions {
dropExtension?: boolean;
EOL?: string;
loaderPlugins?: Plugin[];
esModule?: boolean;
}

export class DtsCreator {
Expand All @@ -30,6 +31,7 @@ export class DtsCreator {
private allowArbitraryExtensions: boolean;
private dropExtension: boolean;
private EOL: string;
private esModule: boolean;

constructor(options?: DtsCreatorOptions) {
if (!options) options = {};
Expand All @@ -43,6 +45,7 @@ export class DtsCreator {
this.allowArbitraryExtensions = !!options.allowArbitraryExtensions;
this.dropExtension = !!options.dropExtension;
this.EOL = options.EOL || os.EOL;
this.esModule = !!options.esModule;
}

public async create(
Expand Down Expand Up @@ -80,6 +83,7 @@ export class DtsCreator {
allowArbitraryExtensions: this.allowArbitraryExtensions,
camelCase: this.camelCase,
EOL: this.EOL,
esModule: this.esModule,
});

return content;
Expand Down
1 change: 1 addition & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface RunOptions {
dropExtension?: boolean;
silent?: boolean;
listDifferent?: boolean;
esModule?: boolean;
}

export async function run(searchDir: string, options: RunOptions = {}): Promise<void> {
Expand Down