zoukankan      html  css  js  c++  java
  • Nest.js 初探

    安装@nestjs/core

    import * as nest from '@nestjs/core';
    console.log(nest);
    

    尝试导入@nestjs/core,最终需要以下依赖,才能导入cjs模块:

      "dependencies": {
        "@nestjs/common": "^7.4.4",
        "@nestjs/core": "^7.4.4",
        "reflect-metadata": "^0.1.13",
        "rxjs": "^6.6.3"
      },
    

    官方说法:

    您还可以通过安装核心和npm(或yarn)支持文件来从头开始手动创建新项目。当然,在这种情况下,您将负责自己创建项目样板文件。

    $ yarn add @nestjs/core @nestjs/common rxjs reflect-metadata
    

    打印cjs模块,有如下导出:

    {
      APP_FILTER: [Getter],
      APP_GUARD: [Getter],
      APP_INTERCEPTOR: [Getter],
      APP_PIPE: [Getter],
      NestFactory: [Getter],
      AbstractHttpAdapter: [Getter],
      ApplicationConfig: [Getter],
      DiscoveryModule: [Getter],
      DiscoveryService: [Getter],
      BaseExceptionFilter: [Getter],
      createContextId: [Getter],
      ContextIdFactory: [Getter],
      HttpAdapterHost: [Getter],
      NestContainer: [Getter],
      ModuleRef: [Getter],
      ModulesContainer: [Getter],
      INQUIRER: [Getter],
      MetadataScanner: [Getter],
      MiddlewareBuilder: [Getter],
      NestApplication: [Getter],
      NestApplicationContext: [Getter],
      REQUEST: [Getter],
      Reflector: [Getter]
    }
    

    安装express插件

    就像Spring一样,nest也需要内置express:

    yarn add @nestjs/platform-express
    

    事情没有结束

    你如果开始使用nds启动项目,会有许多的问题:
    首先,我们需要让ts-loader保留类装饰器(类的注解),然后让Babel支持类装饰器,并(处理顺序:ts-loader => babel-loader => webpack)。
    配置tsconfig.json:

    {
        "compilerOptions": {
            "moduleResolution": "node", // 模块解析度,设置为Node平台
            "target": "es2015", // 编译为ES6代码,再交给Babel处理
            "experimentalDecorators": true, // 保留类装饰器
            "declaration": true // 支持导出类型声明文件
        }
    }
    

    下载Babel插件:

    $ yarn add -D @babel/plugin-proposal-decorators
    

    在babel.config.json中配置插件:

    {
        "plugins": [
            [
                "@babel/plugin-proposal-decorators",
                {
                    "legacy": true // 重要,必须保留类装饰器,交给Webpack
    // 相关错误:装饰器插件需要一个decoratorsBeforeExport选项,其值必须为布尔值。如果要使用旧版装饰器语义,则可以设置legacy:true选项。
    // 实践证明,只能用旧版语义
                }
            ],
            "@babel/plugin-proposal-class-properties"
        ],
        "presets": [
            [
                "@babel/preset-env",
                {
                    "targets": {
                        "node": "12.19.0"
                    }
                }
            ],
            [
                "@babel/preset-typescript",
                {
                    "allowNamespaces": false
                }
            ]
        ]
    }
    

    现在,项目支持node-dev-server编译并启动:
    https://github.com/develon2015/You-dont-need-CLI/tree/b64777d5a6f0e02250e030753a5fb744852cd783

    但是,webpack编译dist失败。没有关系,nds编译后照样跑,不像nest build一样,没有任何运行时依赖,全部都是开发依赖。
    又不是web平台,为什么要全部打包呢?哈哈。而且必要的时候把tsconfig.json复制成tsconfig.build.json就可以用nest build命令编译了。

  • 相关阅读:
    小程序接入第三方ui库(组件库)
    vue仿微信网页版|vue+web端聊天室|仿微信客户端vue版
    Java 开发环境配置
    那么多 Java 版本,如何选择合适的版本
    你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
    JavaScript HTML DOM EventListener addEventListener() 方法
    Vue2.0史上最全入坑教程(上)—— 搭建Vue脚手架(vue-cli)
    使用 Fetch
    CSS3中steps()动画的详解
    MYSQL常用命令
  • 原文地址:https://www.cnblogs.com/develon/p/13895843.html
Copyright © 2011-2022 走看看