ค้นหาเว็บไซต์

node.js - มุมมองของ NestJS ไม่ถูกเพิ่มเข้าไปใน Dist


โครงสร้างโฟลเดอร์ของฉันคล้ายกับด้านล่างนี้

public
views
src
  main.ts
  /users
       users.controller.ts
       /views
         my-view.hbs
  /books
       books.controller.ts
       /views
         my-view.hbs

นี่คือสิ่งที่ฉันใช้ในการเพิ่มเทมเพลตและมุมมอง

 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'));

สคริปต์ในไฟล์ 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"
  },

ปัญหาของฉันคือเมื่อฉันรัน Nest ในโหมดพัฒนา มันจะสร้างโค้ดสำหรับแจกจ่าย แต่ไม่เพิ่มโฟลเดอร์ views และ public

คำตอบ #1

ฉันกำลังเจอปัญหาเดียวกันและฉันก็พบว่า "assets" ควรอยู่ภายใน "compilerOptions" คุณยืนยันเหมือนกันหรือเปล่า?

ฉันทำให้มันทำงานแบบนี้ได้:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "webpack": false,
    "assets": [
      "**/*.hbs"
    ],
    "watchAssets": true
  },
  "generateOptions": {
    "spec": false
  }
}

คำตอบ #2

ตรวจสอบส่วนนี้ในเอกสารทางการ

ด้วย assets nest build จะแจกจ่ายไฟล์ที่ไม่ใช่ TypeScript เช่น ไฟล์ .graphql รูปภาพ ไฟล์ .html และแอสเซทอื่น ๆ เป็นส่วนหนึ่งของขั้นตอนการสร้างสำหรับการพัฒนาของคุณ

ตัวอย่าง :

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

เพิ่มสิ่งนี้ไปที่ไฟล์ nest-cli.json ของคุณที่อยู่ในไดเร็กทอรีราก