Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): Fix CSS replacement in Vite HMR #73

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading