Skip to content

Commit fdafb6d

Browse files
authored
Correctly resolve module paths when using Vite plugins that affect module resolution (#1309)
1 parent 20e33a5 commit fdafb6d

File tree

14 files changed

+961
-682
lines changed

14 files changed

+961
-682
lines changed

.changeset/integration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@vanilla-extract/integration': major
3+
---
4+
5+
Use absolute paths internally to make sure Vite resolves modules correctly
6+
7+
This change only affects integrations that use the vite-node compiler, which is currently the esbuild (next) and Vite plugins

.changeset/plugins.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
'@vanilla-extract/esbuild-plugin-next': patch
4+
---
5+
6+
Correctly resolve module paths when using Vite plugins that affect module resolution, such as [`vite-tsconfig-paths`](https://github.com/aleclarson/vite-tsconfig-paths)

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"start-site": "pnpm --filter=site start",
1515
"build-site": "pnpm --filter=site build",
1616
"test:unit": "pnpm test:jest && pnpm test:vitest",
17-
"test:jest": "jest",
17+
"test:jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
1818
"test:vitest": "vitest --watch=false",
1919
"test:playwright": "NODE_OPTIONS=--no-experimental-fetch playwright test",
2020
"format": "prettier --write .",
@@ -41,17 +41,18 @@
4141
"@types/testing-library__jest-dom": "^5.14.5",
4242
"@vanilla-extract/jest-transform": "*",
4343
"babel-jest": "^27.3.1",
44+
"cross-env": "^7.0.3",
4445
"fast-glob": "^3.2.7",
45-
"jest": "^29.3.1",
46+
"jest": "^29.7.0",
4647
"jest-environment-jsdom": "^29.3.1",
4748
"prettier": "^2.8.8",
4849
"resolve.exports": "^1.1.0",
4950
"rollup": "^2.7.0",
5051
"rollup-plugin-dts": "^4.2.2",
5152
"rollup-plugin-node-externals": "^5.0.0",
5253
"ts-node": "^10.0.0",
53-
"typescript": "^4.9.4",
54-
"vitest": "^1.1.0"
54+
"typescript": "^5.3.3",
55+
"vitest": "^1.2.2"
5556
},
5657
"preconstruct": {
5758
"___experimentalFlags_WILL_CHANGE_IN_PATCH": {

packages/esbuild-plugin-next/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export function vanillaExtractPlugin({
5353
build.onLoad(
5454
{ filter: /.*/, namespace: vanillaCssNamespace },
5555
async ({ path }) => {
56-
const [rootRelativePath] = path.split('.vanilla.css');
56+
const [vanillaModulePath] = path.split('.vanilla.css');
5757

58-
let { css, filePath } = compiler.getCssForFile(rootRelativePath);
58+
let { css, filePath } = compiler.getCssForFile(vanillaModulePath);
5959

6060
if (typeof processCss === 'function') {
6161
css = await processCss(css);

packages/integration/src/compiler.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, relative, isAbsolute } from 'path';
1+
import { join, isAbsolute } from 'path';
22
import type { Adapter } from '@vanilla-extract/css';
33
import { transformCss } from '@vanilla-extract/css/transformCss';
44
import type { ModuleNode, InlineConfig as ViteConfig } from 'vite';
@@ -16,7 +16,7 @@ type Composition = Parameters<Adapter['registerComposition']>[0];
1616

1717
const globalAdapterIdentifier = '__vanilla_globalCssAdapter__';
1818

19-
const scanModule = (entryModule: ModuleNode, root: string) => {
19+
const scanModule = (entryModule: ModuleNode) => {
2020
const queue = new Set([entryModule]);
2121
const cssDeps = new Set<string>();
2222
const watchFiles = new Set<string>();
@@ -26,10 +26,8 @@ const scanModule = (entryModule: ModuleNode, root: string) => {
2626
continue;
2727
}
2828

29-
const relativePath = moduleNode.id && relative(root, moduleNode.id);
30-
31-
if (relativePath && cssFileFilter.test(relativePath)) {
32-
cssDeps.add(relativePath);
29+
if (moduleNode.id && cssFileFilter.test(moduleNode.id)) {
30+
cssDeps.add(moduleNode.id);
3331
}
3432
if (moduleNode.file) {
3533
watchFiles.add(moduleNode.file);
@@ -151,7 +149,7 @@ class NormalizedMap<V> extends Map<string, V> {
151149

152150
#normalizePath(filePath: string) {
153151
return normalizePath(
154-
isAbsolute(filePath) ? relative(this.root, filePath) : filePath,
152+
isAbsolute(filePath) ? filePath : join(this.root, filePath),
155153
);
156154
}
157155

@@ -319,7 +317,7 @@ export const createCompiler = ({
319317

320318
const cssImports = [];
321319

322-
const { cssDeps, watchFiles } = scanModule(moduleNode, root);
320+
const { cssDeps, watchFiles } = scanModule(moduleNode);
323321

324322
for (const cssDep of cssDeps) {
325323
const cssDepModuleId = normalizePath(cssDep);
@@ -388,9 +386,7 @@ export const createCompiler = ({
388386
},
389387
getCssForFile(filePath: string) {
390388
filePath = isAbsolute(filePath) ? filePath : join(root, filePath);
391-
const rootRelativePath = relative(root, filePath);
392-
393-
const moduleId = normalizePath(rootRelativePath);
389+
const moduleId = normalizePath(filePath);
394390
const result = cssCache.get(moduleId);
395391

396392
if (!result) {
@@ -399,7 +395,7 @@ export const createCompiler = ({
399395

400396
return {
401397
css: result.css,
402-
filePath: rootRelativePath,
398+
filePath: filePath,
403399
resolveDir: root,
404400
};
405401
},

0 commit comments

Comments
 (0)