From 9a879c8634ec9145efc0ef1743d3a6092b90f140 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu Date: Fri, 30 Aug 2024 13:25:05 +0530 Subject: [PATCH] [plugin] Fix bug related to path separator on Windows (#218) --- packages/pigment-css-unplugin/src/index.ts | 9 ++++++--- .../pigment-css-vite-plugin/src/vite-plugin.ts | 14 ++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/pigment-css-unplugin/src/index.ts b/packages/pigment-css-unplugin/src/index.ts index 62a6effe..6ae7addc 100644 --- a/packages/pigment-css-unplugin/src/index.ts +++ b/packages/pigment-css-unplugin/src/index.ts @@ -218,8 +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 [id] = 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(filePath); 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) {