1- import type { Plugin , OutputChunk , NormalizedOutputOptions , OutputBundle } from 'rolldown' ;
1+ import type { Plugin , OutputChunk , NormalizedOutputOptions , OutputBundle , OutputOptions } from 'rolldown' ;
22import type JavaScriptTypes from '@ast-grep/napi/lang/JavaScript' ;
33import type { Kinds } from '@ast-grep/napi/types/staticTypes' ;
4- import { Lang , parse } from '@ast-grep/napi' ;
4+ import { Lang , parse , type SgNode } from '@ast-grep/napi' ;
5+
6+ type CSSFiles = Set < string > | undefined ;
7+
8+ type NodePos = SgNode < JavaScriptTypes > | undefined ;
59
610/**
7- * Extract CSS imports from source code
11+ * @name extractCssImports
12+ * @description Extract CSS imports from source code
13+ * @example
14+ * const s = 'import "./index.css";'
15+ * const arr = extractCssImports(s) // ["./index.css"]
16+ * @param code - The source code to analyze
17+ * @returns An array of CSS import paths
818 */
919const extractCssImports = ( code : string ) : string [ ] => {
1020 const cssImports : string [ ] = [ ] ;
@@ -23,8 +33,8 @@ const extractCssImports = (code: string): string[] => {
2333} ;
2434
2535/**
26- * Inject css at the top of each generated chunk file for tsdown library builds.
27- * This plugin automatically imports CSS files that are referenced by each chunk .
36+ * @description Inject CSS files at the top of each generated chunk file for tsdown builds.
37+ * @return { Plugin } A Rolldown plugin to inject CSS imports into library chunks .
2838 */
2939export const libInjectCss = ( ) : Plugin => {
3040 // Track CSS imports per module
@@ -36,28 +46,29 @@ export const libInjectCss = (): Plugin => {
3646 name : 'tsdown:lib-inject-css' ,
3747
3848 // Set default config for better library bundling
39- outputOptions ( outputOptions ) {
49+ // Not sure if this is required
50+ outputOptions ( outputOptions : OutputOptions ) : OutputOptions {
4051 // Prevent hoisting transitive imports to avoid tree-shaking issues
4152 if ( typeof outputOptions . hoistTransitiveImports !== 'boolean' ) {
4253 return {
4354 ...outputOptions ,
4455 hoistTransitiveImports : false
4556 } ;
4657 }
58+
4759 return outputOptions ;
4860 } ,
4961
50- // Capture CSS imports before they're stripped
62+ // Capture CSS imports before they're stripped by the build
5163 transform ( code , id ) {
52- // Only process TypeScript/JavaScript files
64+ // Only process TypeScript/JavaScript files (ignore .d.ts files)
5365 if ( ! / \. ( t s x ? | j s x ? ) $ / . test ( id ) ) {
5466 return null ;
5567 }
5668
5769 const cssImports = extractCssImports ( code ) ;
5870
5971 if ( cssImports . length > 0 ) {
60- console . log ( `BOSH: Found ${ cssImports . length } CSS imports in ${ id } :` , cssImports ) ;
6172 cssImportMap . set ( id , cssImports ) ;
6273 }
6374
@@ -109,7 +120,7 @@ export const libInjectCss = (): Plugin => {
109120 continue ;
110121 }
111122
112- const cssFiles = chunkCssMap . get ( outputChunk . fileName ) ;
123+ const cssFiles : CSSFiles = chunkCssMap . get ( outputChunk . fileName ) ;
113124
114125 if ( ! cssFiles || cssFiles . size === 0 ) {
115126 continue ;
@@ -118,7 +129,7 @@ export const libInjectCss = (): Plugin => {
118129 const excludeTokens : Kinds < JavaScriptTypes > [ ] = [ 'import_statement' , 'expression_statement' ] ;
119130
120131 // Find the position to inject CSS imports
121- const node = parse < JavaScriptTypes > ( Lang . JavaScript , outputChunk . code )
132+ const node : NodePos = parse < JavaScriptTypes > ( Lang . JavaScript , outputChunk . code )
122133 . root ( )
123134 . children ( )
124135 . find ( ( node ) => ! excludeTokens . includes ( node . kind ( ) ) ) ;
0 commit comments