Skip to content

Commit

Permalink
fix(vite): Fix CSS replacement in Vite HMR (#73)
Browse files Browse the repository at this point in the history
* fix(vite): Fix CSS replacement in Vite HMR

Also remove dependency on @rollup/plugin-utils

* Add changeset
  • Loading branch information
brijeshb42 authored Apr 16, 2024
1 parent cd7b7f0 commit ee9e680
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-bags-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wyw-in-js/vite': patch
---

Fix CSS updation during Vite HMR where the new change wouldn't get correctly applied
1 change: 0 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@wyw-in-js/vite",
"version": "0.5.0",
"dependencies": {
"@rollup/pluginutils": "^5.0.4",
"@wyw-in-js/shared": "workspace:*",
"@wyw-in-js/transform": "workspace:*"
},
Expand Down
34 changes: 17 additions & 17 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import { existsSync } from 'fs';
import path from 'path';

import type { FilterPattern } from '@rollup/pluginutils';
import { createFilter } from '@rollup/pluginutils';
import { optimizeDeps } from 'vite';
import type { ModuleNode, Plugin, ResolvedConfig, ViteDevServer } from 'vite';
import { optimizeDeps, createFilter } from 'vite';
import type {
ModuleNode,
Plugin,
ResolvedConfig,
ViteDevServer,
FilterPattern,
} from 'vite';

import { logger, syncResolve } from '@wyw-in-js/shared';
import type {
Expand All @@ -21,7 +25,6 @@ import type {
import {
createFileReporter,
getFileIdx,
slugify,
transform,
TransformCacheCollection,
} from '@wyw-in-js/transform';
Expand Down Expand Up @@ -182,10 +185,8 @@ export default function wywInJS({

dependencies ??= [];

const slug = slugify(cssText);

const cssFilename = path
.normalize(`${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`)
.normalize(`${id.replace(/\.[jt]sx?$/, '')}.wyw-in-js.css`)
.replace(/\\/g, path.posix.sep);

const cssRelativePath = path
Expand All @@ -203,15 +204,6 @@ export default function wywInJS({
cssFileLookup[cssId] = cssFilename;

result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;
if (devServer?.moduleGraph) {
const module = devServer.moduleGraph.getModuleById(cssId);

if (module) {
devServer.moduleGraph.invalidateModule(module);
module.lastHMRTimestamp =
module.lastInvalidationTimestamp || Date.now();
}
}

for (let i = 0, end = dependencies.length; i < end; i++) {
// eslint-disable-next-line no-await-in-loop
Expand All @@ -223,6 +215,14 @@ export default function wywInJS({
const target = targets.find((t) => t.id === id);
if (!target) targets.push({ id, dependencies });
else target.dependencies = dependencies;

if (devServer?.moduleGraph) {
const module = devServer.moduleGraph.getModuleById(cssFilename);

if (module) {
devServer.reloadModule(module);
}
}
/* eslint-disable-next-line consistent-return */
return { code: result.code, map: result.sourceMap };
},
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee9e680

Please sign in to comment.