I'm working on an EmailModule that reads a pug template.
I couldn't get the build to include the *.pug templates file:
I have followed the instruction based on this: https://github.com/nestjs/nest-cli/issues/320
Adding the assets property in nest-cli.json
"email": {
"type": "library",
"root": "libs/email",
"entryFile": "index",
"sourceRoot": "libs/email/src",
"compilerOptions": {
"tsConfigPath": "libs/email/tsconfig.lib.json"
},
"assets": ["**/*.pug"]
},
My use case involved setting up static file serving via the @nest/serve-static package. The tutorial recommeds putting those assets in the client folder at the root of the project, and configuring the module like so:
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'client'),
}),
Unfortunately, this did not work for me since the client folder wasn't getting copied to the dist folder. It took some tries but eventually I got the assets configuration to do just that, here it is:
"assets": [
{ "include": "../client/**", "outDir": "dist/client", "watchAssets": true }
]
You have to include it in the nest-cli.json, inside the compilerOptions top-level property, e.g.
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"assets": [
{ "include": "../client/**", "outDir": "dist/client", "watchAssets": true }
]
}
}
The documentation is a bit unclear about this, and how NestJS currently (version 10.2.2) implements the copying of assets is not very logical.
What to keep in mind is that assets will ONLY be copied from your source (normally src) folder.
Let's say that you have a folder named assets in your src folder, and you want to include it in your dist/src folder. You would then assume that a nest-cli.json like the one below would copy that same folder to dist/src/assets:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": ["assets/**/*"]
]
}
}
But the above settings will instead copy the assets folder to the root of your dist folder. It will not be put in the dist/src folder, which will break any relative linking to these files from other files in your dist folder.
So, if you want to maintain the same structure in your dist folder, the proper way to do this is like this:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [
{
"include": "assets/**/*",
"outDir": "dist/src/"
}
]
}
}
The above settings will copy all files and folders from src/assets to dist/src/assets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With