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 micromatch from "micromatch";
import { join, isAbsolute } from "node:path"; import { resolve, sep, posix } from "node:path";
import { TailwindConfig } from "tailwindcss/tailwind-config"; import { TailwindConfig } from "tailwindcss/tailwind-config";
import { TailwindcssReactNativeBabelOptions, AllowPathOptions } from "../types"; import { TailwindcssReactNativeBabelOptions, AllowPathOptions } from "../types";
@@ -43,9 +43,19 @@ export function isAllowedProgramPath({
return true; 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 allowRelativeModules.some((modulePath) => {
return isAbsolute(modulePath) return micromatch.isMatch(
? micromatch.isMatch(path, modulePath) posixPath,
: micromatch.isMatch(path, join(cwd, modulePath)); resolve(cwd, modulePath).split(sep).join(posix.sep)
);
}); });
} }

View File

@@ -1,5 +1,5 @@
import { createRequire } from "node:module"; 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 { readdirSync, lstatSync, existsSync } from "node:fs";
import micromatch from "micromatch"; import micromatch from "micromatch";
@@ -88,11 +88,24 @@ export function getImportBlockedComponents(
} else { } else {
const normalizedAllowRelativeModules = !Array.isArray(allowRelativeModules) 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) => const isNotAllowedRelative = !modulePaths.some((modulePath) => {
micromatch.isMatch(modulePath, normalizedAllowRelativeModules) /**
); * 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; returnComponentsAsBlocked = isNotAllowedRelative;
} }