Skip to content

Commit 2d46805

Browse files
committed
Refactor
1 parent 15e8009 commit 2d46805

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/config.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @ts-check
22
import * as fs from 'fs/promises'
3-
import { createRequire } from 'module'
43
import * as path from 'path'
54
import { pathToFileURL } from 'url'
65
import clearModule from 'clear-module'
@@ -21,8 +20,6 @@ import { expiringMap } from './expiring-map.js'
2120
import { resolveFrom, resolveIn } from './resolve'
2221
import type { ContextContainer } from './types'
2322

24-
let localRequire = createRequire(import.meta.url)
25-
2623
let sourceToPathMap = new Map<string, string | null>()
2724
let sourceToEntryMap = new Map<string, string | null>()
2825
let pathToContextMap = expiringMap<string | null, ContextContainer>(10_000)
@@ -151,9 +148,6 @@ async function loadTailwindConfig(
151148
* Create a loader function that can load plugins and config files relative to
152149
* the CSS file that uses them. However, we don't want missing files to prevent
153150
* everything from working so we'll let the error handler decide how to proceed.
154-
*
155-
* @param {object} param0
156-
* @returns
157151
*/
158152
function createLoader<T>({
159153
filepath,
@@ -162,12 +156,11 @@ function createLoader<T>({
162156
filepath: string
163157
onError: (id: string, error: unknown) => T
164158
}) {
165-
let baseDir = path.dirname(filepath)
166159
let cacheKey = `${+Date.now()}`
167160

168-
return async function loadFile(id: string) {
161+
async function loadFile(id: string, base: string) {
169162
try {
170-
let resolved = resolveFrom(baseDir, id)
163+
let resolved = resolveFrom(base, id)
171164
let url = pathToFileURL(resolved)
172165
url.searchParams.append('t', cacheKey)
173166

@@ -176,6 +169,9 @@ function createLoader<T>({
176169
return onError(id, err)
177170
}
178171
}
172+
173+
let baseDir = path.dirname(filepath)
174+
return (id: string) => loadFile(id, baseDir)
179175
}
180176

181177
async function loadV4(
@@ -199,10 +195,11 @@ async function loadV4(
199195
let css = await fs.readFile(entryPoint, 'utf-8')
200196
let resolveImports = postcss([postcssImport()])
201197
let result = await resolveImports.process(css, { from: entryPoint })
198+
css = result.css
202199

203200
// Load the design system and set up a compatible context object that is
204201
// usable by the rest of the plugin
205-
let design = await tw.__unstable__loadDesignSystem(result.css, {
202+
let design = await tw.__unstable__loadDesignSystem(css, {
206203
loadPlugin: createLoader({
207204
filepath: entryPoint,
208205
onError(id, err) {

0 commit comments

Comments
 (0)