Skip to content

Commit

Permalink
fix(vite): non-win32 file resolution (#809)
Browse files Browse the repository at this point in the history
Vite passes the plugin paths that *look* like they're absolute, but they're actually local paths. So check if the file exists if it doesn't just assume we can whack `process.cwd()` at the front of it.
  • Loading branch information
tivac authored Feb 9, 2022
1 parent b8406a1 commit 115c517
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/vite/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const virtualize = (file) => `${file}?${CSS_QUERY}`;
const devirtualize = (file) => `${file.split("?")[0]}`;
const isVirtual = (file) => file.endsWith(`?${CSS_QUERY}`);

// NOTE: Can't use basic isAbsolute check because of paths vite passes in. On windows they can be
// "/foo/bar/baz.mcss" which isAbsolute will treat as absolute, even though it definitely isn't
const isPartial = (file) => (process.platform !== "win32" ? !path.isAbsolute(file) : path.parse(file).root === "/");

module.exports = (
/* istanbul ignore next: too painful to test */
pluginOptions = {}
Expand Down Expand Up @@ -89,15 +85,26 @@ module.exports = (
Object.keys(processor.files).forEach((file) => this.addWatchFile(file));
},

resolveId(source) {
async resolveId(source) {
// Only care about our particular type of virtual file
if(!isVirtual(source)) {
return null;
}

log("resolving", source);

let resolved = source;

// Check file as passed (minus the query params)
if(processor.has(devirtualize(resolved))) {
return resolved;
}

resolved = path.join(process.cwd(), resolved);

if(isPartial(resolved)) {
resolved = path.join(process.cwd(), resolved);
// Check file as an asbolute path (minus the query params)
if(!processor.has(devirtualize(resolved))) {
return null;
}

resolved = slash(resolved);
Expand All @@ -118,15 +125,9 @@ module.exports = (

log("loading", id);

let file = devirtualize(id);

if(isPartial(file)) {
file = path.join(process.cwd(), file);
}
const file = devirtualize(id);

if(!processor.has(file)) {
log("no loading", file);

return null;
}

Expand Down

0 comments on commit 115c517

Please sign in to comment.