Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy non-ts files to dist when building typescript?

Tags:

I have Mail module in folder with this structure:

- Mail   - templates       - <Handlebars files>   - mail.module.ts 

When I build (compile) TypeScript project, my template folder is not included in build folder. How to move those kind of files into dist when building?

Is it different for development vs production builds?

like image 434
Baterka Avatar asked Feb 19 '20 18:02

Baterka


2 Answers

You can also do this by adding an asset property in the nest-cli.json as mentioned in the documentation.

For your case, you can try something like this:

"assets":["**/Mail/templates/*"] 

Or if your templates all have a specific filetype (I am assuming its called .template), this should also work:

"assets":["**/*.template"] 

The 2nd example will copy all files of type .template from all sub directories to your dist folder with the same folder structure when you run a nest build. Similarly, you can use this to copy any filetype of your choice (.json, .proto, etc) by replacing .template in the glob pattern.

like image 123
Derryl Thomas Avatar answered Sep 28 '22 04:09

Derryl Thomas


TS compiler doesn't handle files that are other than TypeScript or JS (e.g. .ts, .js, .tsx, etc.).

One way of doing it, just running cp to copy those files after you compile NestJS. In your package.json replace the line

"build": "nest build", 

with

"build": "nest build && cp ./Mail/templates ./build", 

Ideally, I would switch to Webpack (or similar) to transpile TypeScript and copy artifacts. NestJs has a basic example on how to build with Webpack here. To extend it to have a "copy" phase, install copy-webpack-plugin npm package and add those additions in webpack config file:

const copyFiles = require('copy-webpack-plugin'); // ... omitted for abbreviation  module.exports = function(options) {    return {    // ... omitted for abbreviation    ,     plugins: [       // ... omitted for abbreviation       new copyFiles([             { from: 'Mail/templates', to: 'templates' }         ])     ] } 
like image 29
Jenya Y. Avatar answered Sep 28 '22 04:09

Jenya Y.