From b0c63dfaf701dc11d2f9cbb2cef5079efe7402e7 Mon Sep 17 00:00:00 2001 From: qwqcode <22412567+qwqcode@users.noreply.github.com> Date: Thu, 15 Aug 2024 00:52:59 +0800 Subject: [PATCH] fix: normalize typescript lib path for rollup (#366) * fix: normalize typescript lib path for rollup * chore: add tests * add existsSync test --- src/utils.ts | 7 ++++--- tests/utils.spec.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 738f703..b8e33fd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -254,9 +254,10 @@ export function getTsLibFolder({ root, entryRoot }: { root: string, entryRoot: s try { // try the `require.resolve` method first // @see https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules - libFolder = createRequire(import.meta.url) - .resolve('typescript') - .replace(/node_modules\/typescript.*/, 'node_modules/typescript') + libFolder = normalizePath(createRequire(import.meta.url).resolve('typescript')).replace( + /node_modules\/typescript.*/, + 'node_modules/typescript' + ) } catch { // fallback to legacy path method libFolder = resolve(root, 'node_modules/typescript') diff --git a/tests/utils.spec.ts b/tests/utils.spec.ts index 5447ec1..3f53653 100644 --- a/tests/utils.spec.ts +++ b/tests/utils.spec.ts @@ -2,12 +2,14 @@ /* eslint-disable promise/param-names */ import { normalize, resolve } from 'node:path' +import { existsSync } from 'node:fs' import { describe, expect, it } from 'vitest' import { base64VLQEncode, ensureAbsolute, ensureArray, + getTsLibFolder, isNativeObj, isPromise, isRegExp, @@ -171,4 +173,19 @@ describe('utils tests', () => { expect(toCapitalCase('-aa bb cc ')).toEqual('AaBbCc') expect(toCapitalCase(' -aa bb cc -')).toEqual('AaBbCc') }) + + it('test: getTsLibFolder', () => { + const root = normalizePath(resolve(__dirname, '..')) + const entryRoot = resolve(root, 'src') + + expect( + getTsLibFolder({ root, entryRoot }) + ).toMatch(/node_modules\/typescript$/) + + expect( + existsSync( + getTsLibFolder({ root, entryRoot }) || '' + ) + ).toBe(true) + }) })