Skip to content

Commit

Permalink
fix: correct declaration output dir (#627)
Browse files Browse the repository at this point in the history
If declarationDir is special, the output dir will be unexpected, fixes #481, and I run typescript example will cause same error as follow
![image](https://user-images.githubusercontent.com/32335736/102162314-ec5d1480-3ec3-11eb-91e5-b45afab4211c.png)

`tsconfig.json` as follow
![image](https://user-images.githubusercontent.com/32335736/102162468-334b0a00-3ec4-11eb-835a-789bff388307.png)

I give a quick fix, rewrite dir before write file into disk, seems can fix the problem.
  • Loading branch information
zeroooooooo authored Jan 11, 2021
1 parent b4f9b42 commit d175973
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -51,7 +51,7 @@ function ncc (
debugLog = false,
transpileOnly = false,
license = '',
target
target,
} = {}
) {
process.env.__NCC_OPTS = JSON.stringify({
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/unit/ts-decl-dir/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './test.ts';
101 changes: 101 additions & 0 deletions test/unit/ts-decl-dir/output-coverage.js
Original file line number Diff line number Diff line change
@@ -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);
/******/ })()
;
101 changes: 101 additions & 0 deletions test/unit/ts-decl-dir/output.js
Original file line number Diff line number Diff line change
@@ -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);
/******/ })()
;
3 changes: 3 additions & 0 deletions test/unit/ts-decl-dir/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function test (arg: string): string {
return arg;
}
8 changes: 8 additions & 0 deletions test/unit/ts-decl-dir/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"declaration": true,
"declarationDir": "dist/types"
}
}

0 comments on commit d175973

Please sign in to comment.