fix: path matching on windows

fix: #64
This commit is contained in:
Mark Lawlor
2022-05-28 20:39:37 +10:00
parent 0a915b97bf
commit 2ec7363d07
2 changed files with 32 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
import micromatch from "micromatch";
import { join, isAbsolute } from "node:path";
import { resolve, sep, posix } from "node:path";
import { TailwindConfig } from "tailwindcss/tailwind-config";
import { TailwindcssReactNativeBabelOptions, AllowPathOptions } from "../types";
@@ -43,9 +43,19 @@ export function isAllowedProgramPath({
return true;
}
/**
* This is my naive way to get path matching working on Windows.
* Basically I turn it into a posix path which seems to work fine
*
* If you are a windows user and understand micromatch, can you please send a PR
* to do this the proper way
*/
const posixPath = path.split(sep).join(posix.sep);
return allowRelativeModules.some((modulePath) => {
return isAbsolute(modulePath)
? micromatch.isMatch(path, modulePath)
: micromatch.isMatch(path, join(cwd, modulePath));
return micromatch.isMatch(
posixPath,
resolve(cwd, modulePath).split(sep).join(posix.sep)
);
});
}

View File

@@ -1,5 +1,5 @@
import { createRequire } from "node:module";
import { join, dirname, basename, resolve } from "node:path";
import { join, dirname, basename, resolve, sep, posix } from "node:path";
import { readdirSync, lstatSync, existsSync } from "node:fs";
import micromatch from "micromatch";
@@ -88,11 +88,24 @@ export function getImportBlockedComponents(
} else {
const normalizedAllowRelativeModules = !Array.isArray(allowRelativeModules)
? []
: allowRelativeModules.map((modulePath) => resolve(cwd, modulePath));
: allowRelativeModules.map((modulePath) =>
resolve(cwd, modulePath).split(sep).join(posix.sep)
);
const isNotAllowedRelative = !modulePaths.some((modulePath) =>
micromatch.isMatch(modulePath, normalizedAllowRelativeModules)
);
const isNotAllowedRelative = !modulePaths.some((modulePath) => {
/**
* This is my naive way to get path matching working on Windows.
* Basically I turn it into a posix path which seems to work fine
*
* If you are a windows user and understand micromatch, can you please send a PR
* to do this the proper way
*/
const posixModulePath = modulePath.split(sep).join(posix.sep);
return micromatch.isMatch(
posixModulePath,
normalizedAllowRelativeModules
);
});
returnComponentsAsBlocked = isNotAllowedRelative;
}