Skip to content

Commit c49188f

Browse files
committed
v3.0.1: add ability to custom emit dts outdir
1 parent a084df6 commit c49188f

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## V3.0.1
2+
- add ability to custom ts declaration file outdir
3+
14
## V3.0.0
25
This version has some breaking changes:
36
- drop postcss-module, as a result most of postcss-module configurations are removed as well

index.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Plugin, PluginBuild } from 'esbuild';
22

3+
declare type EmitDtsConfig = Partial<Record<'.d.css.ts' | '.css.d.ts' | '*', string>>;
4+
35
declare interface BuildOptions {
46
/**
57
* force to build css modules files even if `bundle` is disabled in esbuild
@@ -16,9 +18,19 @@ declare interface BuildOptions {
1618
* - `.css.d.ts` : emit `xxx.css.d.ts`
1719
* - `.d.css.ts` : emit `xxx.d.css.ts` (from typescript@5, see https://www.typescriptlang.org/tsconfig#allowArbitraryExtensions)
1820
* - `true` : emit both `xxx.css.d.ts` and `xxx.d.css.ts`
21+
* by default the dts files would be generated in `outdir` of esbuild config, if you want to custom outdir of these dts files:
22+
* ```js
23+
* {
24+
* emitDeclarationFile: {
25+
* '*': 'custom/path/for/all',
26+
* '.css.d.ts': 'custom/path/for/*.css.d.ts',
27+
* '.d.css.ts': 'custom/path/for/*.d.css.ts'
28+
* }
29+
* }
30+
* ```
1931
* @default false
2032
*/
21-
emitDeclarationFile?: boolean | '.d.css.ts' | '.css.d.ts';
33+
emitDeclarationFile?: boolean | '.d.css.ts' | '.css.d.ts' | EmitDtsConfig;
2234
/**
2335
* set to false to not inject generated css into page;
2436
* @description
@@ -98,6 +110,8 @@ declare interface BuildOptions {
98110
declare function CssModulesPlugin(options?: BuildOptions): Plugin;
99111

100112
declare namespace CssModulesPlugin {
113+
export type EmitDts = EmitDtsConfig;
114+
101115
export interface Options extends BuildOptions {}
102116

103117
export interface BuildContext {

index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,31 @@ export const setup = (build, _options) => {
105105
});
106106

107107
if (emitDts) {
108+
/** @type {('.d.css.ts'|'.css.d.ts')[]} */
108109
const dtsExts = [];
110+
/** @type {import('./index.js').EmitDts} */
111+
let outdirs = {};
109112
if (emitDts === '.d.css.ts' || emitDts === '.css.d.ts') {
110113
dtsExts.push(emitDts);
111-
} else {
114+
} else if (emitDts === true) {
112115
dtsExts.push('.d.css.ts', '.css.d.ts');
116+
} else if (typeof emitDts === 'object') {
117+
outdirs = { ...emitDts };
118+
if (emitDts['*']) {
119+
dtsExts.push('.d.css.ts', '.css.d.ts');
120+
} else {
121+
emitDts['.css.d.ts'] && dtsExts.push('.css.d.ts');
122+
emitDts['.d.css.ts'] && dtsExts.push('.d.css.ts');
123+
}
113124
}
114125
const outdir = resolve(buildRoot, patchedBuild.initialOptions.outdir ?? '');
115126
const outbase = patchedBuild.initialOptions.outbase;
116127
dtsExts.forEach(async (dtsExt) => {
117128
let outDtsfile = resolve(outdir, rpath).replace(/\.css$/i, dtsExt);
129+
const dtsOutdir = outdirs[dtsExt] || outdirs['*'];
130+
if (dtsOutdir) {
131+
outDtsfile = resolve(buildRoot, dtsOutdir, rpath).replace(/\.css$/i, dtsExt);
132+
}
118133
if (outbase) {
119134
let normalized = normalize(outbase);
120135
if (normalized.endsWith(sep)) {
@@ -248,13 +263,13 @@ export const setup = (build, _options) => {
248263
});
249264

250265
await Promise.all([
251-
...(moduleJsFiles.map(([src, dist]) => {
266+
...moduleJsFiles.map(([src, dist]) => {
252267
const fp = resolve(buildRoot, dist);
253268
const filename = basename(src) + outJsExt;
254269
const finalPath = resolve(dirname(fp), filename);
255270
log(`rename ${dist} to ${filename}`);
256271
return rename(fp, finalPath);
257-
})),
272+
}),
258273
...jsFiles.map(([js, places]) => {
259274
const fulljs = resolve(buildRoot, js);
260275
return readFile(fulljs, { encoding: 'utf8' })

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
55
"main": "./index.cjs",
66
"module": "./index.js",

test/test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ import cssModulesPlugin from '../index.js';
108108
plugins: [
109109
cssModulesPlugin({
110110
inject: false,
111-
namedExports: true
111+
namedExports: true,
112+
emitDeclarationFile: true
112113
})
113114
],
114115
logLevel: 'debug'
@@ -133,7 +134,10 @@ import cssModulesPlugin from '../index.js';
133134
write: true,
134135
plugins: [
135136
cssModulesPlugin({
136-
emitDeclarationFile: true,
137+
emitDeclarationFile: {
138+
'.css.d.ts': './dist/no-bundle',
139+
'.d.css.ts': './generated-dts'
140+
},
137141
force: true,
138142
forceInlineImages: true,
139143
inject: '#my-styles-container'

0 commit comments

Comments
 (0)