Skip to content

Commit 2e7bb84

Browse files
authored
[pigment-css][nextjs-plugin] Fix alias resolver (#41494)
1 parent c25839e commit 2e7bb84

File tree

1 file changed

+48
-7
lines changed
  • packages/pigment-css-unplugin/src

1 file changed

+48
-7
lines changed

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'node:path';
12
import { transformAsync } from '@babel/core';
23
import {
34
type Preprocessor,
@@ -21,6 +22,7 @@ import {
2122
extendTheme,
2223
type Theme as BaseTheme,
2324
} from '@pigment-css/react/utils';
25+
import type { ResolvePluginInstance } from 'webpack';
2426

2527
type NextMeta = {
2628
type: 'next';
@@ -60,6 +62,8 @@ function hasCorectExtension(fileName: string) {
6062
const VIRTUAL_CSS_FILE = `\0zero-runtime-styles.css`;
6163
const VIRTUAL_THEME_FILE = `\0zero-runtime-theme.js`;
6264

65+
type AsyncResolver = (what: string, importer: string, stack: string[]) => Promise<string>;
66+
6367
function isZeroRuntimeThemeFile(fileName: string) {
6468
return fileName === VIRTUAL_CSS_FILE || fileName === VIRTUAL_THEME_FILE;
6569
}
@@ -134,6 +138,22 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
134138
};
135139
},
136140
};
141+
142+
let webpackResolver: AsyncResolver;
143+
144+
const asyncResolve: AsyncResolver = async (what, importer, stack) => {
145+
const result = asyncResolveOpt?.(what);
146+
if (typeof result === 'string') {
147+
return result;
148+
}
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);
153+
}
154+
return asyncResolveFallback(what, importer, stack);
155+
};
156+
137157
const linariaTransformPlugin: UnpluginOptions = {
138158
name: 'zero-plugin-transform-linaria',
139159
enforce: 'post',
@@ -143,14 +163,35 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
143163
transformInclude(id) {
144164
return isZeroRuntimeProcessableFile(id, transformLibraries);
145165
},
146-
async transform(code, id) {
147-
const asyncResolve: typeof asyncResolveFallback = async (what, importer, stack) => {
148-
const result = asyncResolveOpt?.(what);
149-
if (typeof result === 'string') {
150-
return result;
151-
}
152-
return asyncResolveFallback(what, importer, stack);
166+
webpack(compiler) {
167+
const resolverPlugin: ResolvePluginInstance = {
168+
apply(resolver) {
169+
webpackResolver = function webpackAsyncResolve(
170+
what: string,
171+
importer: string,
172+
stack: string[],
173+
) {
174+
const context = path.isAbsolute(importer)
175+
? path.dirname(importer)
176+
: path.join(process.cwd(), path.dirname(importer));
177+
return new Promise((resolve, reject) => {
178+
resolver.resolve({}, context, what, { stack: new Set(stack) }, (err, result) => {
179+
if (err) {
180+
reject(err);
181+
} else if (result) {
182+
resolve(result);
183+
} else {
184+
reject(new Error(`${process.env.PACKAGE_NAME}: Cannot resolve ${what}`));
185+
}
186+
});
187+
});
188+
};
189+
},
153190
};
191+
compiler.options.resolve.plugins = compiler.options.resolve.plugins || [];
192+
compiler.options.resolve.plugins.push(resolverPlugin);
193+
},
194+
async transform(code, id) {
154195
const transformServices = {
155196
options: {
156197
filename: id,

0 commit comments

Comments
 (0)