Skip to content

Commit

Permalink
[plugin] Fix bug related to path separator on Windows (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
brijeshb42 authored Aug 30, 2024
1 parent 5499c64 commit 9a879c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ export const plugin = createUnplugin<PigmentOptions, true>((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,
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 9a879c8

Please sign in to comment.