1+ import * as path from 'node:path' ;
12import { transformAsync } from '@babel/core' ;
23import {
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
2527type NextMeta = {
2628 type : 'next' ;
@@ -60,6 +62,8 @@ function hasCorectExtension(fileName: string) {
6062const VIRTUAL_CSS_FILE = `\0zero-runtime-styles.css` ;
6163const VIRTUAL_THEME_FILE = `\0zero-runtime-theme.js` ;
6264
65+ type AsyncResolver = ( what : string , importer : string , stack : string [ ] ) => Promise < string > ;
66+
6367function 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