Skip to content

Commit a0fd623

Browse files
graupmattcompiles
andauthored
rollup-plugin: fix caching in watch mode (#1084)
* rollup-plugin: fix caching in watch mode * Add changeset * lint --------- Co-authored-by: mattcompiles <[email protected]>
1 parent 0d0ea39 commit a0fd623

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

.changeset/healthy-turtles-help.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+
Fix emitting assets when in watch mode (#1076)

packages/rollup-plugin/src/index.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ export function vanillaExtractPlugin({
2222
cwd = process.cwd(),
2323
esbuildOptions,
2424
}: Options = {}): Plugin {
25-
const emittedFiles = new Map<string, string>();
2625
const isProduction = process.env.NODE_ENV === 'production';
2726

2827
return {
2928
name: 'vanilla-extract',
30-
buildStart() {
31-
emittedFiles.clear();
32-
},
29+
// Transform .css.js to .js
3330
async transform(_code, id) {
3431
if (!cssFileFilter.test(id)) {
3532
return null;
@@ -61,47 +58,47 @@ export function vanillaExtractPlugin({
6158
map: { mappings: '' },
6259
};
6360
},
61+
// Resolve .css to external module
6462
async resolveId(id) {
6563
if (!virtualCssFileFilter.test(id)) {
6664
return null;
6765
}
68-
69-
// Emit an asset for every virtual css file
7066
const { fileName, source } = await getSourceFromVirtualCssFile(id);
71-
if (!emittedFiles.get(fileName)) {
72-
const assetId = this.emitFile({
73-
type: 'asset',
74-
name: fileName,
75-
source,
76-
});
77-
emittedFiles.set(fileName, assetId);
78-
}
79-
80-
// Resolve to a temporary external module
8167
return {
8268
id: fileName,
8369
external: true,
70+
meta: {
71+
css: source,
72+
},
8473
};
8574
},
75+
// Emit .css assets
76+
moduleParsed(moduleInfo) {
77+
moduleInfo.importedIdResolutions.forEach((resolution) => {
78+
if (resolution.meta.css) {
79+
resolution.meta.assetId = this.emitFile({
80+
type: 'asset',
81+
name: resolution.id,
82+
source: resolution.meta.css,
83+
});
84+
}
85+
});
86+
},
87+
// Replace .css import paths with relative paths to emitted css files
8688
renderChunk(code, chunkInfo) {
87-
// For all imports in this chunk that we have emitted files for...
88-
const importsToReplace = chunkInfo.imports.filter((fileName) =>
89-
emittedFiles.get(fileName),
90-
);
91-
if (!importsToReplace.length) {
92-
return null;
93-
}
94-
95-
// ...replace import paths with relative paths to emitted css files
9689
const chunkPath = dirname(chunkInfo.fileName);
97-
const output = importsToReplace.reduce((codeResult, importPath) => {
98-
const assetId = emittedFiles.get(importPath)!;
99-
const assetName = this.getFileName(assetId);
100-
const fixedImportPath = `./${normalize(
101-
relative(chunkPath, assetName),
90+
const output = chunkInfo.imports.reduce((codeResult, importPath) => {
91+
const moduleInfo = this.getModuleInfo(importPath);
92+
if (!moduleInfo?.meta.assetId) {
93+
return codeResult;
94+
}
95+
const assetPath = this.getFileName(moduleInfo?.meta.assetId);
96+
const relativeAssetPath = `./${normalize(
97+
relative(chunkPath, assetPath),
10298
)}`;
103-
return codeResult.replace(importPath, fixedImportPath);
99+
return codeResult.replace(importPath, relativeAssetPath);
104100
}, code);
101+
105102
return {
106103
code: output,
107104
map: chunkInfo.map ?? null,

0 commit comments

Comments
 (0)