From d298014070d7298573222a17a917da802ae3cd0d Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Mon, 25 Jul 2022 16:28:54 +0100 Subject: [PATCH] Support `require.extensions` mutations (#583) --- .../src/util/resolveFrom.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss-language-server/src/util/resolveFrom.ts b/packages/tailwindcss-language-server/src/util/resolveFrom.ts index 5c13995..b390fd4 100644 --- a/packages/tailwindcss-language-server/src/util/resolveFrom.ts +++ b/packages/tailwindcss-language-server/src/util/resolveFrom.ts @@ -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 = {}): Resolver { return ResolverFactory.createResolver({ @@ -7,18 +11,28 @@ function createResolver(options: Partial = {}): 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