Skip to content

Commit e2c439f

Browse files
authored
allow plugin to work in rolldown (vanilla-extract-css#1616)
1 parent 6714033 commit e2c439f

File tree

7 files changed

+1655
-33
lines changed

7 files changed

+1655
-33
lines changed

.changeset/light-guests-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/rollup-plugin': patch
3+
---
4+
5+
allow plugin to work in rolldown

packages/rollup-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@rollup/plugin-json": "^6.1.0",
2525
"@vanilla-extract/css": "workspace:^",
2626
"esbuild": "~0.25.0",
27+
"rolldown": "1.0.0-beta.27",
2728
"rollup": "^4.20.0",
2829
"rollup-plugin-esbuild": "^6.1.1"
2930
},

packages/rollup-plugin/src/index.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,21 @@ export function vanillaExtractPlugin({
116116
},
117117
};
118118
},
119-
120-
// Emit .css assets
121-
moduleParsed(moduleInfo) {
122-
moduleInfo.importedIdResolutions.forEach((resolution) => {
123-
if (resolution.meta.css && !extract) {
124-
resolution.meta.assetId = this.emitFile({
125-
type: 'asset',
126-
name: resolution.id,
127-
source: resolution.meta.css,
128-
});
129-
}
130-
});
131-
},
132-
133-
// Replace .css import paths with relative paths to emitted css files
119+
// Emit .css assets and replace .css import paths with relative paths to emitted css files
134120
renderChunk(code, chunkInfo) {
135121
const chunkPath = dirname(chunkInfo.fileName);
136122
const output = chunkInfo.imports.reduce((codeResult, importPath) => {
137123
const moduleInfo = this.getModuleInfo(importPath);
138-
if (!moduleInfo?.meta.assetId) {
124+
if (!moduleInfo?.meta.css || extract) {
139125
return codeResult;
140126
}
141127

142-
const assetPath = this.getFileName(moduleInfo?.meta.assetId);
128+
const assetId = this.emitFile({
129+
type: 'asset',
130+
name: moduleInfo.id,
131+
source: moduleInfo.meta.css,
132+
});
133+
const assetPath = this.getFileName(assetId);
143134
const relativeAssetPath = `./${normalize(
144135
relative(chunkPath, assetPath),
145136
)}`;

packages/rollup-plugin/src/lib.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import MagicString, { Bundle as MagicStringBundle } from 'magic-string';
33
import type { ModuleInfo, PluginContext } from 'rollup';
44

55
/** Generate a CSS bundle from Rollup context */
6-
export function generateCssBundle({
7-
getModuleIds,
8-
getModuleInfo,
9-
warn,
10-
}: Pick<PluginContext, 'getModuleIds' | 'getModuleInfo' | 'warn'>): {
6+
export function generateCssBundle(
7+
plugin: Pick<PluginContext, 'getModuleIds' | 'getModuleInfo' | 'warn'>,
8+
): {
119
bundle: MagicStringBundle;
1210
extractedCssIds: Set<string>;
1311
} {
@@ -16,17 +14,18 @@ export function generateCssBundle({
1614

1715
// 1. identify CSS files to bundle
1816
const cssFiles: Record<string, ImportChain> = {};
19-
for (const id of getModuleIds()) {
17+
for (const id of plugin.getModuleIds()) {
2018
if (cssFileFilter.test(id)) {
21-
cssFiles[id] = buildImportChain(id, { getModuleInfo, warn });
19+
cssFiles[id] = buildImportChain(id, plugin);
2220
}
2321
}
2422

2523
// 2. build bundle from import order
2624
for (const id of sortModules(cssFiles)) {
27-
const { importedIdResolutions } = getModuleInfo(id) ?? {};
28-
for (const resolution of importedIdResolutions ?? []) {
29-
if (resolution.meta.css && !extractedCssIds.has(resolution.id)) {
25+
const { importedIds } = plugin.getModuleInfo(id) ?? {};
26+
for (const importedId of importedIds ?? []) {
27+
const resolution = plugin.getModuleInfo(importedId);
28+
if (resolution?.meta.css && !extractedCssIds.has(resolution.id)) {
3029
extractedCssIds.add(resolution.id);
3130
cssBundle.addSource({
3231
filename: resolution.id,
@@ -45,9 +44,9 @@ export type ImportChain = [id: string, order: number][];
4544
/** Trace a file back through its importers, building an ordered list */
4645
export function buildImportChain(
4746
id: string,
48-
{ getModuleInfo, warn }: Pick<PluginContext, 'getModuleInfo' | 'warn'>,
47+
plugin: Pick<PluginContext, 'getModuleInfo' | 'warn'>,
4948
): ImportChain {
50-
let mod: ModuleInfo | null = getModuleInfo(id)!;
49+
let mod: ModuleInfo | null = plugin.getModuleInfo(id)!;
5150
if (!mod) {
5251
return [];
5352
}
@@ -61,14 +60,14 @@ export function buildImportChain(
6160
break;
6261
}
6362
if (chain.some(([id]) => id === lastImporterId)) {
64-
warn(
63+
plugin.warn(
6564
`Circular import detected. Can’t determine ideal import order of module.\n${chain
6665
.reverse()
6766
.join('\n → ')}`,
6867
);
6968
break;
7069
}
71-
mod = getModuleInfo(lastImporterId);
70+
mod = plugin.getModuleInfo(lastImporterId);
7271
if (!mod) {
7372
break;
7473
}

0 commit comments

Comments
 (0)