Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain src/ folder structure when building to dist/ folder with Typescript 3

I have a TypeScript nodejs server with this structure:

tsconfig.json
package.json
src/
    middleware/
    utils/
    index.ts
dist/
    middleware/
    utils/
    index.js

When using TypeScript 2, I was able to transpile my project from the src/ to a dist/ folder and have a mirror image of my directory structure to work with.

With the release of TypeScript 3, they have introduced project references and changed the way code is transpiled into an output directory. Now tsc outputs to the dist/ folder in a nested way like this:

dist/
    src/
        middleware/
        utils/
        index.js

My tsconfig.json is:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowJs": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "declaration": false,
    "outDir": "dist/",
    "lib": [
      "es7",
      "dom"
    ]
  },
  "include": [
    "src/"
  ]
}

How can I configure TypeScript to output my src/ folder as a mirror image into a dist/ folder?

like image 641
nfadili Avatar asked Aug 31 '18 19:08

nfadili


People also ask

What is the difference between Dist and src folders?

src/ stands for source, and is the raw code before minification or concatenation or some other compilation - used to read/edit the code. dist/ stands for distribution, and is the minified/concatenated version - actually used on production sites.

What should be in src folder?

The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

What is dist TypeScript?

dist — the folder that has the output from the compiler. node_modules — the folder containing the packages that the app and dev tools require. src — the folder containing the source code files that will be compiled by the TypeScript compiler. package.

What is dist folder?

The dist folder, short for distribution folder, is dynamically generated when using the nuxt generate commands and includes the generated production ready HTML files and assets that are necessary to deploy and run your statically generated Nuxt application.


2 Answers

I had a similar problem when initially converting to a TypeScript project. I also set resolveJsonModule: true and the src directory was copied to the output dist directory.

The underlying reason is that one of my source files required package.json at the root of the project. Once I removed that, tsc no longer added src to the dist directory.

In short, make sure you are not requiring files outside of your src directory.

Explanatory FAQ here: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file

like image 56
Luke W Avatar answered Oct 10 '22 09:10

Luke W


The structure of the output directory is controlled by the rootDir of the compilerOptions. See documentation here, setting it to ./src should solve the issue.

{
  "compilerOptions": {
    "rootDir": "src",
    ...
  },
  "include": [
    "src/"
  ]
}
like image 47
Morlo Mbakop Avatar answered Oct 10 '22 10:10

Morlo Mbakop