1.6.1 • Published 8 months ago

nestjs-esm-fix v1.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

nestjs-esm-fix

CI Maintainability Test Coverage npm (tag)

Patch Nestjs app ESM bundles to make them work RHRN
⚠️ This is a temporary solution until Nestjs is fixed

Stack

Problems

https://github.com/nestjs/nest-cli/issues/1157 https://github.com/nestjs/swagger/issues/1450 https://github.com/evanw/esbuild/pull/509 https://github.com/evanw/esbuild/issues/566

  1. openapi is not defined. https://github.com/nestjs/swagger/issues/1450
__decorate([
  Post('event-unsafe-batch'),
  HttpCode(200),
  openapi.ApiResponse({ status: 200, type: String }),
  __param(0, Body()),
  __param(1, Req()),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", [Object, Object]),
  __metadata("design:returntype", Promise)
], EventUnsafeController.prototype, "logEventBatch", null);
  1. openapi / class-validator DTOs are referenced by require API. https://github.com/microsoft/TypeScript/issues/43329
export class CspReportDto {
  static _OPENAPI_METADATA_FACTORY() {
    return { timestamp: { required: false, type: () => Object }, 'csp-report': { required: true, type: () => require("./csp.dto.js").CspReport } };
  }
}
  1. NodeJS builtins are referenced via require API.
var require_async4 = __commonJS({
  "node_modules/resolve/lib/async.js"(exports, module2) {
    var fs2 = require("fs");
  1. esbuild-compiled ESM bundle cannot refer to views/redoc.handlebars
const redocFilePath = path_1.default.join(__dirname, "..", "views", "redoc.handlebars");
  1. _OPENAPI_METADATA_FACTORY class fields may be empty, so the swagger declaration cannot be properly rendered.
var Meta = class {
};
// →
var Meta = class {
  static _OPENAPI_METADATA_FACTORY() {
    return { appName: { required: false,  type: () =>  String }, appHost: { required: false,  type: () =>  String }, appVersion: { required: false,  type: () =>  String }, appNamespace: { required: false,  type: () =>  String }, appConfig: { required: false,  type: () =>  typeof (_a3 = typeof Record !== "undefined" && Record) === "function" ? _a3 : Object }, deviceInfo: { required: false,  type: () =>  typeof (_b3 = typeof Record !== "undefined" && Record) === "function" ? _b3 : Object }, userAgent: { required: false,  type: () =>  String }, envProfile: { required: false,  enum:  typeof (_c = typeof import_substrate2.EnvironmentProfile !== "undefined" && import_substrate2.EnvironmentProfile) === "function" ? _c : Object } }
  }
};
  1. Extra type wrappers cannot be processed by openapi / class-validator / class-transformer
  __metadata("design:type", typeof (_d = typeof Array !== "undefined" && Array) === "function" ? _d : Object)
  __metadata("design:type", typeof (_e = typeof import_substrate2.LogLevel !== "undefined" && import_substrate2.LogLevel) === "function" ? _e : Object)
  // →
  __metadata("design:type", Array)
  __metadata("design:type", import_substrate2.LogLevel)

Solution

Old good monkey patching.

Install

yarn add -D nestjs-esm-fix

Usage

CLI

nestjs-esm-fix target/**/*.js
nestjs-esm-fix --target=target/**/*.js
nestjs-esm-fix --target=**/* --cwd=target
OptionDescriptionDefault
--targetPattern to match files to fix.**/*
--cwdCurrent working dir.process.cwd()
--openapi-complex-typesSimplify __metadata("design:type") declarations.true
--openapi-metaRestore static OPENAPI_METADATA_FACTORY if missing.true
--openapi-varInject openapi variable. Set --no-openapi-var to disable.true
--dirname-varInject __dirname and __filename polyfills.true
--importifyReplace require with import API for Nodejs builtins. Replace type: () => require(smth) statements with import.true
--require-mainInject main field for require API polyfill.true
--redoc-tplInject redoc.hbs templates.true

JS API

import { fix } from 'nestjs-esm-fix'
await fix({
  cwd: '.',
  target: 'target/**/*.js',
  openapiComplexTypes: true,
  openapiVar: true,
  openapiMeta: true,
  dirnameVar: true,
  importify: true,
  requireMain: true,
  redocTpl: true
})

License

MIT

1.6.1

8 months ago

1.2.0

2 years ago

1.5.2

2 years ago

1.6.0

2 years ago

1.5.1

2 years ago

1.4.2

2 years ago

1.2.4

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.3.2

2 years ago

1.2.3

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago