| 34 | } |
| 35 | |
| 36 | function createCustomResolver( |
| 37 | resolvers: ((id: string, importer: string) => Promise<string | undefined>)[], |
| 38 | filter = (_path: string) => true, |
| 39 | ) { |
| 40 | return async (id: string, base: string) => { |
| 41 | // The resolver expects an `importer` file. We don't really know where the |
| 42 | // current `id` was imported from, but Vite will essentially do a |
| 43 | // `path.dirname(importer)` so it doesn't really matter. |
| 44 | // |
| 45 | // It does matter that this is a file, otherwise we would go up a directory, |
| 46 | // which means that we would be resolving files from a parent folder first, |
| 47 | // instead of the current folder we are in. |
| 48 | let importer = path.resolve(base, '__placeholder__.ts') |
| 49 | |
| 50 | for (let resolver of resolvers) { |
| 51 | let resolved = await resolver(id, importer) |
| 52 | |
| 53 | // If we didn't resolve, we don't have to bail immediately, but we can try |
| 54 | // the next resolver |
| 55 | if (!resolved) continue |
| 56 | |
| 57 | if (resolved === id) continue |
| 58 | |
| 59 | // Looks like a relative file, let's resolve it to an absolute path |
| 60 | if (resolved[0] === '.') resolved = path.resolve(base, resolved) |
| 61 | |
| 62 | // Must adhere to additional filters (e.g.: must be a .css file) |
| 63 | if (!filter(resolved)) continue |
| 64 | |
| 65 | // If it's not an absolute path, then we don't really know how to read |
| 66 | // the file from disk. |
| 67 | if (!path.isAbsolute(resolved)) continue |
| 68 | |
| 69 | return resolved |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | export default function tailwindcss(opts: PluginOptions = {}): Plugin[] { |
| 75 | let servers: ViteDevServer[] = [] |