typescriptgruntjstsconfiggrunt-contrib-copy

Is there a way to move MD file in a TypeScript project to the compiled JS version of the project?


I am working on a TypeScript application, wherein each module of my project I have README.md file. The project structure is like:

- tsconfig
- src
    - actions
      - assignShop
       - index.ts
       - index.test.ts
       - README.md
    - assignStore
       - index.ts
       - index.test.ts
       - README.md

And my tsconfig is like following:

{
  "compilerOptions": {
    "allowJs": false,
    "declaration": true, // enable this once all files are .ts and we can remove allowJs
    "esModuleInterop": true,
    "lib": ["es2015", "es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "pretty": true,
    "noImplicitAny": true,
    "outDir": "./lib",
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "sourceMap": true,
    "target": "es5",

  },
  "include": [
    "src/**/*"
  ],
  "compileOnSave": false
}

Whenever I am compiling my files, they all compile to .js in ./lib directory but I am unable to have my README.md for each compiled version of actions.

Update: 1 So I tired implementing grunt to perform the following task but, I think I need some guidance. Here's my Gruntfile.js:

module.exports = function (grunt) {
  grunt.initConfig({
    clean: ['./lib'],
    ts: {
      default: {
        tsconfig: './tsconfig.json'
      }
    },
    copy: {
        main: {
            expand: true,
            cwd: 'src/',
            src: '**/*.md',
            dest: 'lib/**/*'
        },
      }
  });

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-ts');
  grunt.loadNpmTasks('grunt-contrib-copy');

  grunt.registerTask('default', ['clean', 'ts']);
};

The Solution - Gruntfile.js

module.exports = function (grunt) {
  grunt.initConfig({
    clean: ['./lib'],
    ts: {
      default: {
        tsconfig: './tsconfig.json'
      }
    },
    copy: {
        main: {
            expand: true,
            cwd: 'src/',
            src: '**/*.md',
            dest: 'lib/'
        },
      }
  });

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-ts');
  grunt.loadNpmTasks('grunt-contrib-copy');

  grunt.registerTask('default', ['clean', 'ts']);
}


Solution

  • The TypeScript developers have explicitly ruled out adding such a feature, as they feel it's out of scope for their project.

    Instead, you will need to use another build tool, such as Webpack, Gulp or Grunt.