Skip to content

Commit

Permalink
fix: normalize typescript lib path for rollup (#366)
Browse files Browse the repository at this point in the history
* fix: normalize typescript lib path for rollup

* chore: add tests

* add existsSync test
  • Loading branch information
qwqcode authored Aug 14, 2024
1 parent 2986651 commit b0c63df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 17 additions & 0 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
})

0 comments on commit b0c63df

Please # to comment.