Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin] Fix bug related to path separator on Windows #218

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this arg differs from the one in the unplugin (url vs filePath)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference. Updated to use same names.

// 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
Loading