Suj*_*haW 1 node.js typescript nestjs
我的文件夹结构类似于下面。
public
views
src
main.ts
/users
users.controller.ts
/views
my-view.hbs
/books
books.controller.ts
/views
my-view.hbs
Run Code Online (Sandbox Code Playgroud)
这是我用来添加模板和视图的
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
console.log(join(__dirname, 'public'));
app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
hbs.registerPartials(join(__dirname, '..', 'views', 'partials'));
Run Code Online (Sandbox Code Playgroud)
我的 package.json 脚本看起来像这样
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/src/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我在开发模式下运行 nest 时,它会构建分发代码,但不会添加视图和公用文件夹。
在官方文档中查看此部分。
使用assets,nest build将分发非 TypeScript 文件,例如.graphql文件、图像、.html文件和其他资产,作为开发构建步骤的一部分。
例子:
"assets": ["**/*.hbs"]
Run Code Online (Sandbox Code Playgroud)
将此添加到nest-cli.json位于根目录中的文件中。
我遇到了同样的问题,我发现"assets"应该在里面"compilerOptions",你确认了吗?
我确实让它像这样工作:
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"webpack": false,
"assets": [
"**/*.hbs"
],
"watchAssets": true
},
"generateOptions": {
"spec": false
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2994 次 |
| 最近记录: |