Skip to content

Commit f53c873

Browse files
authored
Merge pull request #111 from AFatNiBBa/exportName
Added and handled the `PluginOptions.exportName` property
2 parents 678e432 + 98f0e4d commit f53c873

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ we have confirmed that it does not work with the [sass](https://www.npmjs.com/pa
2929
| global.generate | boolean | Outputs the common style set in <b>additionalData</b> of <b>preprocessorOptions</b> as a global type definition file. |
3030
| global.outputFilePath | string | Specify the file that outputs the global common style with an absolute path.Relative paths will be supported. |
3131
| typeName.replacement | string \| (fileName: string) => string | Type name can be changed to any value. (default is the classname key as a string. e.g. `theClassName: 'theClassName';`) |
32+
| exportName.replacement | string \| (fileName: string) => string | Export name can be changed to any value. (default is 'classNames'. e.g. `declare const classNames: { ... };`) |
3233
| outputDir | string | An absolute path to the output directory. If undefined, declaration files will be generated in the source directories. `) |
3334
| sourceDir | string | An absolute path to your source code directory. The plugin will replace this path with `outputDir` option when writing declaration files. `) |
3435
| esmExport | boolean | Specify dts export type. If enabled, going to use ESM style export `export default ...`. Otherwise `export = ...`. |

src/type.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export type AdditionalData =
1010
export type PluginOptions = {
1111
enabledMode?: ('development' | 'production')[]
1212
global?: { generate: boolean; outputFilePath: string }
13-
typeName?: { replacement: string | ((fileName: string) => string) }
13+
typeName?: ContentReplacer
14+
exportName?: ContentReplacer
1415
esmExport?: boolean
1516
outputDir?: string
1617
sourceDir?: string
@@ -28,6 +29,10 @@ export type CSSJSObj = Record<
2829

2930
export type GetParseCaseFunction = ((target: string) => string) | undefined
3031

32+
export type ContentReplacer = {
33+
replacement: string | ((fileName: string) => string)
34+
}
35+
3136
export type CssUrlReplacer = (
3237
url: string,
3338
importer?: string

src/write.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import prettier from 'prettier'
44
const { format } = prettier
55

66
import { type Options } from 'prettier'
7-
import { PluginOptions } from 'type'
7+
import { ContentReplacer, PluginOptions } from 'type'
88
import { getRelativePath } from './util'
99
import path from 'path'
1010
import { mkdir } from 'node:fs/promises'
@@ -15,12 +15,15 @@ export const writeToFile = async (
1515
classNameKeys: Map<string, boolean>,
1616
options?: PluginOptions
1717
) => {
18-
const typeName = getTypeName(path.basename(fileName), options)
18+
const baseName = path.basename(fileName)
19+
const typeName = getReplacerResult(baseName, options?.typeName)
20+
const exportName =
21+
getReplacerResult(baseName, options?.exportName) ?? 'classNames'
1922
let exportTypes = ''
2023
let namedExports = ''
2124
const exportStyle = options?.esmExport
22-
? 'export default classNames;'
23-
: 'export = classNames;'
25+
? `export default ${exportName};`
26+
: `export = ${exportName};`
2427
for (const classNameKey of classNameKeys.keys()) {
2528
exportTypes = `${exportTypes}\n${formatExportType(classNameKey, typeName)}`
2629
namedExports = `${namedExports}\nexport const ${classNameKey}: '${
@@ -38,12 +41,12 @@ export const writeToFile = async (
3841
options.global.outputFilePath
3942
)
4043
outputFileString = `import globalClassNames from '${relativePath}${exportTypeFileName}'\n`
41-
outputFileString = `declare const classNames: typeof globalClassNames & {${exportTypes}\n};\n${exportStyle}`
44+
outputFileString = `declare const ${exportName}: typeof globalClassNames & {${exportTypes}\n};\n${exportStyle}`
4245
if (options?.useNamedExport) {
4346
outputFileString = `${outputFileString}\n${namedExports}\n\n`
4447
}
4548
} else {
46-
outputFileString = `declare const classNames: {${exportTypes}\n};\n${exportStyle}`
49+
outputFileString = `declare const ${exportName}: {${exportTypes}\n};\n${exportStyle}`
4750
if (options?.useNamedExport) {
4851
outputFileString = `${outputFileString}\n\n${namedExports}`
4952
}
@@ -66,12 +69,15 @@ export const writeToFile = async (
6669
})
6770
}
6871

69-
export const getTypeName = (fileName: string, options?: PluginOptions) => {
70-
if (options && options.typeName && options.typeName.replacement) {
71-
if (typeof options.typeName.replacement === 'function') {
72-
return options.typeName.replacement(fileName)
72+
export const getReplacerResult = (
73+
fileName: string,
74+
replacer?: ContentReplacer
75+
) => {
76+
if (replacer && replacer.replacement) {
77+
if (typeof replacer.replacement === 'function') {
78+
return replacer.replacement(fileName)
7379
} else {
74-
return options.typeName.replacement
80+
return replacer.replacement
7581
}
7682
}
7783

0 commit comments

Comments
 (0)