@@ -14,7 +14,7 @@ import {
14
14
validateOptions
15
15
} from './lib/utils.js' ;
16
16
import { compact } from 'lodash-es' ;
17
- import { readFile , writeFile } from 'node:fs/promises' ;
17
+ import { readFile , rename , writeFile } from 'node:fs/promises' ;
18
18
import { patchContext } from './lib/context.js' ;
19
19
20
20
/**
@@ -39,7 +39,7 @@ export const setup = (build, _options) => {
39
39
const jsLoader = patchedBuild . initialOptions . loader ?. [ '.js' ] ?? 'js' ;
40
40
const outJsExt = patchedBuild . initialOptions . outExtension ?. [ '.js' ] ?? '.js' ;
41
41
const forceInlineImages = ! ! options . forceInlineImages ;
42
- const emitDts = ! ! options . emitDeclarationFile ;
42
+ const emitDts = options . emitDeclarationFile ;
43
43
44
44
patchedBuild . onLoad ( { filter : / .+ / , namespace : pluginCssNamespace } , ( args ) => {
45
45
const { path } = args ;
@@ -85,42 +85,52 @@ export const setup = (build, _options) => {
85
85
) ;
86
86
87
87
patchedBuild . onLoad ( { filter : modulesCssRegExp , namespace : 'file' } , async ( args ) => {
88
+ if ( ! emitDts && ! bundle && ! forceBuild ) {
89
+ return undefined ;
90
+ }
91
+
88
92
log ( '[file] on load:' , args ) ;
89
93
const { path } = args ;
90
94
const rpath = relative ( buildRoot , path ) ;
91
-
92
95
const prefix = basename ( rpath , extname ( path ) )
93
96
. replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '-' )
94
97
. replace ( / ^ \- * / , '' ) ;
95
98
const suffix = patchedBuild . context . packageVersion ?. replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ?? '' ;
96
- CSSTransformer . getInstance ( patchedBuild ) . bundle ( path , {
99
+
100
+ const buildResult = CSSTransformer . getInstance ( patchedBuild ) . bundle ( path , {
97
101
prefix,
98
102
suffix,
99
103
forceInlineImages,
100
- emitDeclarationFile : emitDts
104
+ emitDeclarationFile : ! ! emitDts
101
105
} ) ;
102
106
103
- if ( ! bundle && ! forceBuild ) {
104
- return undefined ;
105
- } else if ( ! bundle && forceBuild ) {
106
- log ( 'force build modules css:' , rpath ) ;
107
- const buildResult = CSSTransformer . getInstance ( patchedBuild ) . getCachedResult ( path ) ;
108
-
109
- if ( emitDts ) {
110
- const outdir = resolve ( buildRoot , patchedBuild . initialOptions . outdir ?? '' ) ;
111
- const outbase = patchedBuild . initialOptions . outbase ;
112
- let outDtsfile = resolve ( outdir , rpath ) + '.d.ts' ;
107
+ if ( emitDts ) {
108
+ const dtsExts = [ ] ;
109
+ if ( emitDts === '.d.css.ts' || emitDts === '.css.d.ts' ) {
110
+ dtsExts . push ( emitDts ) ;
111
+ } else {
112
+ dtsExts . push ( '.d.css.ts' , '.css.d.ts' ) ;
113
+ }
114
+ const outdir = resolve ( buildRoot , patchedBuild . initialOptions . outdir ?? '' ) ;
115
+ const outbase = patchedBuild . initialOptions . outbase ;
116
+ dtsExts . forEach ( async ( dtsExt ) => {
117
+ let outDtsfile = resolve ( outdir , rpath ) . replace ( / \. c s s $ / i, dtsExt ) ;
113
118
if ( outbase ) {
114
119
let normalized = normalize ( outbase ) ;
115
120
if ( normalized . endsWith ( sep ) ) {
116
121
normalized = compact ( normalized . split ( sep ) ) . join ( sep ) ;
117
122
}
118
- outDtsfile = resolve ( outDtsfile . replace ( normalized , '' ) ) ;
123
+ if ( normalized !== '.' ) {
124
+ outDtsfile = resolve ( outDtsfile . replace ( normalized , '' ) ) ;
125
+ }
119
126
}
127
+ log ( `emit typescript declarations file:` , patchedBuild . context . relative ( outDtsfile ) ) ;
128
+ await ensureFile ( outDtsfile , buildResult ?. dts ?? '' ) ;
129
+ } ) ;
130
+ }
120
131
121
- ensureFile ( outDtsfile , buildResult ?. dts ?? '' ) ;
122
- }
123
-
132
+ if ( ! bundle && forceBuild ) {
133
+ log ( 'force build modules css:' , rpath ) ;
124
134
if ( injectCss ) {
125
135
const anotherBuildOptions = { ...patchedBuild . initialOptions } ;
126
136
delete anotherBuildOptions . entryPoints ;
@@ -192,11 +202,10 @@ export const setup = (build, _options) => {
192
202
} ;
193
203
}
194
204
} else if ( bundle ) {
195
- const bundleResult = CSSTransformer . getInstance ( patchedBuild ) . getCachedResult ( path ) ;
196
205
return {
197
- contents : bundleResult ?. js ,
206
+ contents : buildResult ?. js ,
198
207
loader : jsLoader ,
199
- watchFiles : [ path , ...( bundleResult ?. composedFiles ?? [ ] ) ] ,
208
+ watchFiles : [ path , ...( buildResult ?. composedFiles ?? [ ] ) ] ,
200
209
resolveDir : dirname ( path ) ,
201
210
pluginData : {
202
211
originCssPath : path
@@ -214,17 +223,22 @@ export const setup = (build, _options) => {
214
223
if ( ! bundle && forceBuild ) {
215
224
/** @type {[string, Record<string, string>][] } */
216
225
const jsFiles = [ ] ;
217
- /** @type {Record<string, string> } */
226
+ /** @type {[string, string][] } */
227
+ const moduleJsFiles = [ ] ;
228
+
218
229
Object . entries ( r . metafile ?. outputs ?? { } ) . forEach ( ( [ js , meta ] ) => {
230
+ if ( meta . entryPoint && modulesCssRegExp . test ( meta . entryPoint ) ) {
231
+ moduleJsFiles . push ( [ meta . entryPoint , js ] ) ;
232
+ }
233
+
219
234
if ( meta . entryPoint && ! modulesCssRegExp . test ( meta . entryPoint ) ) {
220
235
let shouldPush = false ;
221
236
/** @type {Record<string, string> } */
222
237
const defines = { } ;
223
238
meta . imports ?. forEach ( ( imp ) => {
224
239
if ( modulesCssRegExp . test ( imp . path ) ) {
225
240
shouldPush = true ;
226
- const impExt = extname ( imp . path ) ;
227
- defines [ imp . path ] = imp . path . replace ( new RegExp ( `${ impExt } $` ) , `${ outJsExt } ` ) ;
241
+ defines [ imp . path ] = imp . path + outJsExt ;
228
242
}
229
243
} ) ;
230
244
if ( shouldPush ) {
@@ -233,8 +247,15 @@ export const setup = (build, _options) => {
233
247
}
234
248
} ) ;
235
249
236
- await Promise . all (
237
- jsFiles . map ( ( [ js , places ] ) => {
250
+ await Promise . all ( [
251
+ ...( moduleJsFiles . map ( ( [ src , dist ] ) => {
252
+ const fp = resolve ( buildRoot , dist ) ;
253
+ const filename = basename ( src ) + outJsExt ;
254
+ const finalPath = resolve ( dirname ( fp ) , filename ) ;
255
+ log ( `rename ${ dist } to ${ filename } ` ) ;
256
+ return rename ( fp , finalPath ) ;
257
+ } ) ) ,
258
+ ...jsFiles . map ( ( [ js , places ] ) => {
238
259
const fulljs = resolve ( buildRoot , js ) ;
239
260
return readFile ( fulljs , { encoding : 'utf8' } )
240
261
. then ( ( content ) => {
@@ -249,7 +270,7 @@ export const setup = (build, _options) => {
249
270
return writeFile ( fulljs , nc , { encoding : 'utf8' } ) ;
250
271
} ) ;
251
272
} )
252
- ) ;
273
+ ] ) ;
253
274
254
275
return dispose ( ) ;
255
276
}
0 commit comments