Skip to content

Commit 7e1c8c3

Browse files
authored
Merge pull request #120 from activeguild/fix/typescript-5-compatible-file-format
新しい定義ファイルフォーマットの取り込み
2 parents b0f0b63 + a1b893e commit 7e1c8c3

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ we have confirmed that it does not work with the [sass](https://www.npmjs.com/pa
3535
| esmExport | boolean | Specify dts export type. If enabled, going to use ESM style export `export default ...`. Otherwise `export = ...`. |
3636
| prettierFilePath | string | Specify the path to the prettier configuration file. |
3737
| useNamedExport | boolean | Output also in named export.(default: false) |
38+
| legacyFileFormat | boolean | Use legacy file naming format. If `true`, generates `*.scss.d.ts` (legacy). If `false`, generates `*.d.scss.ts` (TypeScript 5 compatible). (default: `false`) |
3839

3940
## Add it to vite.config.ts
4041

@@ -129,7 +130,9 @@ Saving the scss file creates a d.ts file in the `outputDir` hierarchy.
129130

130131
> Note: if `outputDir` is not set, declaration files are output to the same directory as the source files.
131132
132-
[dist/App.scss.d.ts]
133+
> **TypeScript 5 Compatibility**: By default, declaration files are generated in the format `*.d.scss.ts` (e.g., `App.module.d.scss.ts`) which is compatible with TypeScript 5's module resolution. To use the legacy format `*.scss.d.ts`, set `legacyFileFormat: true` in the plugin options.
134+
135+
[dist/App.module.d.scss.ts]
133136

134137
```ts
135138
import globalClassNames, { ClassNames as GlobalClassNames } from './style.d'

src/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type PluginOptions = {
1818
excludePath?: string | RegExp | Array<string | RegExp>
1919
prettierFilePath?: string
2020
useNamedExport?: boolean
21+
legacyFileFormat?: boolean
2122
}
2223

2324
export type CSS = { localStyle: string; globalStyle?: string }

src/write.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,31 @@ export const formatWriteFilePath = (file: string, options?: PluginOptions) => {
103103
path = path.replace(src, dist)
104104
}
105105

106-
return formatWriteFileName(path)
106+
return formatWriteFileName(path, options?.legacyFileFormat)
107107
}
108108

109-
export const formatWriteFileName = (file: string) =>
110-
`${file}${file.endsWith('d.ts') ? '' : '.d.ts'}`
109+
export const formatWriteFileName = (file: string, legacyFormat = false) => {
110+
if (file.endsWith('d.ts')) {
111+
return file
112+
}
113+
114+
if (legacyFormat) {
115+
// Legacy format: sample.module.scss.d.ts
116+
return `${file}.d.ts`
117+
}
118+
119+
// TypeScript 5 format: sample.module.d.scss.ts
120+
// Extract the file extension (e.g., .scss, .sass, .css)
121+
const extensionMatch = file.match(/\.(scss|sass|css)$/)
122+
if (extensionMatch) {
123+
const extension = extensionMatch[1]
124+
const basePath = file.slice(0, -extension.length - 1) // Remove .scss/.sass/.css
125+
return `${basePath}.d.${extension}.ts`
126+
}
127+
128+
// Fallback for unknown extensions
129+
return `${file}.d.ts`
130+
}
111131

112132
export const formatExportTypeFileName = (file: string) =>
113133
basename(file.replace('.ts', ''))

0 commit comments

Comments
 (0)