Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tsconfig.json detection #770

Merged
merged 2 commits into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/index.js
Expand Up @@ -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: {}
};

Expand Down Expand Up @@ -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;
}
83 changes: 61 additions & 22 deletions 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__;
/******/ })()
Expand Down
40 changes: 5 additions & 35 deletions test/unit/ts-mixed-modules/output.js
Expand Up @@ -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;



/***/ })

/******/ });
Expand All @@ -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: {}
/******/ };
/******/
Expand All @@ -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 + "/";
Expand Down
3 changes: 3 additions & 0 deletions test/unit/ts-target-es2018/input.ts
@@ -0,0 +1,3 @@
if (process?.env.FOO) {
console.log('foo');
}
16 changes: 16 additions & 0 deletions 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');
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TooTallNate Here we can see the optional chaining operator is properly down-leveled 😌


module.exports = __webpack_exports__;
/******/ })()
;
16 changes: 16 additions & 0 deletions 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__;
/******/ })()
;
11 changes: 11 additions & 0 deletions 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"
}
}
48 changes: 4 additions & 44 deletions test/unit/tsconfig-paths-allowjs/output.js
Expand Up @@ -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 + "/";
Expand All @@ -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);

})();

Expand Down
48 changes: 4 additions & 44 deletions test/unit/tsconfig-paths-conflicting-external/output.js
Expand Up @@ -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 + "/";
Expand All @@ -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);

})();

Expand Down