Skip to content
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

Add export default option #66

Closed
wants to merge 3 commits into from
Closed
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ export const SomeComponent: string;

See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).

#### Use `export default` syntax
With `-x` or `--exportDefault`, this tool will use the [export default declaration syntax](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#11342-export-default-declarations) rather than the default [export assignment syntax](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1135-export-assignments).

If you have the following css,

```css
/* stylesWithExportDefault.css */

@value primary: red;

.myClass {
color: primary;
}
```

typed-css-modules creates the following .d.ts files from the above css:

```ts
/* stylesWithExportDefault.css.d.ts */
declare const styles: {
readonly "primary": string;
readonly "myClass": string;
};
export default styles;
```


## API

```sh
Expand Down Expand Up @@ -133,6 +160,7 @@ You can set the following options:
* `option.outDir`: Output directory(default: `option.searchDir`).
* `option.camelCase`: Camelize CSS class tokens.
* `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).
* `option.exportDefault`: Uses `export default styles` syntax fir the generated `d.ts` files.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*for


#### `create(filepath, contents) => Promise(dtsContent)`
Returns `DtsContent` instance.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --coverage",
"prepublish": "npm run build"
"prepare": "npm run build"
},
"bin": {
"tcm": "lib/cli.js"
Expand Down
4 changes: 3 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $
.alias('w', 'watch').describe('w', 'Watch input directory\'s css files or pattern').boolean('w')
.alias('d', 'dropExtension').describe('d', 'Drop the input files extension').boolean('d')
.alias('s', 'silent').describe('s', 'Silent output. Do not show "files written" or warning messages').boolean('s')
.alias('x', 'exportDefault').describe('x', 'Use "export default" syntax for .d.ts files').boolean('x')
.alias('h', 'help').help('h')
.version(() => require('../package.json').version)
let argv = yarg.argv;
Expand Down Expand Up @@ -62,7 +63,8 @@ let main = () => {
searchDir,
outDir: argv.o,
camelCase: argv.c,
dropExtension: argv.d
dropExtension: argv.d,
exportDefault: argv.x
});

if(!argv.w) {
Expand Down
10 changes: 7 additions & 3 deletions src/dtsCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class DtsContent {
rInputPath,
rawTokenList,
resultList,
EOL
EOL,
exportDefault
}) {
this.dropExtension = dropExtension;
this.rootDir = rootDir;
Expand All @@ -35,6 +36,7 @@ class DtsContent {
this.rawTokenList = rawTokenList;
this.resultList = resultList;
this.EOL = EOL;
this.exportDefault = exportDefault;
}

get contents() {
Expand All @@ -47,7 +49,7 @@ class DtsContent {
'declare const styles: {',
...this.resultList.map(line => ' ' + line),
'};',
'export = styles;',
this.exportDefault ? 'export default styles;' : 'export = styles;',
''
].join(os.EOL) + this.EOL;
}
Expand Down Expand Up @@ -94,6 +96,7 @@ export class DtsCreator {
this.camelCase = options.camelCase;
this.dropExtension = !!options.dropExtension;
this.EOL = options.EOL || os.EOL;
this.exportDefault = !!options.exportDefault;
}

create(filePath, initialContents, clearCache = false) {
Expand Down Expand Up @@ -126,7 +129,8 @@ export class DtsCreator {
rInputPath,
rawTokenList: keys,
resultList: result,
EOL: this.EOL
EOL: this.EOL,
exportDefault: this.exportDefault
});

resolve(content);
Expand Down
19 changes: 19 additions & 0 deletions test/dtsCreator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ export = styles;
});
});

describe('#exportDefault option', () => {
it('returns formatted .d.ts string', done => {
new DtsCreator({ exportDefault: true }).create('test/testStyleExportDefault.css').then(content => {
assert.equal(
content.formatted,
`\
declare const styles: {
readonly "myClass": string;
readonly "anotherClass": string;
};
export default styles;

`
);
done();
});
});
})

describe('#camelCase option', () => {
it('camelCase == true: returns camelized tokens for lowercase classes', done => {
new DtsCreator({ camelCase: true })
Expand Down
2 changes: 2 additions & 0 deletions test/testStyleExportDefault.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.myClass {color: red;}
.anotherClass {color: pink;}