diff --git a/packages/client/tsdown.client.ts b/packages/client/tsdown.client.ts index 7bc2922be9..6b4fb97491 100644 --- a/packages/client/tsdown.client.ts +++ b/packages/client/tsdown.client.ts @@ -73,15 +73,17 @@ function compiledClientStyle( fileId: string, expression: string, ): CompiledClientStyle { - const path = relative(packageDir, fileId).split(sep).join('/').replace(/^lib\/types\//, 'src/') - if (!path.startsWith('../')) return { path, expression } - let directory = dirname(fileId) + // Rolldown canonicalizes module ids through symbolic links; the package directory arrives as the caller wrote it. + const file = realpathSync(fileId) + const local = pathBelow(realpathSync(packageDir), file) + if (local !== undefined) return { path: local.split(sep).join('/').replace(/^lib\/types\//, 'src/'), expression } + let directory = dirname(file) while (dirname(directory) !== directory) { const manifestPath = join(directory, 'package.json') if (existsSync(manifestPath)) { const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) as { name?: string } if (typeof manifest.name === 'string') { - return { path: `node_modules/${manifest.name}/${relative(directory, fileId).split(sep).join('/')}`, expression } + return { path: `node_modules/${manifest.name}/${relative(directory, file).split(sep).join('/')}`, expression } } } directory = dirname(directory) diff --git a/scripts/client-bundle-purity.spec.ts b/scripts/client-bundle-purity.spec.ts index 84623469d7..2ed0bd3d5e 100644 --- a/scripts/client-bundle-purity.spec.ts +++ b/scripts/client-bundle-purity.spec.ts @@ -2,7 +2,7 @@ * Pins shared client-bundle preset rules: build modes, module-edge purity, * source-map chaining, and physical watch dependencies hidden behind virtual CSS Modules. */ -import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join, relative } from 'node:path' import { fileURLToPath } from 'node:url' @@ -298,6 +298,25 @@ describe('static Client dependency delivery', () => { expect(stylesheets!['src/global.css']!.text(require)) .toContain('.overlay{position:fixed}') }) + + it('names local styles below the package when the package directory is a symbolic link', async () => { + const root = fixture('import classes from "./Panel.module.css"\nexport { classes }\n') + mkdirSync(join(root, 'src'), { recursive: true }) + writeFileSync(join(root, 'src/Panel.module.css'), '.panel { display: flex; }\n') + const linked = join(root, 'linked') + symlinkSync(root, linked, process.platform === 'win32' ? 'junction' : 'dir') + + const output = await clientChunkOutput(linked, config(linked), 'index.js') + const artifact = await import(`data:text/javascript,${encodeURIComponent(output)}`) as StaticArtifact + const manifest = artifact[STATIC_MODULE_MANIFEST_EXPORT] + const meta = manifest.meta + if (meta === undefined) throw new Error('static CSS metadata missing') + const stylesheets = meta().stylesheets + expect(Object.keys(stylesheets ?? {})).toEqual(['src/Panel.module.css']) + const exports = manifest.factory(unusedRequire) as { classes: { panel: string } } + expect(stylesheets?.['src/Panel.module.css']?.text(unusedRequire)) + .toContain(`.${exports.classes.panel}{display:flex}`) + }) }) function dynamicImportFixture(prefix: string): string {