Skip to content

Commit

Permalink
[plugin] Fix bug related to path separator on Windows
Browse files Browse the repository at this point in the history
While debugging on Windows, I found that (atleast on Windows 11), the
path separator was `/` instead of the expected `\` resulting in some
files being skipped from being transformed from the `node_modules`.

So the fix first normalizes the path first as per the platform and then
checks for the `transformLibraries`'s presence in the normalized path.
  • Loading branch information
brijeshb42 committed Aug 30, 2024
1 parent 5499c64 commit 7b67221
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
compiler.options.resolve.plugins.push(resolverPlugin);
},
async transform(code, filePath) {
const [id] = filePath.split('?');
const [pathSegment] = filePath.split('?');
// Converts path separator as per platform, even on Windows, path segments have `/` instead of the usual `\`,
// so this function replaces such path separators.
const id = path.normalize(pathSegment);
const transformServices = {
options: {
filename: id,
Expand Down Expand Up @@ -314,7 +317,7 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
if (isNext) {
const data = `${meta.placeholderCssFile}?${encodeURIComponent(
JSON.stringify({
filename: id.split('/').pop(),
filename: id.split(path.sep).pop(),
source: cssText.replaceAll('!important', '__IMP__'),
}),
)}`;
Expand Down
14 changes: 8 additions & 6 deletions packages/pigment-css-vite-plugin/src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,19 @@ export default function wywVitePlugin({
.filter((m): m is ModuleNode => !!m);
},
async transform(code, url) {
const [id] = url.split('?', 1);

const [filePath] = url.split('?', 1);
// Converts path separator as per platform, even on Windows, path segments have `/` instead of the usual `\`,
// so this function replaces such path separators.
const id = path.normalize(filePath);
// Main modification starts
if (id in cssLookup) {
return null;
}

let shouldReturn = url.includes('node_modules');
let shouldReturn = id.includes('node_modules');

if (shouldReturn) {
shouldReturn = !transformLibraries.some((libName: string) => url.includes(libName));
shouldReturn = !transformLibraries.some((libName: string) => id.includes(libName));
}

if (shouldReturn) {
Expand All @@ -151,7 +153,7 @@ export default function wywVitePlugin({
// Main modification end

// Do not transform ignored and generated files
if (!filter(url)) {
if (!filter(id)) {
return null;
}

Expand Down Expand Up @@ -282,7 +284,7 @@ export default function wywVitePlugin({

for (let i = 0, end = dependencies.length; i < end; i += 1) {
// eslint-disable-next-line no-await-in-loop
const depModule = await this.resolve(dependencies[i], url, {
const depModule = await this.resolve(dependencies[i], id, {
isEntry: false,
});
if (depModule) {
Expand Down

0 comments on commit 7b67221

Please sign in to comment.