From 7b67221e36cc9ffa8e738b88c979f2851fd577f7 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu Date: Fri, 30 Aug 2024 09:32:49 +0530 Subject: [PATCH 1/2] [plugin] Fix bug related to path separator on Windows 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. --- packages/pigment-css-unplugin/src/index.ts | 7 +++++-- .../pigment-css-vite-plugin/src/vite-plugin.ts | 14 ++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/pigment-css-unplugin/src/index.ts b/packages/pigment-css-unplugin/src/index.ts index 62a6effe..6f276a05 100644 --- a/packages/pigment-css-unplugin/src/index.ts +++ b/packages/pigment-css-unplugin/src/index.ts @@ -219,7 +219,10 @@ export const plugin = createUnplugin((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, @@ -314,7 +317,7 @@ export const plugin = createUnplugin((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__'), }), )}`; diff --git a/packages/pigment-css-vite-plugin/src/vite-plugin.ts b/packages/pigment-css-vite-plugin/src/vite-plugin.ts index af388815..02580197 100644 --- a/packages/pigment-css-vite-plugin/src/vite-plugin.ts +++ b/packages/pigment-css-vite-plugin/src/vite-plugin.ts @@ -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) { @@ -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; } @@ -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) { From 9462e64b3130cecda39076ab7e62179b01c3cfd0 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu Date: Fri, 30 Aug 2024 12:38:55 +0530 Subject: [PATCH 2/2] Use variable names in vite and unplugin --- packages/pigment-css-unplugin/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/pigment-css-unplugin/src/index.ts b/packages/pigment-css-unplugin/src/index.ts index 6f276a05..6ae7addc 100644 --- a/packages/pigment-css-unplugin/src/index.ts +++ b/packages/pigment-css-unplugin/src/index.ts @@ -218,11 +218,11 @@ export const plugin = createUnplugin((options) => { compiler.options.resolve.plugins = compiler.options.resolve.plugins || []; compiler.options.resolve.plugins.push(resolverPlugin); }, - async transform(code, filePath) { - const [pathSegment] = filePath.split('?'); + async transform(code, url) { + const [filePath] = url.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 id = path.normalize(filePath); const transformServices = { options: { filename: id,