Skip to content

Commit 63fcd5d

Browse files
committed
fix: assets cached under watch mode have an incorrect import path in importers #92
1 parent 4cf0686 commit 63fcd5d

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,24 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
314314
return map
315315
}, {} as Record<string, string>)
316316

317-
const updatedSourceMap = processAssetsInStyle(bundleSourceMap)
317+
/** Assets cached under watch mode also need to be processed by `processAssetsInStyle` and `processAssetsInImporters` #92 */
318+
const cacheSourceMap = isBuildWatch
319+
? Object.values(Object.fromEntries(assetCache)).reduce((map, { fileName, source }) => {
320+
if (fileName && source && !outputBundle[fileName])
321+
map[fileName] = String(source)
322+
return map
323+
}, {} as Record<string, string>)
324+
: {}
325+
326+
const updatedSourceMap = processAssetsInStyle({ ...bundleSourceMap, ...cacheSourceMap })
318327
const processedSourceMap = processAssetsInImporters(updatedSourceMap)
319328

320329
Object.keys(bundleSourceMap)
321-
.filter(name => bundleSourceMap[name] !== processedSourceMap[name])
330+
/**
331+
* Under watch mode, Vite won't resolve assets and this plugin wont't emit them,
332+
* so they won't appear in the final output. We must trigger their emit manually.
333+
*/
334+
.filter(name => cacheSourceMap[name] || bundleSourceMap[name] !== processedSourceMap[name])
322335
.forEach((name) => {
323336
const outputPath = path.posix.join(outputDir, name)
324337
const updated = processedSourceMap[name]
@@ -331,17 +344,6 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
331344
else if (name.endsWith('.css'))
332345
bundle.source = updated
333346
})
334-
335-
/**
336-
* Under watch mode, Vite won't resolve assets and this plugin wont't emit them,
337-
* so they won't appear in the final output. We must trigger their emit manually.
338-
*/
339-
if (isBuildWatch) {
340-
assetCache.forEach(({ fileName, source }) => {
341-
if (fileName && source && !outputBundle[fileName])
342-
fs.writeFileSync(path.posix.join(outputDir, fileName), source)
343-
})
344-
}
345347
},
346348
}
347349
}

0 commit comments

Comments
 (0)