diff --git a/src/index.js b/src/index.js index fd60391e..f7dff022 100644 --- a/src/index.js +++ b/src/index.js @@ -111,8 +111,12 @@ function ncc ( // error if there's no tsconfig in the working directory let fullTsconfig = {}; try { - const tsconfig = tsconfigPaths.loadConfig(); - fullTsconfig = loadTsconfig(tsconfig.configFileAbsolutePath) || { + const configFileAbsolutePath = walkParentDirs({ + base: process.cwd(), + start: dirname(entry), + filename: 'tsconfig.json', + }); + fullTsconfig = loadTsconfig(configFileAbsolutePath) || { compilerOptions: {} }; @@ -643,3 +647,25 @@ function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") { } } } + +// Adapted from https://github.com/vercel/vercel/blob/18bec983aefbe2a77bd14eda6fca59ff7e956d8b/packages/build-utils/src/fs/run-user-scripts.ts#L289-L310 +function walkParentDirs({ + base, + start, + filename, +}) { + let parent = ''; + + for (let current = start; base.length <= current.length; current = parent) { + const fullPath = join(current, filename); + + // eslint-disable-next-line no-await-in-loop + if (fs.existsSync(fullPath)) { + return fullPath; + } + + parent = dirname(current); + } + + return null; +} diff --git a/test/unit/ts-exts/output.js b/test/unit/ts-exts/output.js index df9717a3..bff675cc 100644 --- a/test/unit/ts-exts/output.js +++ b/test/unit/ts-exts/output.js @@ -1,38 +1,77 @@ /******/ (() => { // webpackBootstrap /******/ "use strict"; -/******/ // The require scope -/******/ var __nccwpck_require__ = {}; -/******/ +/******/ var __webpack_modules__ = ({ + +/***/ 668: +/***/ ((__unused_webpack_module, exports) => { + + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.default = {}; + + +/***/ }), + +/***/ 975: +/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { + + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.default = void 0; +var dep_dep_js_1 = __nccwpck_require__(668); +Object.defineProperty(exports, "default", ({ enumerable: true, get: function () { return dep_dep_js_1.default; } })); + + +/***/ }) + +/******/ }); /************************************************************************/ -/******/ /* webpack/runtime/make namespace object */ -/******/ (() => { -/******/ // define __esModule on exports -/******/ __nccwpck_require__.r = (exports) => { -/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { -/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); -/******/ } -/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} /******/ }; -/******/ })(); /******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ /******/ /* webpack/runtime/compat */ /******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; /******/ /************************************************************************/ var __webpack_exports__ = {}; -// ESM COMPAT FLAG -__nccwpck_require__.r(__webpack_exports__); - -;// CONCATENATED MODULE: ./test/unit/ts-exts/dep-dep.ts -/* harmony default export */ const dep_dep = ({}); - -;// CONCATENATED MODULE: ./test/unit/ts-exts/dep.ts - +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +var exports = __webpack_exports__; -;// CONCATENATED MODULE: ./test/unit/ts-exts/input.ts +Object.defineProperty(exports, "__esModule", ({ value: true })); +const dep_js_1 = __nccwpck_require__(975); +console.log(dep_js_1.default); -console.log(dep_dep); +})(); module.exports = __webpack_exports__; /******/ })() diff --git a/test/unit/ts-mixed-modules/output.js b/test/unit/ts-mixed-modules/output.js index 55afe872..7f0260f3 100644 --- a/test/unit/ts-mixed-modules/output.js +++ b/test/unit/ts-mixed-modules/output.js @@ -3,17 +3,16 @@ /******/ var __webpack_modules__ = ({ /***/ 119: -/***/ ((module, __webpack_exports__, __nccwpck_require__) => { +/***/ ((module, exports) => { -__nccwpck_require__.r(__webpack_exports__); -/* module decorator */ module = __nccwpck_require__.hmd(module); + +Object.defineProperty(exports, "__esModule", ({ value: true })); const config = { routes: ['/foo'] }; module.exports = config; - /***/ }) /******/ }); @@ -30,8 +29,8 @@ module.exports = config; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { -/******/ id: moduleId, -/******/ loaded: false, +/******/ // no module.id needed +/******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ @@ -44,40 +43,11 @@ module.exports = config; /******/ if(threw) delete __webpack_module_cache__[moduleId]; /******/ } /******/ -/******/ // Flag the module as loaded -/******/ module.loaded = true; -/******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ -/******/ /* webpack/runtime/harmony module decorator */ -/******/ (() => { -/******/ __nccwpck_require__.hmd = (module) => { -/******/ module = Object.create(module); -/******/ if (!module.children) module.children = []; -/******/ Object.defineProperty(module, 'exports', { -/******/ enumerable: true, -/******/ set: () => { -/******/ throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id); -/******/ } -/******/ }); -/******/ return module; -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/make namespace object */ -/******/ (() => { -/******/ // define __esModule on exports -/******/ __nccwpck_require__.r = (exports) => { -/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { -/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); -/******/ } -/******/ Object.defineProperty(exports, '__esModule', { value: true }); -/******/ }; -/******/ })(); -/******/ /******/ /* webpack/runtime/compat */ /******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; diff --git a/test/unit/ts-target-es2018/input.ts b/test/unit/ts-target-es2018/input.ts new file mode 100644 index 00000000..9bdef1d3 --- /dev/null +++ b/test/unit/ts-target-es2018/input.ts @@ -0,0 +1,3 @@ +if (process?.env.FOO) { + console.log('foo'); +} \ No newline at end of file diff --git a/test/unit/ts-target-es2018/output-coverage.js b/test/unit/ts-target-es2018/output-coverage.js new file mode 100644 index 00000000..5f2aaf14 --- /dev/null +++ b/test/unit/ts-target-es2018/output-coverage.js @@ -0,0 +1,16 @@ +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; + +if (process === null || process === void 0 ? void 0 : process.env.FOO) { + console.log('foo'); +} + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/test/unit/ts-target-es2018/output.js b/test/unit/ts-target-es2018/output.js new file mode 100644 index 00000000..5f2aaf14 --- /dev/null +++ b/test/unit/ts-target-es2018/output.js @@ -0,0 +1,16 @@ +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; + +if (process === null || process === void 0 ? void 0 : process.env.FOO) { + console.log('foo'); +} + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/test/unit/ts-target-es2018/tsconfig.json b/test/unit/ts-target-es2018/tsconfig.json new file mode 100644 index 00000000..eae3de46 --- /dev/null +++ b/test/unit/ts-target-es2018/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "lib": ["esnext"], + "module": "commonjs", + "moduleResolution": "node", + "outDir": "dist", + "strict": true, + "target": "es2018" + } + } \ No newline at end of file diff --git a/test/unit/tsconfig-paths-allowjs/output.js b/test/unit/tsconfig-paths-allowjs/output.js index bc41a94c..f7da7944 100644 --- a/test/unit/tsconfig-paths-allowjs/output.js +++ b/test/unit/tsconfig-paths-allowjs/output.js @@ -42,46 +42,6 @@ module.exports = eval("require")("@module"); /******/ } /******/ /************************************************************************/ -/******/ /* webpack/runtime/compat get default export */ -/******/ (() => { -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __nccwpck_require__.n = (module) => { -/******/ var getter = module && module.__esModule ? -/******/ () => (module['default']) : -/******/ () => (module); -/******/ __nccwpck_require__.d(getter, { a: getter }); -/******/ return getter; -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/define property getters */ -/******/ (() => { -/******/ // define getter functions for harmony exports -/******/ __nccwpck_require__.d = (exports, definition) => { -/******/ for(var key in definition) { -/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { -/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); -/******/ } -/******/ } -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/hasOwnProperty shorthand */ -/******/ (() => { -/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) -/******/ })(); -/******/ -/******/ /* webpack/runtime/make namespace object */ -/******/ (() => { -/******/ // define __esModule on exports -/******/ __nccwpck_require__.r = (exports) => { -/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { -/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); -/******/ } -/******/ Object.defineProperty(exports, '__esModule', { value: true }); -/******/ }; -/******/ })(); -/******/ /******/ /* webpack/runtime/compat */ /******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; @@ -91,11 +51,11 @@ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be in strict mode. (() => { "use strict"; -__nccwpck_require__.r(__webpack_exports__); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(17); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_module__WEBPACK_IMPORTED_MODULE_0__); +var exports = __webpack_exports__; -console.log((_module__WEBPACK_IMPORTED_MODULE_0___default())); +Object.defineProperty(exports, "__esModule", ({ value: true })); +const _module_1 = __nccwpck_require__(17); +console.log(_module_1.default); })(); diff --git a/test/unit/tsconfig-paths-conflicting-external/output.js b/test/unit/tsconfig-paths-conflicting-external/output.js index bc41a94c..f7da7944 100644 --- a/test/unit/tsconfig-paths-conflicting-external/output.js +++ b/test/unit/tsconfig-paths-conflicting-external/output.js @@ -42,46 +42,6 @@ module.exports = eval("require")("@module"); /******/ } /******/ /************************************************************************/ -/******/ /* webpack/runtime/compat get default export */ -/******/ (() => { -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __nccwpck_require__.n = (module) => { -/******/ var getter = module && module.__esModule ? -/******/ () => (module['default']) : -/******/ () => (module); -/******/ __nccwpck_require__.d(getter, { a: getter }); -/******/ return getter; -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/define property getters */ -/******/ (() => { -/******/ // define getter functions for harmony exports -/******/ __nccwpck_require__.d = (exports, definition) => { -/******/ for(var key in definition) { -/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { -/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); -/******/ } -/******/ } -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/hasOwnProperty shorthand */ -/******/ (() => { -/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) -/******/ })(); -/******/ -/******/ /* webpack/runtime/make namespace object */ -/******/ (() => { -/******/ // define __esModule on exports -/******/ __nccwpck_require__.r = (exports) => { -/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { -/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); -/******/ } -/******/ Object.defineProperty(exports, '__esModule', { value: true }); -/******/ }; -/******/ })(); -/******/ /******/ /* webpack/runtime/compat */ /******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; @@ -91,11 +51,11 @@ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be in strict mode. (() => { "use strict"; -__nccwpck_require__.r(__webpack_exports__); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(17); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_module__WEBPACK_IMPORTED_MODULE_0__); +var exports = __webpack_exports__; -console.log((_module__WEBPACK_IMPORTED_MODULE_0___default())); +Object.defineProperty(exports, "__esModule", ({ value: true })); +const _module_1 = __nccwpck_require__(17); +console.log(_module_1.default); })(); diff --git a/test/unit/tsconfig-paths/output.js b/test/unit/tsconfig-paths/output.js index bc41a94c..f7da7944 100644 --- a/test/unit/tsconfig-paths/output.js +++ b/test/unit/tsconfig-paths/output.js @@ -42,46 +42,6 @@ module.exports = eval("require")("@module"); /******/ } /******/ /************************************************************************/ -/******/ /* webpack/runtime/compat get default export */ -/******/ (() => { -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __nccwpck_require__.n = (module) => { -/******/ var getter = module && module.__esModule ? -/******/ () => (module['default']) : -/******/ () => (module); -/******/ __nccwpck_require__.d(getter, { a: getter }); -/******/ return getter; -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/define property getters */ -/******/ (() => { -/******/ // define getter functions for harmony exports -/******/ __nccwpck_require__.d = (exports, definition) => { -/******/ for(var key in definition) { -/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { -/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); -/******/ } -/******/ } -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/hasOwnProperty shorthand */ -/******/ (() => { -/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) -/******/ })(); -/******/ -/******/ /* webpack/runtime/make namespace object */ -/******/ (() => { -/******/ // define __esModule on exports -/******/ __nccwpck_require__.r = (exports) => { -/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { -/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); -/******/ } -/******/ Object.defineProperty(exports, '__esModule', { value: true }); -/******/ }; -/******/ })(); -/******/ /******/ /* webpack/runtime/compat */ /******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; @@ -91,11 +51,11 @@ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be in strict mode. (() => { "use strict"; -__nccwpck_require__.r(__webpack_exports__); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(17); -/* harmony import */ var _module__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_module__WEBPACK_IMPORTED_MODULE_0__); +var exports = __webpack_exports__; -console.log((_module__WEBPACK_IMPORTED_MODULE_0___default())); +Object.defineProperty(exports, "__esModule", ({ value: true })); +const _module_1 = __nccwpck_require__(17); +console.log(_module_1.default); })();