@@ -18,7 +18,7 @@ import loadConfigFallback from 'tailwindcss/loadConfig'
18
18
import resolveConfigFallback from 'tailwindcss/resolveConfig'
19
19
import type { RequiredConfig } from 'tailwindcss/types/config.js'
20
20
import { expiringMap } from './expiring-map.js'
21
- import { resolveIn } from './resolve'
21
+ import { resolveFrom , resolveIn } from './resolve'
22
22
import type { ContextContainer } from './types'
23
23
24
24
let localRequire = createRequire ( import . meta. url )
@@ -147,6 +147,37 @@ async function loadTailwindConfig(
147
147
}
148
148
}
149
149
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
+
150
181
async function loadV4 (
151
182
baseDir : string ,
152
183
pkgDir : string ,
@@ -172,9 +203,23 @@ async function loadV4(
172
203
// Load the design system and set up a compatible context object that is
173
204
// usable by the rest of the plugin
174
205
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
+ } ) ,
178
223
} )
179
224
180
225
return {
0 commit comments