From a7d819c26c3f6b79cdc5414736d720e80c7a2ed7 Mon Sep 17 00:00:00 2001 From: JJordan0C <69581081+JJordan0C@users.noreply.github.com> Date: Fri, 14 Aug 2026 10:30:20 +0200 Subject: [PATCH] fix: support TypeScript project references --- src/index.js | 3 +++ test/unit.test.js | 10 +++++---- test/unit/ts-project-references/app/input.ts | 3 +++ .../ts-project-references/app/tsconfig.json | 12 +++++++++++ test/unit/ts-project-references/lib/index.ts | 3 +++ .../ts-project-references/lib/tsconfig.json | 6 ++++++ test/unit/ts-project-references/opt.json | 4 ++++ .../ts-project-references/output-coverage.js | 21 +++++++++++++++++++ test/unit/ts-project-references/output.js | 21 +++++++++++++++++++ 9 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/unit/ts-project-references/app/input.ts create mode 100644 test/unit/ts-project-references/app/tsconfig.json create mode 100644 test/unit/ts-project-references/lib/index.ts create mode 100644 test/unit/ts-project-references/lib/tsconfig.json create mode 100644 test/unit/ts-project-references/opt.json create mode 100644 test/unit/ts-project-references/output-coverage.js create mode 100644 test/unit/ts-project-references/output.js diff --git a/src/index.js b/src/index.js index 8db368791..880d3bf3c 100644 --- a/src/index.js +++ b/src/index.js @@ -369,6 +369,9 @@ function ncc ( module: 'esnext', target: 'esnext', ...fullTsconfig.compilerOptions, + // ncc bundles the root project and its dependencies as one compilation. + // Disable composite's implicit rootDir so referenced sources are included. + composite: false, allowSyntheticDefaultImports: true, noEmit: false, outDir: '//' diff --git a/test/unit.test.js b/test/unit.test.js index e01ca242a..07720192d 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -26,11 +26,13 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) { opts = JSON.parse(opts); } catch (_) {} - // set env variable so tsconfig-paths can find the config - process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`; // find the name of the input file (e.g input.ts) - const inputFile = fs.readdirSync(testDir).find(file => file.includes("input")); - await ncc(`${testDir}/${inputFile}`, Object.assign({ + const rootInput = fs.readdirSync(testDir).find(file => file.includes("input")); + const inputDir = rootInput ? testDir : `${testDir}/app`; + const inputFile = rootInput || fs.readdirSync(inputDir).find(file => file.includes("input")); + // set env variable so tsconfig-paths can find the config + process.env.TS_NODE_PROJECT = `${inputDir}/tsconfig.json`; + await ncc(`${inputDir}/${inputFile}`, Object.assign({ assetBuilds: true, transpileOnly: true, customEmit (path) { diff --git a/test/unit/ts-project-references/app/input.ts b/test/unit/ts-project-references/app/input.ts new file mode 100644 index 000000000..20a97dc70 --- /dev/null +++ b/test/unit/ts-project-references/app/input.ts @@ -0,0 +1,3 @@ +import { hello } from '../lib/index'; + +console.log(hello()); diff --git a/test/unit/ts-project-references/app/tsconfig.json b/test/unit/ts-project-references/app/tsconfig.json new file mode 100644 index 000000000..8ca9a2ef8 --- /dev/null +++ b/test/unit/ts-project-references/app/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "composite": true, + "outDir": "dist" + }, + "include": ["input.ts"], + "references": [ + { + "path": "../lib" + } + ] +} diff --git a/test/unit/ts-project-references/lib/index.ts b/test/unit/ts-project-references/lib/index.ts new file mode 100644 index 000000000..64884ae96 --- /dev/null +++ b/test/unit/ts-project-references/lib/index.ts @@ -0,0 +1,3 @@ +export function hello() { + return 'Hello, World!'; +} diff --git a/test/unit/ts-project-references/lib/tsconfig.json b/test/unit/ts-project-references/lib/tsconfig.json new file mode 100644 index 000000000..679a56a61 --- /dev/null +++ b/test/unit/ts-project-references/lib/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "composite": true, + "outDir": "dist" + } +} diff --git a/test/unit/ts-project-references/opt.json b/test/unit/ts-project-references/opt.json new file mode 100644 index 000000000..548f583b4 --- /dev/null +++ b/test/unit/ts-project-references/opt.json @@ -0,0 +1,4 @@ +{ + "cache": false, + "transpileOnly": false +} diff --git a/test/unit/ts-project-references/output-coverage.js b/test/unit/ts-project-references/output-coverage.js new file mode 100644 index 000000000..113adba41 --- /dev/null +++ b/test/unit/ts-project-references/output-coverage.js @@ -0,0 +1,21 @@ +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; + +;// CONCATENATED MODULE: ./test/unit/ts-project-references/lib/index.ts +function hello() { + return 'Hello, World!'; +} + +;// CONCATENATED MODULE: ./test/unit/ts-project-references/app/input.ts + +console.log(hello()); + +module.exports = __webpack_exports__; +/******/ })() +; diff --git a/test/unit/ts-project-references/output.js b/test/unit/ts-project-references/output.js new file mode 100644 index 000000000..113adba41 --- /dev/null +++ b/test/unit/ts-project-references/output.js @@ -0,0 +1,21 @@ +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; + +;// CONCATENATED MODULE: ./test/unit/ts-project-references/lib/index.ts +function hello() { + return 'Hello, World!'; +} + +;// CONCATENATED MODULE: ./test/unit/ts-project-references/app/input.ts + +console.log(hello()); + +module.exports = __webpack_exports__; +/******/ })() +;