Skip to content

Commit d8a1f5c

Browse files
ocavueautofix-ci[bot]sxzzsxzz-claw
authored
fix: resolve css files in node_modules (#795)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Kevin Deng <sxzz@sxzz.moe> Co-authored-by: Rei <claw@sxzz.moe>
1 parent 0173c6e commit d8a1f5c

11 files changed

+62
-43
lines changed

packages/css/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
},
6363
"dependencies": {
6464
"lightningcss": "catalog:prod",
65-
"postcss-load-config": "catalog:prod"
65+
"postcss-load-config": "catalog:prod",
66+
"rolldown": "catalog:prod"
6667
}
6768
}

packages/css/src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import {
1010
getPreprocessorLang,
1111
isCssOrPreprocessor,
1212
} from './preprocessors.ts'
13-
import type { ResolvedConfig, Rolldown } from 'tsdown'
13+
import type { MinimalLogger } from './types.ts'
14+
import type { Plugin } from 'rolldown'
15+
import type { ResolvedConfig } from 'tsdown'
1416

15-
export function CssPlugin(config: ResolvedConfig): Rolldown.Plugin {
17+
export function CssPlugin(
18+
config: ResolvedConfig,
19+
{ logger }: { logger: MinimalLogger },
20+
): Plugin {
1621
const styles: CssStyles = new Map()
1722
const postHooks = createCssPostHooks(config, styles)
1823

@@ -30,7 +35,7 @@ export function CssPlugin(config: ResolvedConfig): Rolldown.Plugin {
3035
const deps: string[] = []
3136

3237
if (config.css.transformer === 'lightningcss') {
33-
code = await loadWithLightningCSS(id, deps, config)
38+
code = await loadWithLightningCSS(id, deps, config, logger)
3439
} else {
3540
code = await loadWithPostCSS(id, deps, config)
3641
}
@@ -59,6 +64,7 @@ async function loadWithLightningCSS(
5964
id: string,
6065
deps: string[],
6166
config: ResolvedConfig,
67+
logger: MinimalLogger,
6268
): Promise<string> {
6369
const lang = getPreprocessorLang(id)
6470

@@ -83,6 +89,7 @@ async function loadWithLightningCSS(
8389
lightningcss: config.css.lightningcss,
8490
minify: config.css.minify,
8591
preprocessorOptions: config.css.preprocessorOptions,
92+
logger,
8693
})
8794
deps.push(...bundleResult.deps)
8895
return bundleResult.code

packages/css/src/lightningcss.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { readFile } from 'node:fs/promises'
1+
import { readFileSync } from 'node:fs'
22
import path from 'node:path'
3+
import { ResolverFactory } from 'rolldown/experimental'
34
import { compilePreprocessor, getPreprocessorLang } from './preprocessors.ts'
5+
import type { MinimalLogger } from './types.ts'
46
import type { Targets } from 'lightningcss'
57
import type { LightningCSSOptions, PreprocessorOptions } from 'tsdown/css'
68

9+
let resolver: ResolverFactory | undefined
10+
function getResolver(): ResolverFactory {
11+
return (resolver ??= new ResolverFactory({
12+
conditionNames: ['style', 'default'],
13+
}))
14+
}
15+
716
const encoder = new TextEncoder()
817
const decoder = new TextDecoder()
918

@@ -18,6 +27,7 @@ export interface BundleCssOptions {
1827
lightningcss?: LightningCSSOptions
1928
minify?: boolean
2029
preprocessorOptions?: PreprocessorOptions
30+
logger: MinimalLogger
2131
}
2232

2333
export interface BundleCssResult {
@@ -67,7 +77,9 @@ export async function bundleWithLightningCSS(
6777
minify: options.minify,
6878
resolver: {
6979
async read(filePath: string) {
70-
const code = await readFile(filePath, 'utf8')
80+
// Note: LightningCSS explicitly recommends using `readFileSync` instead
81+
// of `readFile` for better performance.
82+
const code = readFileSync(filePath, 'utf8')
7183
const lang = getPreprocessorLang(filePath)
7284
if (lang) {
7385
const preprocessed = await compilePreprocessor(
@@ -82,7 +94,15 @@ export async function bundleWithLightningCSS(
8294
return code
8395
},
8496
resolve(specifier: string, from: string) {
85-
return path.resolve(path.dirname(from), specifier)
97+
const dir = path.dirname(from)
98+
const result = getResolver().sync(dir, specifier)
99+
if (result.error || !result.path) {
100+
options.logger.warn(
101+
`[@tsdown/css] Failed to resolve import '${specifier}' from '${from}': ${result.error || 'unknown error'}`,
102+
)
103+
return path.resolve(dir, specifier)
104+
}
105+
return result.path
86106
},
87107
},
88108
})

packages/css/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface MinimalLogger {
2+
info: (...args: any[]) => void
3+
warn: (...args: any[]) => void
4+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/rolldown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function resolveInputOptions(
152152
let cssPlugin: Plugin
153153
try {
154154
const { CssPlugin: AdvancedCssPlugin } = await import('@tsdown/css')
155-
cssPlugin = AdvancedCssPlugin(config)
155+
cssPlugin = AdvancedCssPlugin(config, { logger })
156156
} catch {
157157
cssPlugin = CssPlugin(config)
158158
}

tests/__snapshots__/css/lightningcss-inlines-import-default-.snap.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export { };
1111
color: #00f;
1212
}
1313

14+
.lib {
15+
color: green;
16+
}
17+
1418
.main {
1519
color: red;
1620
}

tests/__snapshots__/css/lightningcss-transformer-ignores-postcss-plugins.snap.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/__snapshots__/css/lightningcss-transformer-inlines-import.snap.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/__snapshots__/css/postcss-with-import.snap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export { };
77
## style.css
88

99
```css
10-
/* postcss-processed */ .other { color: blue } .main { color: red }
10+
/* postcss-processed */ .other { color: blue } .lib { color: green } .main { color: red }
1111

1212
```

0 commit comments

Comments
 (0)