Skip to content

Commit

Permalink
build: enable ESM bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Dec 15, 2023
1 parent a95223a commit a5f70ca
Show file tree
Hide file tree
Showing 50 changed files with 324 additions and 155 deletions.
5 changes: 4 additions & 1 deletion apps/auth/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@
"url": "http://localhost:3000"
},
{
"url": "http://localhost"
"url": "http://localhost:8080"
},
{
"url": "http://ticketing.dev"
}
],
"components": {
Expand Down
3 changes: 2 additions & 1 deletion apps/auth/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/auth",
"outputFileName": "main.mjs",
"main": "apps/auth/src/main.ts",
"tsConfig": "apps/auth/tsconfig.app.json",
"assets": ["apps/auth/src/assets"],
"generatePackageJson": true,
"target": "node",
"compiler": "tsc",
"webpackConfig": "apps/auth/webpack.config.js",
"webpackConfig": "apps/auth/webpack.config.cjs",
"isolatedConfig": true,
"babelUpwardRootMode": true
},
Expand Down
16 changes: 11 additions & 5 deletions apps/auth/src/app/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import {
OryEnvironmentVariables,
} from '@ticketing/microservices/shared/env';
import { Exclude } from 'class-transformer';
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Mixin } from 'ts-mixer';

export type AppConfigService = ConfigService<EnvironmentVariables, true>;

const __dirname = dirname(fileURLToPath(import.meta.url));
const pkgPath = join(__dirname, '..', '..', '..', '..', '..', 'package.json');

export class EnvironmentVariables extends Mixin(
BaseEnvironmentVariables,
JWTEnvironmentVariables,
MongoEnvironmentVariables,
OryEnvironmentVariables,
OryActionEnvironmentVariables
OryActionEnvironmentVariables,
) {
@Exclude()
private pkg: { [key: string]: unknown; name?: string; version?: string } =
// eslint-disable-next-line security/detect-non-literal-fs-filename
JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8'));
JSON.parse(
// eslint-disable-next-line security/detect-non-literal-fs-filename
readFileSync(pkgPath, 'utf8'),
);

APP_NAME?: string = 'auth';

Expand Down
2 changes: 1 addition & 1 deletion apps/auth/src/app/users/schemas/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { omit } from 'lodash';
import { omit } from 'lodash-es';
import { Document, Model } from 'mongoose';

import { User as UserAttrs } from '../models';
Expand Down
18 changes: 14 additions & 4 deletions apps/auth/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node"],
"emitDecoratorMetadata": true,
"moduleResolution": "Bundler",
"module": "esnext",
"target": "es2022",
"types": [
"node"
],
},
"exclude": ["**/*.spec.ts", "**/*.e2e-spec.ts", "jest.config.ts"],
"include": ["**/*.ts"]
"exclude": [
"**/*.spec.ts",
"**/*.e2e-spec.ts",
"jest.config.ts"
],
"include": [
"**/*.ts"
]
}
2 changes: 1 addition & 1 deletion apps/auth/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"module": "esnext",
"emitDecoratorMetadata": true,
"types": [
"jest",
Expand Down
43 changes: 43 additions & 0 deletions apps/auth/webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { composePlugins, withNx } = require('@nx/webpack');
const nodeExternals = require('webpack-node-externals');

// workaround to load ESM modules in node
// @see https://github.com/nrwl/nx/pull/10414
// @see https://github.com/nrwl/nx/issues/7872#issuecomment-997460397

// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
config.resolve.extensionAlias = {
...config.resolve.extensionAlias,
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
};
return {
...config,
externalsPresets: {
node: true,
},
output: {
...config.output,
module: true,
libraryTarget: 'module',
chunkFormat: 'module',
filename: '[name].mjs',
chunkFilename: '[name].mjs',
library: {
type: 'module',
},
environment: {
module: true,
},
},
experiments: {
...config.experiments,
outputModule: true,
topLevelAwait: true,
},
externals: nodeExternals({
importType: 'module',
}),
};
});
10 changes: 0 additions & 10 deletions apps/auth/webpack.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion apps/expiration/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/expiration",
"outputFileName": "main.mjs",
"main": "apps/expiration/src/main.ts",
"tsConfig": "apps/expiration/tsconfig.app.json",
"assets": ["apps/expiration/src/assets"],
"generatePackageJson": true,
"target": "node",
"compiler": "tsc",
"webpackConfig": "apps/expiration/webpack.config.js",
"webpackConfig": "apps/expiration/webpack.config.cjs",
"isolatedConfig": true,
"babelUpwardRootMode": true
},
Expand Down
8 changes: 6 additions & 2 deletions apps/expiration/src/app/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import {
} from '@ticketing/microservices/shared/env';
import { Exclude } from 'class-transformer';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Mixin } from 'ts-mixer';

export type AppConfigService = ConfigService<EnvironmentVariables, true>;

const __dirname = dirname(fileURLToPath(import.meta.url));
const pkgPath = join(__dirname, '..', '..', '..', '..', '..', 'package.json');

export class EnvironmentVariables extends Mixin(
BaseEnvironmentVariables,
RedisEnvironmentVariables,
RmqEnvironmentVariables,
) {
@Exclude()
private pkg: { [key: string]: unknown; name?: string; version?: string } =
JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8'));
JSON.parse(readFileSync(pkgPath, 'utf8'));

APP_NAME?: string = 'expiration';

Expand Down
6 changes: 4 additions & 2 deletions apps/expiration/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"emitDecoratorMetadata": true,
"moduleResolution": "Bundler",
"module": "esnext",
"target": "es2022",
"types": [
"node"
],
"emitDecoratorMetadata": true,
},
"exclude": [
"**/*.spec.ts",
Expand Down
2 changes: 1 addition & 1 deletion apps/expiration/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"module": "esnext",
"emitDecoratorMetadata": true,
"types": ["jest", "node"],
"allowJs": true
Expand Down
43 changes: 43 additions & 0 deletions apps/expiration/webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { composePlugins, withNx } = require('@nx/webpack');
const nodeExternals = require('webpack-node-externals');

// workaround to load ESM modules in node
// @see https://github.com/nrwl/nx/pull/10414
// @see https://github.com/nrwl/nx/issues/7872#issuecomment-997460397

// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
config.resolve.extensionAlias = {
...config.resolve.extensionAlias,
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
};
return {
...config,
externalsPresets: {
node: true,
},
output: {
...config.output,
module: true,
libraryTarget: 'module',
chunkFormat: 'module',
filename: '[name].mjs',
chunkFilename: '[name].mjs',
library: {
type: 'module',
},
environment: {
module: true,
},
},
experiments: {
...config.experiments,
outputModule: true,
topLevelAwait: true,
},
externals: nodeExternals({
importType: 'module',
}),
};
});
10 changes: 0 additions & 10 deletions apps/expiration/webpack.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion apps/orders/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"generatePackageJson": true,
"target": "node",
"compiler": "tsc",
"webpackConfig": "apps/orders/webpack.config.js",
"webpackConfig": "apps/orders/webpack.config.cjs",
"isolatedConfig": true,
"babelUpwardRootMode": true
},
Expand Down
8 changes: 6 additions & 2 deletions apps/orders/src/app/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
import { Exclude, Expose } from 'class-transformer';
import { IsNumber, IsOptional } from 'class-validator';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Mixin } from 'ts-mixer';

export type AppConfigService = ConfigService<EnvironmentVariables, true>;

const __dirname = dirname(fileURLToPath(import.meta.url));
const pkgPath = join(__dirname, '..', '..', '..', '..', '..', 'package.json');

export class EnvironmentVariables extends Mixin(
BaseEnvironmentVariables,
JWTEnvironmentVariables,
Expand All @@ -23,7 +27,7 @@ export class EnvironmentVariables extends Mixin(
) {
@Exclude()
private pkg: { [key: string]: unknown; name?: string; version?: string } =
JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8'));
JSON.parse(readFileSync(pkgPath, 'utf8'));

APP_NAME?: string = 'orders';

Expand Down
2 changes: 1 addition & 1 deletion apps/orders/src/app/orders/orders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@ticketing/microservices/shared/events';
import { transactionManager } from '@ticketing/microservices/shared/mongo';
import { User } from '@ticketing/shared/models';
import { isEmpty } from 'lodash';
import { isEmpty } from 'lodash-es';
import { Model } from 'mongoose';
import { lastValueFrom, Observable, zip } from 'rxjs';

Expand Down
2 changes: 1 addition & 1 deletion apps/orders/src/app/orders/schemas/order.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { omit } from 'lodash';
import { omit } from 'lodash-es';
import { Document, Model, Schema as MongooseSchema } from 'mongoose';

import { TicketDocument } from '../../tickets/schemas';
Expand Down
3 changes: 1 addition & 2 deletions apps/orders/src/app/tickets/schemas/ticket.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { omit } from 'lodash';
import { omit } from 'lodash-es';
import { Document, Model } from 'mongoose';

import { OrderStatus } from '../../orders/models';
Expand Down Expand Up @@ -35,7 +35,6 @@ export class Ticket implements TicketAttrs {
}

export type TicketDocument = Ticket & Document;
// export type TicketDocument = Ticket & Document<ObjectId>;

export const TicketSchema = SchemaFactory.createForClass(Ticket);

Expand Down
6 changes: 4 additions & 2 deletions apps/orders/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"emitDecoratorMetadata": true,
"moduleResolution": "Bundler",
"module": "esnext",
"target": "es2022",
"types": [
"node"
],
"emitDecoratorMetadata": true,
},
"exclude": [
"**/*.spec.ts",
Expand Down
2 changes: 1 addition & 1 deletion apps/orders/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"module": "esnext",
"emitDecoratorMetadata": true,
"types": [
"jest",
Expand Down
Loading

0 comments on commit a5f70ca

Please sign in to comment.