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

feat: exportAsDefault #8

Merged
merged 2 commits into from
Feb 26, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
dest: "src/classnames.d.ts",
// Set isModule if you want to import ClassNames from another file
isModule: true,
exportAsDefault: true, // to use in combination with isModule
}),

require("@fullhuman/postcss-purgecss")({
Expand Down
30 changes: 30 additions & 0 deletions __tests__/postcss-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,34 @@ describe("files", () => {
| \\"foo\\";"
`);
});
test("can export the type file as a module and as default", async () => {
const dest = PathUtils.join(dir, "types.ts");
const collector = new ClassNameCollector({
dest,
isModule: true,
exportAsDefault: true,
});

await run(
collector,
css`
.foo {
color: blue;
}
`,
);
await collector.waitForWrite();

const content = await fs.readFile(dest);

expect(content.toString()).toMatchInlineSnapshot(`
"// This file is auto-generated with postcss-ts-classnames.

export type ClassNames =
| \\"foo\\";

export default ClassNames;"
`);
});

});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"license": "ISC",
"devDependencies": {
"@types/jest": "^24.0.21",
"@types/lodash.debounce": "^4.0.6",
"@types/node": "^12.12.5",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"sh-thunk": "^0.3.0",
"@types/lodash.debounce": "^4.0.6",
"ts-jest": "^24.1.0",
"typescript": "^3.6.4"
},
Expand Down
11 changes: 10 additions & 1 deletion src/class-name-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import debounce from "lodash.debounce";
export interface ClassNameCollectorOptions {
dest?: string;
isModule?: boolean;
exportAsDefault?: boolean;
}

export class ClassNameCollector {
classNames: Map<string, undefined | Set<string>>;
dest?: string;
isModule?: boolean;
exportAsDefault?: boolean;

waiters = [] as VoidFunction[];

constructor(options: ClassNameCollectorOptions) {
this.dest = options.dest;
this.isModule = options.isModule;
this.exportAsDefault = options.exportAsDefault;
this.classNames = new Map();
}

Expand Down Expand Up @@ -67,7 +70,13 @@ export class ClassNameCollector {
.map(n => `"${n}"`)
.join(`\n${prefix}`);

return `${comment}\n\n${this.isModule ? 'export ' : ''}type ClassNames =\n${prefix}${names};`;
if (this.isModule) {
const result = `${comment}\n\nexport type ClassNames =\n${prefix}${names};`;
return (this.exportAsDefault)
? result + '\n\nexport default ClassNames;'
: result;
}
return `${comment}\n\ntype ClassNames =\n${prefix}${names};`;
}

process(root: Root) {
Expand Down
3 changes: 3 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export function createPlugin(collector: ClassNameCollector) {
if (userOptions && userOptions.isModule) {
collector.isModule = userOptions.isModule;
}
if (userOptions && userOptions.exportAsDefault) {
collector.exportAsDefault = userOptions.exportAsDefault;
}

return root => {
collector.process(root);
Expand Down