Skip to content

Commit

Permalink
[next] Wrap all generated CSS with a cascade layer (WIP) (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Sep 11, 2024
1 parent 4a8bc55 commit 2a45a3a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from '@pigment-css/react/utils';
import type { ResolvePluginInstance } from 'webpack';
import { styledEngineMockup } from '@pigment-css/react/internal';

import { handleUrlReplacement, type AsyncResolver } from './utils';

type NextMeta = {
Expand Down Expand Up @@ -284,6 +283,9 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
}

let { cssText } = result;

const slug = slugify(cssText);

if (isNext && !outputCss) {
return {
code: result.code,
Expand All @@ -298,6 +300,19 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
if (cssText && cssText.includes('url(')) {
cssText = await handleUrlReplacement(cssText, id, asyncResolve, projectPath);
}

// Valid names must start with a underscore or letter.
const layerName = `_${slug}`;

// Fix for https://github.com/mui/pigment-css/issues/199
// Bring each file in its own layer so that the order is maintained between css modules
// shared between layout.tsx and page.tsx.
// TODO: Do this in a way that keeps the source map correct
cssText = `
@layer pigment.${layerName} {
${cssText}
}
`;
}

if (sourceMap && result.cssSourceMapText) {
Expand All @@ -324,7 +339,7 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
}

const rootPath = process.cwd();
const slug = slugify(cssText);

const cssFilename = path
.normalize(`${id.replace(/\.[jt]sx?$/, '')}-${slug}.pigment.css`)
.replace(/\\/g, path.posix.sep);
Expand Down

0 comments on commit 2a45a3a

Please sign in to comment.