npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-tsx-render

v0.0.29

Published

在 nestjs 中使用 tsx 作为模板引擎,将通过 nestjs 处理的数据塞入到 tsx 的模板的中进行渲染。

Downloads

20

Readme

介绍

在 nestjs 中使用 tsx 作为模板引擎,将通过 nestjs 处理的数据塞入到 tsx 的模板的中进行渲染。

安装:

npm i --save nestjs-tsx-render

基本用法

  1. 在项目根目录下新建名为:webpack.config.override.js 的文件 指定需要编译的前端入口:
module.exports = {
  entry: {
    // 这里的key就是下面render时的name
    home: "/path/to/page/index",
  },
};
  1. 配置后端模块

Module 中:

import { Module } from "@nestjs/common";
import { TsxViewsModule } from "nestjs-tsx-render";
import XXXController from "./xxx.controller";

@Global()
@Module({
  imports: [
    TsxViewsModule.register({
      forRoutes: [XXXController],
    }),
  ],
  controllers: [XXXController],
  providers: [],
  exports: [],
})
export default class XXXModule {}

Controller:

@Controller('')
export default class XXXController {
  @Get('/path/to/rote')
  @Render('home')
  home() {
    return (
      msg: '这是一条来自于服务端的消息'
    )
  }
}
  1. 启动编译

前端:

# 前端编译
npx ntr build
# 或:前端编译(watch模式)
npx ntr build -w

后端:

# 一般借助nestjs cli
nest start --watch

高级用法

多入口

修改 webpack.config.override.js 文件

module.exports = {
  entry: {
    // 这里的key就是下面render时的name
    home: "/path/to/page/index",
    home2: "/path/to/page/index2",
  },
};

在后端 controller 中即可:

@Controller('')
export default class XXXController {
  @Get('/path/to/rote')
  @Render('home')
  home() {
    return (
      msg: '这是一条来自于服务端的消息'
    )
  }
  @Get('/path/to/rote2')
  @Render('home2')
  home() {
    return (
      msg: '这是一条来自于服务端的消息2'
    )
  }
}

往页面中资源

修改 webpack.config.override.js 文件

{
  // 注入style
  injectStyle: [
    '<link rel="stylesheet" type="text/css" href="xxx.css" />',
  ],
  // 注入script
  injectScript: [
    '<script src="xxx.js"></script>'
  ]
}

修改 webpack 配置

修改 webpack.config.override.js 文件,直接添加你想修改的字段,内部采用的是 webpack-merge进行配置的合并

{
  mode: process.env.NODE_ENV === 'dev' ? 'development' : 'production',
}

splitChunks

修改 splitChunks 的生成规则,并指定 publicPath,目的是生成的静态资源前缀

{
  publicPath: '/build/static/',
  optimization: {
    maxSize: 300 * 1000,
    splitChunks: {
      cacheGroups: {
        commons: {
          test: /[\\/]node_modules[\\/]/,
          // 这里name必须是vendors
          name: 'vendors',
          chunks: 'all',
        },
      },
    },
  },
}

在 nestjs 的 APPModule 中指定静态目录:

import { ServeStaticModule } from "@nestjs/serve-static";
@Module({
  imports: [
    ServeStaticModule.forRoot({
      // 这里的目录需要是构建生成的前端资源目录
      rootPath: path.join(__dirname, "..", "buildfe"),
      // 这里的值需要时上面配置的publicPath的值
      serveRoot: "/build/static/",
    }),
  ],
})
export class AppModule {}