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

[pigment-css][nextjs-plugin] Add missing RTL implementation #41751

Merged
merged 1 commit into from
Apr 4, 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
24 changes: 17 additions & 7 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
generateThemeTokens,
extendTheme,
type Theme as BaseTheme,
type PluginCustomOptions,
} from '@pigment-css/react/utils';
import type { ResolvePluginInstance } from 'webpack';

Expand Down Expand Up @@ -52,7 +53,8 @@ export type PigmentOptions<Theme extends BaseTheme = BaseTheme> = {
meta?: Meta;
asyncResolve?: (...args: Parameters<AsyncResolver>) => Promise<string | null>;
transformSx?: boolean;
} & Partial<WywInJsPluginOptions>;
} & Partial<WywInJsPluginOptions> &
Omit<PluginCustomOptions, 'themeArgs'>;

const extensions = ['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts'];

Expand Down Expand Up @@ -100,13 +102,14 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
theme,
meta,
transformLibraries = [],
preprocessor = basePreprocessor,
preprocessor,
asyncResolve: asyncResolveOpt,
debug = false,
sourceMap = false,
transformSx = true,
overrideContext,
tagResolver,
css,
...rest
} = options;
const cache = new TransformCacheCollection();
Expand Down Expand Up @@ -151,6 +154,10 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
return asyncResolveFallback(what, importer, stack);
};

const withRtl = (selector: string, cssText: string) => {
return basePreprocessor(selector, cssText, css);
};

const wywInJSTransformPlugin: UnpluginOptions = {
name: 'zero-plugin-transform-wyw-in-js',
enforce: 'post',
Expand Down Expand Up @@ -188,12 +195,13 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
compiler.options.resolve.plugins = compiler.options.resolve.plugins || [];
compiler.options.resolve.plugins.push(resolverPlugin);
},
async transform(code, id) {
async transform(code, filePath) {
const [id] = filePath.split('?');
const transformServices = {
options: {
filename: id,
root: process.cwd(),
preprocessor,
preprocessor: preprocessor ?? withRtl,
pluginOptions: {
...rest,
themeArgs: {
Expand Down Expand Up @@ -246,8 +254,6 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
map: result.sourceMap,
};
}
const slug = slugify(cssText);
const cssFilename = `${slug}.zero.css`;

if (sourceMap && result.cssSourceMapText) {
const map = Buffer.from(result.cssSourceMapText).toString('base64');
Expand All @@ -260,7 +266,7 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
if (isNext) {
const data = `${meta.placeholderCssFile}?${encodeURIComponent(
JSON.stringify({
filename: cssFilename,
filename: id.split('/').pop(),
source: cssText,
}),
)}`;
Expand All @@ -270,9 +276,13 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
map: result.sourceMap,
};
}

const slug = slugify(cssText);
const cssFilename = `${slug}.zero.css`;
const cssId = `./${cssFilename}`;
cssFileLookup.set(cssId, cssFilename);
cssLookup.set(cssFilename, cssText);

return {
code: `${result.code}\nimport ${JSON.stringify(`./${cssFilename}`)};`,
map: result.sourceMap,
Expand Down
7 changes: 1 addition & 6 deletions packages/pigment-css-vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import {
import { transformAsync } from '@babel/core';
import baseWywPluginPlugin, { type VitePluginOptions } from './vite-plugin';

export interface PigmentOptions extends VitePluginOptions {
export interface PigmentOptions extends Omit<VitePluginOptions, 'themeArgs'> {
/**
* The theme object that you want to be passed to the `styled` function
*/
theme: Theme;
/**
* Whether the css variables for the default theme should target the :root selector or not.
* @default true
*/
injectDefaultThemeInRoot?: boolean;
}

const VIRTUAL_CSS_FILE = `\0zero-runtime-styles.css`;
Expand Down
Loading