Skip to content

build: generate multiple formats (cjs, esm) #8736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"changelog": "lerna-changelog",
"prebuild:prod": "npm run clean",
"build:prod": "gulp build",
"build:multi": "npx tsup --dts --sourcemap --format esm,cjs",
"build:samples": "gulp install:samples && npm run build && gulp build:samples && gulp test:samples && gulp test:e2e:samples",
"clean": "gulp clean:bundle",
"codechecks:benchmarks": "codechecks ./tools/benchmarks/check-benchmarks.ts",
Expand Down Expand Up @@ -162,6 +163,7 @@
"supertest": "6.2.2",
"ts-morph": "13.0.3",
"ts-node": "10.4.0",
"tsup": "^5.10.0",
"typeorm": "0.2.41",
"typescript": "4.3.5",
"wrk": "1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/injector/module-token-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Type } from '@nestjs/common/interfaces/type.interface';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { isFunction, isSymbol } from '@nestjs/common/utils/shared.utils';
import stringify from 'fast-safe-stringify';
import * as hash from 'object-hash';
import hash from 'object-hash';

export class ModuleTokenFactory {
private readonly moduleIdsCache = new WeakMap<Type<unknown>, string>();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/middleware/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RequestMethod } from '@nestjs/common';
import { HttpServer, RouteInfo, Type } from '@nestjs/common/interfaces';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { iterate } from 'iterare';
import * as pathToRegexp from 'path-to-regexp';
import pathToRegexp from 'path-to-regexp';
import { v4 as uuid } from 'uuid';
import { ExcludeRouteMetadata } from '../router/interfaces/exclude-route-metadata.interface';
import { isRouteExcluded } from '../router/utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@nestjs/common/utils/shared.utils';
import { iterate } from 'iterare';
import { platform } from 'os';
import * as pathToRegexp from 'path-to-regexp';
import pathToRegexp from 'path-to-regexp';
import { AbstractHttpAdapter } from './adapters';
import { ApplicationConfig } from './application-config';
import { MESSAGES } from './constants';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
isString,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import * as pathToRegexp from 'path-to-regexp';
import pathToRegexp from 'path-to-regexp';
import { ApplicationConfig } from '../application-config';
import { UnknownRequestMappingException } from '../errors/exceptions/unknown-request-mapping.exception';
import { GuardsConsumer } from '../guards/guards-consumer';
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/client/client-tcp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '@nestjs/common';
import * as net from 'net';
import net from 'net';
import { EmptyError, lastValueFrom } from 'rxjs';
import { share, tap } from 'rxjs/operators';
import {
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/external/kafka.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/// <reference types="node" />

import * as net from 'net';
import * as tls from 'tls';
import net from 'net';
import tls from 'tls';

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = T | U extends object
Expand Down
3 changes: 1 addition & 2 deletions packages/microservices/server/server-tcp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isString, isUndefined } from '@nestjs/common/utils/shared.utils';
import * as net from 'net';
import { Server as NetSocket, Socket } from 'net';
import net, { Server as NetSocket, Socket } from 'net';
import { Observable } from 'rxjs';
import {
CLOSE_EVENT,
Expand Down
18 changes: 9 additions & 9 deletions packages/platform-express/adapters/express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ import {
InternalServerErrorException,
RequestMethod,
StreamableFile,
VersioningType,
VersioningType
} from '@nestjs/common';
import {
VersioningOptions,
VersionValue,
VERSION_NEUTRAL,
VERSION_NEUTRAL
} from '@nestjs/common/interfaces';
import {
CorsOptions,
CorsOptionsDelegate,
CorsOptionsDelegate
} from '@nestjs/common/interfaces/external/cors-options.interface';
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
import {
isFunction,
isNil,
isObject,
isString,
isString
} from '@nestjs/common/utils/shared.utils';
import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
import { RouterMethodFactory } from '@nestjs/core/helpers/router-method-factory';
import {
json as bodyParserJson,
urlencoded as bodyParserUrlencoded,
urlencoded as bodyParserUrlencoded
} from 'body-parser';
import * as cors from 'cors';
import * as express from 'express';
import * as http from 'http';
import * as https from 'https';
import cors from 'cors';
import express from 'express';
import http from 'http';
import https from 'https';
import { ServeStaticOptions } from '../interfaces/serve-static-options.interface';

export class ExpressAdapter extends AbstractHttpAdapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Optional,
Type,
} from '@nestjs/common';
import * as multer from 'multer';
import multer from 'multer';
import { Observable } from 'rxjs';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
import { MulterModuleOptions } from '../interfaces';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Optional,
Type,
} from '@nestjs/common';
import * as multer from 'multer';
import multer from 'multer';
import { Observable } from 'rxjs';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
import { MulterModuleOptions } from '../interfaces';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Optional,
Type,
} from '@nestjs/common';
import * as multer from 'multer';
import multer from 'multer';
import { Observable } from 'rxjs';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
import { MulterModuleOptions } from '../interfaces';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Optional,
Type,
} from '@nestjs/common';
import * as multer from 'multer';
import multer from 'multer';
import { Observable } from 'rxjs';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
import { MulterModuleOptions } from '../interfaces';
Expand Down
11 changes: 8 additions & 3 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
HttpStatus,
Logger,
Expand Down Expand Up @@ -29,10 +30,10 @@ import {
RawServerDefault,
RequestGenericInterface,
} from 'fastify';
import * as Reply from 'fastify/lib/reply';
import Reply from 'fastify/lib/reply';
import { RouteShorthandMethod } from 'fastify/types/route';
import * as http2 from 'http2';
import * as https from 'https';
import http2 from 'http2';
import https from 'https';
import {
Chain as LightMyRequestChain,
InjectOptions,
Expand Down Expand Up @@ -100,6 +101,7 @@ export class FastifyAdapter<
TRawResponse
> = FastifyInstance<TServer, TRawRequest, TRawResponse>,
> extends AbstractHttpAdapter<TServer, TRequest, TReply> {
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why all the @ts-ignores? Is there a typing that isn't right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS was complaining the typing isn't right!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we should probably fix it rather than ignore it, right?

Copy link
Author

@manju-reddys manju-reddys Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but I just didn't want to break anything.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but I just didn't want to break anything.

ts-ignore = I know it may break things but I do accept it.

protected readonly instance: TInstance;

private _isParserRegistered: boolean;
Expand Down Expand Up @@ -195,6 +197,7 @@ export class FastifyAdapter<
},
...(instanceOrOptions as FastifyServerOptions),
});
// @ts-ignore
this.setInstance(instance);
}

Expand Down Expand Up @@ -350,6 +353,7 @@ export class FastifyAdapter<
return await this.instance.close();
} catch (err) {
// Check if server is still running
// @ts-ignore
if (err.code !== 'ERR_SERVER_NOT_RUNNING') {
throw err;
}
Expand All @@ -358,6 +362,7 @@ export class FastifyAdapter<
}

public initHttpServer() {
// @ts-ignore
this.httpServer = this.instance.server;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/platform-ws/adapters/ws-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { AbstractWsAdapter } from '@nestjs/websockets';
import {
CLOSE_EVENT,
CONNECTION_EVENT,
ERROR_EVENT,
ERROR_EVENT
} from '@nestjs/websockets/constants';
import { MessageMappingProperties } from '@nestjs/websockets/gateway-metadata-explorer';
import * as http from 'http';
import { EMPTY, fromEvent, Observable } from 'rxjs';
import http from 'http';
import { fromEvent, Observable } from 'rxjs';
import { filter, first, mergeMap, share, takeUntil } from 'rxjs/operators';
import { URL } from 'url';

let wsPackage: any = {};

Expand Down
1 change: 1 addition & 0 deletions packages/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"allowJs": false,
"strict": true,
"strictNullChecks": false,
"esModuleInterop": true,
"types": ["node"]
},
"exclude": ["../node_modules", "./**/*.spec.ts"]
Expand Down
2 changes: 1 addition & 1 deletion packages/websockets/sockets-container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as hash from 'object-hash';
import hash from 'object-hash';
import { GatewayMetadata, ServerAndEventStreamsHost } from './interfaces';

export class SocketsContainer {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"allowJs": true,
"outDir": "dist",
"lib": ["es7"],
"esModuleInterop": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for setting this to true? Is it necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so because this will allow us to start letting Nest be used via ESM we need to make the rest of the CJS imports ESM compatible, correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it just adds the compatibility

"paths": {
"@nestjs/common": ["./packages/common"],
"@nestjs/common/*": ["./packages/common/*"],
Expand Down
19 changes: 19 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from 'tsup';

export default defineConfig({
splitting: false,
sourcemap: true,
outDir: 'node_modules/@nestjs',
clean: true,
entryPoints: [
'packages/common/index.ts',
'packages/core/index.ts',
'packages/microservices/index.ts',
'packages/platform-express/index.ts',
'packages/platform-fastify/index.ts',
'packages/platform-socket.io/index.ts',
'packages/platform-ws/index.ts',
'packages/testing/index.ts',
'packages/websockets/index.ts'
],
});