diff --git a/src/index.js b/src/index.js index 2a9be4c2..5ea3f6bb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const resolve = require("resolve"); const fs = require("graceful-fs"); const crypto = require("crypto"); -const { join, dirname, extname, relative } = require("path"); +const { join, dirname, extname, relative, resolve: pathResolve } = require("path"); const webpack = require("webpack"); const MemoryFS = require("memory-fs"); const terser = require("terser"); @@ -51,7 +51,7 @@ function ncc ( debugLog = false, transpileOnly = false, license = '', - target + target, } = {} ) { process.env.__NCC_OPTS = JSON.stringify({ @@ -86,9 +86,12 @@ function ncc ( // add TsconfigPathsPlugin to support `paths` resolution in tsconfig // we need to catch here because the plugin will // error if there's no tsconfig in the working directory + let fullTsconfig; try { const tsconfig = tsconfigPaths.loadConfig(); - const fullTsconfig = loadTsconfig(tsconfig.configFileAbsolutePath) + fullTsconfig = loadTsconfig(tsconfig.configFileAbsolutePath) || { + compilerOptions: {} + }; const tsconfigPathsOptions = { silent: true } if (fullTsconfig.compilerOptions.allowJs) { @@ -353,7 +356,7 @@ function ncc ( async function finalizeHandler (stats) { const assets = Object.create(null); - getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta); + getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, fullTsconfig); // filter symlinks to existing assets const symlinks = Object.create(null); for (const [key, value] of Object.entries(relocateLoader.getSymlinks())) { @@ -513,15 +516,21 @@ function ncc ( } // this could be rewritten with actual FS apis / globs, but this is simpler -function getFlatFiles(mfsData, output, getAssetMeta, curBase = "") { +function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") { for (const path of Object.keys(mfsData)) { const item = mfsData[path]; - const curPath = `${curBase}/${path}`; + let curPath = `${curBase}/${path}`; // directory - if (item[""] === true) getFlatFiles(item, output, getAssetMeta, curPath); + if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfig, curPath); // file else if (!curPath.endsWith("/")) { const meta = getAssetMeta(curPath.substr(1)) || {}; + if(curPath.endsWith(".d.ts")) { + const outDir = tsconfig.compilerOptions.outDir ? pathResolve(tsconfig.compilerOptions.outDir) : pathResolve('dist'); + curPath = curPath + .replace(outDir, "") + .replace(process.cwd(), "") + } output[curPath.substr(1)] = { source: mfsData[path], permissions: meta.permissions diff --git a/test/unit/ts-decl-dir/input.js b/test/unit/ts-decl-dir/input.js new file mode 100644 index 00000000..40caa795 --- /dev/null +++ b/test/unit/ts-decl-dir/input.js @@ -0,0 +1 @@ +export * from './test.ts'; diff --git a/test/unit/ts-decl-dir/output-coverage.js b/test/unit/ts-decl-dir/output-coverage.js new file mode 100644 index 00000000..721cc0c7 --- /dev/null +++ b/test/unit/ts-decl-dir/output-coverage.js @@ -0,0 +1,101 @@ +module.exports = +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ var __webpack_modules__ = ({ + +/***/ 676: +/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "__esModule": () => /* reexport safe */ _test_ts__WEBPACK_IMPORTED_MODULE_0__.X, +/* harmony export */ "test": () => /* reexport safe */ _test_ts__WEBPACK_IMPORTED_MODULE_0__.B +/* harmony export */ }); +/* harmony import */ var _test_ts__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(867); + + + +/***/ }), + +/***/ 867: +/***/ ((__unused_webpack_module, exports) => { + + +exports.X = true; +function test(arg) { + return arg; +} +exports.B = test; + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ if(__webpack_module_cache__[moduleId]) { +/******/ return __webpack_module_cache__[moduleId].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, __webpack_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_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 */ +/******/ +/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/ +/******/ // module exports must be returned from runtime so entry inlining is disabled +/******/ // startup +/******/ // Load entry module and return exports +/******/ return __webpack_require__(676); +/******/ })() +; \ No newline at end of file diff --git a/test/unit/ts-decl-dir/output.js b/test/unit/ts-decl-dir/output.js new file mode 100644 index 00000000..a16fe9d4 --- /dev/null +++ b/test/unit/ts-decl-dir/output.js @@ -0,0 +1,101 @@ +module.exports = +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ var __webpack_modules__ = ({ + +/***/ 345: +/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "__esModule": () => /* reexport safe */ _test_ts__WEBPACK_IMPORTED_MODULE_0__.X, +/* harmony export */ "test": () => /* reexport safe */ _test_ts__WEBPACK_IMPORTED_MODULE_0__.B +/* harmony export */ }); +/* harmony import */ var _test_ts__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(57); + + + +/***/ }), + +/***/ 57: +/***/ ((__unused_webpack_module, exports) => { + + +exports.X = true; +function test(arg) { + return arg; +} +exports.B = test; + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ if(__webpack_module_cache__[moduleId]) { +/******/ return __webpack_module_cache__[moduleId].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, __webpack_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_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 */ +/******/ +/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/ +/******/ // module exports must be returned from runtime so entry inlining is disabled +/******/ // startup +/******/ // Load entry module and return exports +/******/ return __webpack_require__(345); +/******/ })() +; \ No newline at end of file diff --git a/test/unit/ts-decl-dir/test.ts b/test/unit/ts-decl-dir/test.ts new file mode 100644 index 00000000..5436fc69 --- /dev/null +++ b/test/unit/ts-decl-dir/test.ts @@ -0,0 +1,3 @@ +export function test (arg: string): string { + return arg; +} \ No newline at end of file diff --git a/test/unit/ts-decl-dir/tsconfig.json b/test/unit/ts-decl-dir/tsconfig.json new file mode 100644 index 00000000..2a668ec3 --- /dev/null +++ b/test/unit/ts-decl-dir/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "outDir": "dist", + "declaration": true, + "declarationDir": "dist/types" + } +} \ No newline at end of file