Support `require.extensions` mutations (#583)

master
Brad Cornes 2022-07-25 16:28:54 +01:00 committed by GitHub
parent 31d20e3a37
commit d298014070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 2 deletions

View File

@ -1,5 +1,9 @@
import * as fs from 'fs'
import { CachedInputFileSystem, ResolverFactory, Resolver, ResolveOptions } from 'enhanced-resolve'
import { equal } from 'tailwindcss-language-service/src/util/array'
let pnpApi: any
let extensions = Object.keys(require.extensions)
function createResolver(options: Partial<ResolveOptions> = {}): Resolver {
return ResolverFactory.createResolver({
@ -7,18 +11,28 @@ function createResolver(options: Partial<ResolveOptions> = {}): Resolver {
useSyncFileSystemCalls: true,
// cachePredicate: () => false,
conditionNames: ['node', 'require'],
extensions,
pnpApi,
...options,
})
}
let resolver = createResolver()
export function setPnpApi(pnpApi: any): void {
resolver = createResolver({ pnpApi })
export function setPnpApi(newPnpApi: any): void {
pnpApi = newPnpApi
resolver = createResolver()
}
export default function resolveFrom(from?: string, id?: string): string {
if (id.startsWith('\\\\')) return id
let newExtensions = Object.keys(require.extensions)
if (!equal(newExtensions, extensions)) {
extensions = newExtensions
resolver = createResolver()
}
let result = resolver.resolveSync({}, from, id)
if (result === false) throw Error()
// https://github.com/webpack/enhanced-resolve/issues/282