Support `require.extensions` mutations (#583)
parent
31d20e3a37
commit
d298014070
|
@ -1,5 +1,9 @@
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import { CachedInputFileSystem, ResolverFactory, Resolver, ResolveOptions } from 'enhanced-resolve'
|
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 {
|
function createResolver(options: Partial<ResolveOptions> = {}): Resolver {
|
||||||
return ResolverFactory.createResolver({
|
return ResolverFactory.createResolver({
|
||||||
|
@ -7,18 +11,28 @@ function createResolver(options: Partial<ResolveOptions> = {}): Resolver {
|
||||||
useSyncFileSystemCalls: true,
|
useSyncFileSystemCalls: true,
|
||||||
// cachePredicate: () => false,
|
// cachePredicate: () => false,
|
||||||
conditionNames: ['node', 'require'],
|
conditionNames: ['node', 'require'],
|
||||||
|
extensions,
|
||||||
|
pnpApi,
|
||||||
...options,
|
...options,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let resolver = createResolver()
|
let resolver = createResolver()
|
||||||
|
|
||||||
export function setPnpApi(pnpApi: any): void {
|
export function setPnpApi(newPnpApi: any): void {
|
||||||
resolver = createResolver({ pnpApi })
|
pnpApi = newPnpApi
|
||||||
|
resolver = createResolver()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function resolveFrom(from?: string, id?: string): string {
|
export default function resolveFrom(from?: string, id?: string): string {
|
||||||
if (id.startsWith('\\\\')) return id
|
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)
|
let result = resolver.resolveSync({}, from, id)
|
||||||
if (result === false) throw Error()
|
if (result === false) throw Error()
|
||||||
// https://github.com/webpack/enhanced-resolve/issues/282
|
// https://github.com/webpack/enhanced-resolve/issues/282
|
||||||
|
|
Loading…
Reference in New Issue