@@ -18,7 +18,7 @@ import loadConfigFallback from 'tailwindcss/loadConfig'
1818import resolveConfigFallback from 'tailwindcss/resolveConfig'
1919import type { RequiredConfig } from 'tailwindcss/types/config.js'
2020import { expiringMap } from './expiring-map.js'
21- import { resolveIn } from './resolve'
21+ import { resolveFrom , resolveIn } from './resolve'
2222import type { ContextContainer } from './types'
2323
2424let localRequire = createRequire ( import . meta. url )
@@ -147,6 +147,37 @@ async function loadTailwindConfig(
147147 }
148148}
149149
150+ /**
151+ * Create a loader function that can load plugins and config files relative to
152+ * the CSS file that uses them. However, we don't want missing files to prevent
153+ * everything from working so we'll let the error handler decide how to proceed.
154+ *
155+ * @param {object } param0
156+ * @returns
157+ */
158+ function createLoader < T > ( {
159+ filepath,
160+ onError,
161+ } : {
162+ filepath : string
163+ onError : ( id : string , error : unknown ) => T
164+ } ) {
165+ let baseDir = path . dirname ( filepath )
166+ let cacheKey = `${ + Date . now ( ) } `
167+
168+ return async function loadFile ( id : string ) {
169+ try {
170+ let resolved = resolveFrom ( baseDir , id )
171+ let url = pathToFileURL ( resolved )
172+ url . searchParams . append ( 't' , cacheKey )
173+
174+ return await import ( url . href ) . then ( ( m ) => m . default ?? m )
175+ } catch ( err ) {
176+ return onError ( id , err )
177+ }
178+ }
179+ }
180+
150181async function loadV4 (
151182 baseDir : string ,
152183 pkgDir : string ,
@@ -172,9 +203,23 @@ async function loadV4(
172203 // Load the design system and set up a compatible context object that is
173204 // usable by the rest of the plugin
174205 let design = await tw . __unstable__loadDesignSystem ( result . css , {
175- loadPlugin ( ) {
176- return ( ) => { }
177- } ,
206+ loadPlugin : createLoader ( {
207+ filepath : entryPoint ,
208+ onError ( id , err ) {
209+ console . error ( `Unable to load plugin: ${ id } ` , err )
210+
211+ return ( ) => { }
212+ } ,
213+ } ) ,
214+
215+ loadConfig : createLoader ( {
216+ filepath : entryPoint ,
217+ onError ( id , err ) {
218+ console . error ( `Unable to load config: ${ id } ` , err )
219+
220+ return { }
221+ } ,
222+ } ) ,
178223 } )
179224
180225 return {
0 commit comments