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命令编译了。

  • 相关阅读:
    [Memcached]操作
    [Linux-CentOS7]安装Telnet
    PAT Advanced 1093 Count PAT's (25分)
    PAT Advanced 1065 A+B and C (64bit) (20分)
    PAT Advanced 1009 Product of Polynomials (25分)
    PAT Advanced 1008 Elevator (20分)
    PAT Advanced 1006 Sign In and Sign Out (25分)
    PAT Advanced 1002 A+B for Polynomials (25分)
    半年分布式处理回顾&机器学习(一)——线性回归
    PAT Advanced 1147 Heaps (30分)
  • 原文地址:https://www.cnblogs.com/develon/p/13895843.html
Copyright © 2011-2022 走看看