Skip to content

Commit

Permalink
[pigment-css][nextjs-plugin] Follow-up to #41494 (#41502)
Browse files Browse the repository at this point in the history
Signed-off-by: Brijesh Bittu <[email protected]>
Co-authored-by: Siriwat K <[email protected]>
  • Loading branch information
brijeshb42 and siriwatknp authored Mar 15, 2024
1 parent c76a373 commit 257b5a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
23 changes: 13 additions & 10 deletions packages/pigment-css-nextjs-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import * as path from 'node:path';
import type { NextConfig } from 'next';
import { findPagesDir } from 'next/dist/lib/find-pages-dir';
import {
webpack as webpackPlugin,
extendTheme,
type PigmentOptions as BasePigmentOptions,
} from '@pigment-css/unplugin';
import { webpack as webpackPlugin, extendTheme, type PigmentOptions } from '@pigment-css/unplugin';

export type PigmentOptions = BasePigmentOptions & {
asyncResolve?: (what: string) => string | null;
};
export { type PigmentOptions };

const extractionFile = path.join(
path.dirname(require.resolve('../package.json')),
Expand Down Expand Up @@ -60,14 +54,23 @@ export function withPigment(nextConfig: NextConfig, pigmentConfig?: PigmentOptio
outputCss: dev || hasAppDir || !isServer,
placeholderCssFile: extractionFile,
},
asyncResolve(what) {
async asyncResolve(what: string, importer: string, stack: string[]) {
// Need to point to the react from node_modules during eval time.
// Otherwise, next makes it point to its own version of react that
// has a lot of RSC specific logic which is not actually needed.
if (what.startsWith('react') || what.startsWith('next')) {
return require.resolve(what);
}
if (what === 'next/image') {
return require.resolve('../next-image');
}
if (what.startsWith('next/font')) {
return require.resolve('../next-font');
}
return asyncResolve?.(what) ?? null;
if (asyncResolve) {
return asyncResolve(what, importer, stack);
}
return null;
},
babelOptions: {
...babelOptions,
Expand Down
11 changes: 4 additions & 7 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type WebpackMeta = {
};

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

export type PigmentOptions<Theme extends BaseTheme = BaseTheme> = {
theme?: Theme;
Expand All @@ -49,7 +50,7 @@ export type PigmentOptions<Theme extends BaseTheme = BaseTheme> = {
debug?: IFileReporterOptions | false;
sourceMap?: boolean;
meta?: Meta;
asyncResolve?: (what: string) => string | null;
asyncResolve?: (...args: Parameters<AsyncResolver>) => Promise<string | null>;
transformSx?: boolean;
} & Partial<WywInJsPluginOptions>;

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

type AsyncResolver = (what: string, importer: string, stack: string[]) => Promise<string>;

function isZeroRuntimeThemeFile(fileName: string) {
return fileName === VIRTUAL_CSS_FILE || fileName === VIRTUAL_THEME_FILE;
}
Expand Down Expand Up @@ -142,13 +141,11 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
let webpackResolver: AsyncResolver;

const asyncResolve: AsyncResolver = async (what, importer, stack) => {
const result = asyncResolveOpt?.(what);
const result = await asyncResolveOpt?.(what, importer, stack);
if (typeof result === 'string') {
return result;
}
// Use Webpack's resolver to resolve actual path but
// ignore next.js files during evaluation phase of WyW
if (webpackResolver && !what.startsWith('next')) {
if (webpackResolver) {
return webpackResolver(what, importer, stack);
}
return asyncResolveFallback(what, importer, stack);
Expand Down

0 comments on commit 257b5a4

Please sign in to comment.