Repository navigation
fix: fall back to bundled TypeScript when typescript is not installed - #1346
Open
SammyTourani wants to merge 1 commit into
Open
SammyTourani wants to merge 1 commit into
SammyTourani wants to merge 1 commit into
Conversation
Since 0.25.0 the published dist/ncc/typescript.js falls back to a bare
require('typescript'), which fails when no typescript package can be
resolved from ncc's install location (e.g. a global install), even though
loaders/ts-loader.js bundles a TypeScript compiler. Fall back to that
bundled compiler again, and copy its default lib .d.ts files to
dist/ncc/loaders/, where TypeScript 5 looks for them.
Fixes vercel#1264
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1264
src/typescript.js: when there's no project-localtypescript, fall back torequire('./loaders/ts-loader.js').typescript. This is the same fallbackdist/ncc/typescript.jshad up to 0.24.1.dist/, this is the compiler bundled intoloaders/ts-loader.js. Webpack has already loaded that module as the loader, so it isn't evaluated a second time.src/(coverage runs),src/loaders/ts-loader.jsre-exports thetypescriptdevDependency, so behaviour there doesn't change.scripts/build.js: copy TypeScript's lib.d.tsfiles todist/ncc/loaders/instead ofdist/ncc/loaders/typescript/lib/.getDefaultLibFilePath()looks for them next tosys.getExecutingFilePath(), which for the bundled compiler isdist/ncc/loaders/ts-loader.js.TS6053: File '.../dist/ncc/loaders/lib.es6.d.ts' not found.test/cli.test.js: a new test copiesdist/nccto a temp directory outside the repo, sotypescriptcan't be resolved from ncc's location (as with a global install).test/integration/test.tswithTYPESCRIPT_LOOKUP_PATHpointing nowhere and expects the build to succeed with the built-in compiler.require('typescript')finds the repo's ownnode_modules/typescript.src/, where there is no bundle.A project's own
typescriptis still used first. This only changes what happens when there isn't one.Verification
Environment: macOS, Node 26.3.1. Setup:
pnpm install --frozen-lockfile,pnpm build-test-binary,pnpm build.Command:
node --expose-gc --max_old_space_size=4096 node_modules/jest/bin/jest.js test/cli.test.js -t "built-in typescript when typescript is not installed"Result:
main, the test fails with the issue's error (Could not load TypeScript compiler with NPM package name .../typescript.js).src/typescript.jschange, it fails withTS6053: File '.../loaders/lib.es6.d.ts' not found.Tests: 23 skipped, 1 passed, 24 total).Command:
pnpm testResult:
Test Suites: 4 passed, 4 total,Tests: 122 passed, 122 total.mainplus only the new test:Tests: 1 failed, 121 passed, 122 total. The only failure is the new test.Tests: 1 failed, 121 passed, 122 total. The failure wastest/watcher.test.js › Should support custom watch APIexceeding its 30 s timeout. That test watch-builds twilio, with no TypeScript involved. It passed when run alone (24.8 s) and in the run above. Onmainit took 23.7 s of its 30 s budget.Command:
node --expose-gc --max_old_space_size=4096 node_modules/jest/bin/jest.js --runInBand --coverage --globals '{"coverage":true}' test/cli.test.js(runs the CLI tests fromsrc/)Result:
Tests: 23 passed, 23 totalManual check of the reported scenario: I packed the build with
npm packand installed it withnpm install -g --prefix <tmp>, in a directory tree with notypescriptpackage anywhere."type": "module"project (the issue's case),ncc run src/index.tsprintsncc: Using typescript@5.9.3 (ncc built-in)and runs the program. The published 0.45.0 fails in the same setup with the issue's error.TS2322, exit code 1).-tstill works, and--quietstill prints nothing.typescript@5.4.5still uses it (ncc: Using typescript@5.4.5 (local user-provided)).