Skip to content

Commit b0f46fd

Browse files
committed
[pigment-css][nextjs-plugin] Follow to #41494
Resolve React and other related libraries to the actual ones in node_modules instead of Next.js' own version of React that will also include RSC related code that is not actually needed during WyW eval.
1 parent 5c956ba commit b0f46fd

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/pigment-css-nextjs-plugin/src/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import * as path from 'node:path';
22
import type { NextConfig } from 'next';
33
import { findPagesDir } from 'next/dist/lib/find-pages-dir';
4-
import {
5-
webpack as webpackPlugin,
6-
extendTheme,
7-
type PigmentOptions as BasePigmentOptions,
8-
} from '@pigment-css/unplugin';
4+
import { webpack as webpackPlugin, extendTheme, type PigmentOptions } from '@pigment-css/unplugin';
95

10-
export type PigmentOptions = BasePigmentOptions & {
11-
asyncResolve?: (what: string) => string | null;
12-
};
6+
export { type PigmentOptions };
137

148
const extractionFile = path.join(
159
path.dirname(require.resolve('../package.json')),
@@ -60,14 +54,23 @@ export function withPigment(nextConfig: NextConfig, pigmentConfig?: PigmentOptio
6054
outputCss: dev || hasAppDir || !isServer,
6155
placeholderCssFile: extractionFile,
6256
},
63-
asyncResolve(what) {
57+
async asyncResolve(what: string, importer: string, stack: string[]) {
58+
// Need to point to the react from node_modules during eval time.
59+
// Otherwise, next makes it point to its own version of react that
60+
// has a lot of RSC specific logic which is not actually needed.
61+
if (what.startsWith('react') || what.startsWith('next')) {
62+
return require.resolve(what);
63+
}
6464
if (what === 'next/image') {
6565
return require.resolve('../next-image');
6666
}
6767
if (what.startsWith('next/font')) {
6868
return require.resolve('../next-font');
6969
}
70-
return asyncResolve?.(what) ?? null;
70+
if (asyncResolve) {
71+
return asyncResolve(what, importer, stack);
72+
}
73+
return null;
7174
},
7275
babelOptions: {
7376
...babelOptions,

packages/pigment-css-unplugin/src/index.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type WebpackMeta = {
4141
};
4242

4343
type Meta = NextMeta | ViteMeta | WebpackMeta;
44+
export type AsyncResolver = (what: string, importer: string, stack: string[]) => Promise<string>;
4445

4546
export type PigmentOptions<Theme extends BaseTheme = BaseTheme> = {
4647
theme?: Theme;
@@ -49,7 +50,7 @@ export type PigmentOptions<Theme extends BaseTheme = BaseTheme> = {
4950
debug?: IFileReporterOptions | false;
5051
sourceMap?: boolean;
5152
meta?: Meta;
52-
asyncResolve?: (what: string) => string | null;
53+
asyncResolve?: (...args: Parameters<AsyncResolver>) => Promise<string | null>;
5354
transformSx?: boolean;
5455
} & Partial<WywInJsPluginOptions>;
5556

@@ -62,8 +63,6 @@ function hasCorectExtension(fileName: string) {
6263
const VIRTUAL_CSS_FILE = `\0zero-runtime-styles.css`;
6364
const VIRTUAL_THEME_FILE = `\0zero-runtime-theme.js`;
6465

65-
type AsyncResolver = (what: string, importer: string, stack: string[]) => Promise<string>;
66-
6766
function isZeroRuntimeThemeFile(fileName: string) {
6867
return fileName === VIRTUAL_CSS_FILE || fileName === VIRTUAL_THEME_FILE;
6968
}
@@ -142,14 +141,13 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
142141
let webpackResolver: AsyncResolver;
143142

144143
const asyncResolve: AsyncResolver = async (what, importer, stack) => {
145-
const result = asyncResolveOpt?.(what);
144+
const result = await asyncResolveOpt?.(what, importer, stack);
146145
if (typeof result === 'string') {
147146
return result;
148147
}
149-
// Use Webpack's resolver to resolve actual path but
150-
// ignore next.js files during evaluation phase of WyW
151-
if (webpackResolver && !what.startsWith('next')) {
152-
return webpackResolver(what, importer, stack);
148+
if (webpackResolver) {
149+
const resolved = webpackResolver(what, importer, stack);
150+
return resolved;
153151
}
154152
return asyncResolveFallback(what, importer, stack);
155153
};

0 commit comments

Comments
 (0)