From 1b3bca283e974fc7e93cbda0fa96f7f705924773 Mon Sep 17 00:00:00 2001 From: Matthew Heroux Date: Fri, 27 Oct 2023 22:18:11 -0500 Subject: [PATCH] feat: add create-bundler --watch (#470) Signed-off-by: hxtree --- .github/workflows/call-release.yml | 3 +- .../rush/browser-approved-packages.json | 4 + common/config/rush/pnpm-lock.yaml | 106 ++++++++++-------- common/config/rush/repo-state.json | 2 +- platform/bundlers/CHANGELOG.md | 30 ----- .../src/__tests__/nestjs-bundler.test.ts | 18 --- .../__tests__/test-data/nestjs/src/index.ts | 1 - platform/bundlers/src/bin-nestjs.ts | 7 -- platform/bundlers/src/index.ts | 1 - .../bundlers/src/profiles/nestjs-bundler.ts | 59 ---------- .../{bundlers => create-bundle}/.eslintrc.js | 0 .../CHANGELOG.json | 31 ++++- platform/create-bundle/CHANGELOG.md | 48 ++++++++ .../{bundlers => create-bundle}/LICENSE.md | 0 .../{bundlers => create-bundle}/README.md | 11 +- .../jest.config.js | 0 .../{bundlers => create-bundle}/package.json | 9 +- .../src/__tests__/nestjs-bundler.test.ts | 65 +++++++++++ .../__tests__/test-data/nestjs/src/index.ts | 1 + .../__tests__/test-data/nestjs/tsconfig.json | 0 .../create-bundle/src/create-bundle-bin.ts | 12 ++ platform/create-bundle/src/index.ts | 1 + .../src/profiles/nestjs-bundler.ts | 101 +++++++++++++++++ .../{bundlers => create-bundle}/tsconfig.json | 0 rush.json | 31 ++--- 25 files changed, 346 insertions(+), 195 deletions(-) delete mode 100644 platform/bundlers/CHANGELOG.md delete mode 100644 platform/bundlers/src/__tests__/nestjs-bundler.test.ts delete mode 100644 platform/bundlers/src/__tests__/test-data/nestjs/src/index.ts delete mode 100644 platform/bundlers/src/bin-nestjs.ts delete mode 100644 platform/bundlers/src/index.ts delete mode 100644 platform/bundlers/src/profiles/nestjs-bundler.ts rename platform/{bundlers => create-bundle}/.eslintrc.js (100%) rename platform/{bundlers => create-bundle}/CHANGELOG.json (63%) create mode 100644 platform/create-bundle/CHANGELOG.md rename platform/{bundlers => create-bundle}/LICENSE.md (100%) rename platform/{bundlers => create-bundle}/README.md (64%) rename platform/{bundlers => create-bundle}/jest.config.js (100%) rename platform/{bundlers => create-bundle}/package.json (87%) create mode 100644 platform/create-bundle/src/__tests__/nestjs-bundler.test.ts create mode 100644 platform/create-bundle/src/__tests__/test-data/nestjs/src/index.ts rename platform/{bundlers => create-bundle}/src/__tests__/test-data/nestjs/tsconfig.json (100%) create mode 100644 platform/create-bundle/src/create-bundle-bin.ts create mode 100644 platform/create-bundle/src/index.ts create mode 100644 platform/create-bundle/src/profiles/nestjs-bundler.ts rename platform/{bundlers => create-bundle}/tsconfig.json (100%) diff --git a/.github/workflows/call-release.yml b/.github/workflows/call-release.yml index e9ff6262..443a6e08 100644 --- a/.github/workflows/call-release.yml +++ b/.github/workflows/call-release.yml @@ -57,7 +57,8 @@ jobs: body: | # New Release: [@cats-cradle ${{ github.ref_name }}](https://www.npmjs.com/search?q=%40cats-cradle), - We're pleased to share our latest release, which comprises multiple zip files, each housing a distinct, deploy-ready project from @cats-cradle. Within each package, you'll find the project's code and Infrastructure as Code (IaC) authored in AWS CDK v2 for streamlined deployment. + We're pleased to share our latest services release, which comprises multiple zip files, each housing a distinct, deploy-ready project from @cats-cradle. Within each package, you'll find the project's code and Infrastructure as Code (IaC) authored in AWS CDK v2 for streamlined deployment. Although each package is maintained primarily for the game, most stable packages + We release a multitude of packages independently of our services. Each package is individually published on [NPM](https://www.npmjs.com/search?q=%40cats-cradle) when any changes are made. We warmly invite you to delve into the contents of this release, offer your valuable feedback, and actively engage in our project on GitHub. Your contributions and active participation are fundamental to our ongoing efforts to enhance and refine our offerings. artifacts: 'common/temp/dist/*.zip' diff --git a/common/config/rush/browser-approved-packages.json b/common/config/rush/browser-approved-packages.json index caf749fe..5cf5917a 100644 --- a/common/config/rush/browser-approved-packages.json +++ b/common/config/rush/browser-approved-packages.json @@ -386,6 +386,10 @@ "name": "babel-plugin-prismjs", "allowedCategories": [ "apis" ] }, + { + "name": "chokidar", + "allowedCategories": [ "rigs" ] + }, { "name": "chrome-aws-lambda", "allowedCategories": [ "apis" ] diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 872eb70f..50badab1 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -560,46 +560,6 @@ importers: specifier: 5.1.3 version: 5.1.3 - ../../platform/bundlers: - dependencies: - '@anatine/esbuild-decorators': - specifier: ~0.2.19 - version: 0.2.19(esbuild@0.17.5) - '@cats-cradle/base-nodejs': - specifier: workspace:* - version: link:../rigs/base-nodejs - commander: - specifier: ~10.0.0 - version: 10.0.0 - esbuild: - specifier: ~0.17.5 - version: 0.17.5 - devDependencies: - '@cats-cradle/eslint-config': - specifier: 1.0.6 - version: 1.0.6 - '@types/jest': - specifier: 29.5.5 - version: 29.5.5 - '@types/node': - specifier: 20.8.2 - version: 20.8.2 - eslint: - specifier: 8.44.0 - version: 8.44.0 - jest: - specifier: 29.7.0 - version: 29.7.0(@types/node@20.8.2)(ts-node@10.9.1) - ts-jest: - specifier: 29.1.1 - version: 29.1.1(@babel/core@7.22.20)(esbuild@0.17.5)(jest@29.7.0)(typescript@5.1.3) - ts-node: - specifier: 10.9.1 - version: 10.9.1(@swc/core@1.3.89)(@types/node@20.8.2)(typescript@5.1.3) - typescript: - specifier: 5.1.3 - version: 5.1.3 - ../../platform/constructs: dependencies: '@nestjs/common': @@ -764,6 +724,49 @@ importers: specifier: 5.1.3 version: 5.1.3 + ../../platform/create-bundle: + dependencies: + '@anatine/esbuild-decorators': + specifier: ~0.2.19 + version: 0.2.19(esbuild@0.17.5) + '@cats-cradle/base-nodejs': + specifier: workspace:* + version: link:../rigs/base-nodejs + chokidar: + specifier: ~3.5.3 + version: 3.5.3 + commander: + specifier: ~10.0.0 + version: 10.0.0 + esbuild: + specifier: ~0.17.5 + version: 0.17.5 + devDependencies: + '@cats-cradle/eslint-config': + specifier: 1.0.6 + version: 1.0.6 + '@types/jest': + specifier: 29.5.5 + version: 29.5.5 + '@types/node': + specifier: 20.8.2 + version: 20.8.2 + eslint: + specifier: 8.44.0 + version: 8.44.0 + jest: + specifier: 29.7.0 + version: 29.7.0(@types/node@20.8.2)(ts-node@10.9.1) + ts-jest: + specifier: 29.1.1 + version: 29.1.1(@babel/core@7.22.20)(esbuild@0.17.5)(jest@29.7.0)(typescript@5.1.3) + ts-node: + specifier: 10.9.1 + version: 10.9.1(@swc/core@1.3.89)(@types/node@20.8.2)(typescript@5.1.3) + typescript: + specifier: 5.1.3 + version: 5.1.3 + ../../platform/eslint-config: dependencies: '@rushstack/eslint-config': @@ -2603,12 +2606,21 @@ packages: '@jridgewell/gen-mapping': 0.3.3 '@jridgewell/trace-mapping': 0.3.19 + /@anatine/esbuild-decorators@0.2.19(esbuild@0.17.19): + resolution: {integrity: sha512-pyj6ULyMacyzpDqlnbS2OCkOqxcVgk8IqiTMRJ5CrsF8Yl1azvlX/AM6xWR8UzHKUYDlWOw5mOpos3+7KKR0Lw==} + peerDependencies: + esbuild: ~0.14.29 + dependencies: + esbuild: 0.17.19 + dev: true + /@anatine/esbuild-decorators@0.2.19(esbuild@0.17.5): resolution: {integrity: sha512-pyj6ULyMacyzpDqlnbS2OCkOqxcVgk8IqiTMRJ5CrsF8Yl1azvlX/AM6xWR8UzHKUYDlWOw5mOpos3+7KKR0Lw==} peerDependencies: esbuild: ~0.14.29 dependencies: esbuild: 0.17.5 + dev: false /@angular-devkit/core@14.0.5: resolution: {integrity: sha512-/CUGi6QLwh79FvsOY7M+1LQL3asZsbQW/WBd5f1iu5y7TLNqCwo+wOb0ZXLDNPw45vYBxFajtt3ob3U7qx3jNg==} @@ -7286,11 +7298,11 @@ packages: /@bcoe/v8-coverage@0.2.3: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} - /@cats-cradle/base-nodejs@1.0.3(@babel/core@7.22.20)(@types/node@20.8.2)(esbuild@0.17.5)(ts-node@10.9.1)(typescript@5.1.3): + /@cats-cradle/base-nodejs@1.0.3(@babel/core@7.22.20)(@types/node@20.8.2)(esbuild@0.17.19)(ts-node@10.9.1)(typescript@5.1.3): resolution: {integrity: sha512-t/dwRt45XCOFNU84HBIy200Y7avUKwycQDSJXXB7Yi5AHAmZJnsrbKFpbbllpE3s79D7uZCQxywWFoiKSi/PqQ==} dependencies: jest: 29.5.0(@types/node@20.8.2)(ts-node@10.9.1) - ts-jest: 29.1.0(@babel/core@7.22.20)(esbuild@0.17.5)(jest@29.5.0)(typescript@5.1.3) + ts-jest: 29.1.0(@babel/core@7.22.20)(esbuild@0.17.19)(jest@29.5.0)(typescript@5.1.3) transitivePeerDependencies: - '@babel/core' - '@jest/types' @@ -7308,10 +7320,10 @@ packages: resolution: {integrity: sha512-DREu86GTzX+ZY0VXg9zZVMQ0xqeRUb6jBGnoyNE8Ndu3OOiUbsIHQqqAvcs6x5XOy8uaMyuO8As1TPyVFO0xlg==} hasBin: true dependencies: - '@anatine/esbuild-decorators': 0.2.19(esbuild@0.17.5) - '@cats-cradle/base-nodejs': 1.0.3(@babel/core@7.22.20)(@types/node@20.8.2)(esbuild@0.17.5)(ts-node@10.9.1)(typescript@5.1.3) + '@anatine/esbuild-decorators': 0.2.19(esbuild@0.17.19) + '@cats-cradle/base-nodejs': 1.0.3(@babel/core@7.22.20)(@types/node@20.8.2)(esbuild@0.17.19)(ts-node@10.9.1)(typescript@5.1.3) commander: 10.0.1 - esbuild: 0.17.5 + esbuild: 0.17.19 transitivePeerDependencies: - '@babel/core' - '@jest/types' @@ -29218,7 +29230,7 @@ packages: yargs-parser: 18.1.3 dev: true - /ts-jest@29.1.0(@babel/core@7.22.20)(esbuild@0.17.5)(jest@29.5.0)(typescript@5.1.3): + /ts-jest@29.1.0(@babel/core@7.22.20)(esbuild@0.17.19)(jest@29.5.0)(typescript@5.1.3): resolution: {integrity: sha512-ZhNr7Z4PcYa+JjMl62ir+zPiNJfXJN6E8hSLnaUKhOgqcn8vb3e537cpkd0FuAfRK3sR1LSqM1MOhliXNgOFPA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -29241,7 +29253,7 @@ packages: dependencies: '@babel/core': 7.22.20 bs-logger: 0.2.6 - esbuild: 0.17.5 + esbuild: 0.17.19 fast-json-stable-stringify: 2.1.0 jest: 29.5.0(@types/node@20.8.2)(ts-node@10.9.1) jest-util: 29.7.0 diff --git a/common/config/rush/repo-state.json b/common/config/rush/repo-state.json index 0c48a024..12f993df 100644 --- a/common/config/rush/repo-state.json +++ b/common/config/rush/repo-state.json @@ -1,5 +1,5 @@ // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. { - "pnpmShrinkwrapHash": "8c9243aa1c7fedfa20d02ca87c93357469e14692", + "pnpmShrinkwrapHash": "418ba6bcdb56844db65e8a29967843e698b30e80", "preferredVersionsHash": "8ae0ba5bd02ec9c5763773a15e27aee08a6567f6" } diff --git a/platform/bundlers/CHANGELOG.md b/platform/bundlers/CHANGELOG.md deleted file mode 100644 index 1f40c5a3..00000000 --- a/platform/bundlers/CHANGELOG.md +++ /dev/null @@ -1,30 +0,0 @@ -# Change Log - @cats-cradle/bundlers - -This log was last generated on Sat, 14 Oct 2023 22:49:51 GMT and should not be manually modified. - -## 0.0.5 -Sat, 14 Oct 2023 22:49:51 GMT - -### Patches - -- exclude chromium from build - -## 0.0.4 -Wed, 04 Oct 2023 03:44:49 GMT - -### Patches - -- bump packages - -## 0.0.3 -Mon, 24 Apr 2023 06:43:20 GMT - -_Version update only_ - -## 0.0.2 -Thu, 13 Apr 2023 01:49:51 GMT - -### Patches - -- update packages - diff --git a/platform/bundlers/src/__tests__/nestjs-bundler.test.ts b/platform/bundlers/src/__tests__/nestjs-bundler.test.ts deleted file mode 100644 index 699e4a9b..00000000 --- a/platform/bundlers/src/__tests__/nestjs-bundler.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { rmSync, readFileSync } from 'fs'; -import { join } from 'path'; -import { nestJsBundler } from '../profiles/nestjs-bundler'; - -describe('nestJsBundler', () => { - it('should build src to dist', async () => { - rmSync('./dist', { recursive: true, force: true }); - const result = await nestJsBundler({ - projectRoot: join(__dirname, 'test-data/nestjs'), - }); - const file = readFileSync( - join(__dirname, 'test-data/nestjs/dist/index.js'), - 'utf-8', - ); - expect(result).toEqual(undefined); - expect(file).toContain('console.log("test");'); - }); -}); diff --git a/platform/bundlers/src/__tests__/test-data/nestjs/src/index.ts b/platform/bundlers/src/__tests__/test-data/nestjs/src/index.ts deleted file mode 100644 index a2248efc..00000000 --- a/platform/bundlers/src/__tests__/test-data/nestjs/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -console.log('test'); diff --git a/platform/bundlers/src/bin-nestjs.ts b/platform/bundlers/src/bin-nestjs.ts deleted file mode 100644 index 4ba7b0fe..00000000 --- a/platform/bundlers/src/bin-nestjs.ts +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env node -import { join } from 'path'; -import { nestJsBundler } from './profiles/nestjs-bundler'; - -const projectRoot = process.cwd(); - -nestJsBundler({ projectRoot }); diff --git a/platform/bundlers/src/index.ts b/platform/bundlers/src/index.ts deleted file mode 100644 index 5765f3be..00000000 --- a/platform/bundlers/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { nestJsBundler } from './profiles/nestjs-bundler'; diff --git a/platform/bundlers/src/profiles/nestjs-bundler.ts b/platform/bundlers/src/profiles/nestjs-bundler.ts deleted file mode 100644 index 6d9ae653..00000000 --- a/platform/bundlers/src/profiles/nestjs-bundler.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { build } from 'esbuild'; -import { join } from 'path'; -import { rmSync } from 'fs'; -import { esbuildDecorators } from '@anatine/esbuild-decorators'; - -export interface NestJsBundlerOptions { - projectRoot: string; -} - -export async function nestJsBundler(options: NestJsBundlerOptions) { - const tsconfigPath = join(options.projectRoot, 'tsconfig.json'); - const entryPoint = join(options.projectRoot, 'src', 'index.ts'); - const outDir = join(options.projectRoot, 'dist'); - - // remove previous bundle - rmSync(outDir, { recursive: true, force: true }); - - try { - const externalModules = [ - // nestjs - 'express', - 'reflect-metadata', - 'rxjs', - 'source-map-support', - 'aws-lambda', - 'class-transformer', - 'class-validator', - '@nestjs/common', - '@nestjs/core', - '@nestjs/terminus', - '@vendia/serverless-express', - 'express', - 'node-cache', - 'reflect-metadata', - 'rxjs', - // chromium - 'puppeteer', - // 'chrome-aws-lambda', - '@sparticuz/chromium', - 'puppeteer-core', - ]; - - await build({ - entryPoints: [entryPoint], - outdir: outDir, - bundle: true, - platform: 'node', - target: 'es2021', - external: externalModules, - tsconfig: tsconfigPath, - plugins: [esbuildDecorators()], - }); - - console.log('Build succeeded!'); - } catch (e) { - console.error(`Build failed: ${e}`); - process.exit(1); - } -} diff --git a/platform/bundlers/.eslintrc.js b/platform/create-bundle/.eslintrc.js similarity index 100% rename from platform/bundlers/.eslintrc.js rename to platform/create-bundle/.eslintrc.js diff --git a/platform/bundlers/CHANGELOG.json b/platform/create-bundle/CHANGELOG.json similarity index 63% rename from platform/bundlers/CHANGELOG.json rename to platform/create-bundle/CHANGELOG.json index 91b2ea9a..0b417117 100644 --- a/platform/bundlers/CHANGELOG.json +++ b/platform/create-bundle/CHANGELOG.json @@ -1,6 +1,35 @@ { - "name": "@cats-cradle/bundlers", + "name": "@cats-cradle/create-bundle", "entries": [ + { + "version": "1.0.0", + "tag": "@cats-cradle/create-bundle_v1.0.0", + "date": "Sat, 28 Oct 2023 03:02:33 GMT", + "comments": { + "major": [ + { + "comment": "rename package from bundlers to create-bundle" + } + ], + "patch": [ + { + "comment": "rename package from bundlers to create-bundle" + } + ] + } + }, + { + "version": "0.1.0", + "tag": "@cats-cradle/bundlers_v0.1.0", + "date": "Sat, 28 Oct 2023 02:49:38 GMT", + "comments": { + "minor": [ + { + "comment": "Add support for --watch flag" + } + ] + } + }, { "version": "0.0.5", "tag": "@cats-cradle/bundlers_v0.0.5", diff --git a/platform/create-bundle/CHANGELOG.md b/platform/create-bundle/CHANGELOG.md new file mode 100644 index 00000000..83d6913d --- /dev/null +++ b/platform/create-bundle/CHANGELOG.md @@ -0,0 +1,48 @@ +# Change Log - @cats-cradle/create-bundle + +This log was last generated on Sat, 28 Oct 2023 03:02:33 GMT and should not be manually modified. + +## 1.0.0 +Sat, 28 Oct 2023 03:02:33 GMT + +### Breaking changes + +- rename package from bundlers to create-bundle + +### Patches + +- rename package from bundlers to create-bundle + +## 0.1.0 +Sat, 28 Oct 2023 02:49:38 GMT + +### Minor changes + +- Add support for --watch flag + +## 0.0.5 +Sat, 14 Oct 2023 22:49:51 GMT + +### Patches + +- exclude chromium from build + +## 0.0.4 +Wed, 04 Oct 2023 03:44:49 GMT + +### Patches + +- bump packages + +## 0.0.3 +Mon, 24 Apr 2023 06:43:20 GMT + +_Version update only_ + +## 0.0.2 +Thu, 13 Apr 2023 01:49:51 GMT + +### Patches + +- update packages + diff --git a/platform/bundlers/LICENSE.md b/platform/create-bundle/LICENSE.md similarity index 100% rename from platform/bundlers/LICENSE.md rename to platform/create-bundle/LICENSE.md diff --git a/platform/bundlers/README.md b/platform/create-bundle/README.md similarity index 64% rename from platform/bundlers/README.md rename to platform/create-bundle/README.md index d403e4b4..b49cd666 100644 --- a/platform/bundlers/README.md +++ b/platform/create-bundle/README.md @@ -1,14 +1,14 @@ -# @cats-cradle/bundlers +# @cats-cradle/create-bundle This package contains various bundler profiles. The bundlers bundle another -projects source code to make it ready for deployment. +projects source code to make it ready for distribution. ## Usage Install bundler as devDependency: ```bash -npm install @cats-cradle/bundlers --dev +npm install @cats-cradle/create-bundle --dev ``` Call the binary from within your package.json @@ -17,8 +17,9 @@ Call the binary from within your package.json { "name": "your-package", "scripts": { - "build": "nestjs-bundler", - "cdk:deploy": "nestjs-bundler && cdk deploy" + "build": "create-bundle", + "build:watch": "create-bundle --watch", + "cdk:deploy": "create-bundle && cdk deploy" } } ``` diff --git a/platform/bundlers/jest.config.js b/platform/create-bundle/jest.config.js similarity index 100% rename from platform/bundlers/jest.config.js rename to platform/create-bundle/jest.config.js diff --git a/platform/bundlers/package.json b/platform/create-bundle/package.json similarity index 87% rename from platform/bundlers/package.json rename to platform/create-bundle/package.json index a246f265..cb93fcdd 100644 --- a/platform/bundlers/package.json +++ b/platform/create-bundle/package.json @@ -1,11 +1,11 @@ { - "name": "@cats-cradle/bundlers", + "name": "@cats-cradle/create-bundle", "license": "MIT", "bin": { - "nestjs-bundler": "./dist/bin-nestjs.js" + "create-bundle": "./dist/create-bundle-bin.js" }, "main": "./dist/index.js", - "version": "0.0.5", + "version": "1.0.0", "repository": { "type": "git", "url": "git+https://github.com/hxtree/cats-cradle.git" @@ -34,7 +34,8 @@ "@cats-cradle/base-nodejs": "workspace:*", "@anatine/esbuild-decorators": "~0.2.19", "commander": "~10.0.0", - "esbuild": "~0.17.5" + "esbuild": "~0.17.5", + "chokidar": "~3.5.3" }, "devDependencies": { "@types/jest": "29.5.5", diff --git a/platform/create-bundle/src/__tests__/nestjs-bundler.test.ts b/platform/create-bundle/src/__tests__/nestjs-bundler.test.ts new file mode 100644 index 00000000..1a4ed89f --- /dev/null +++ b/platform/create-bundle/src/__tests__/nestjs-bundler.test.ts @@ -0,0 +1,65 @@ +import { + existsSync, rmSync, readFileSync, writeFileSync, +} from 'fs'; +import { join } from 'path'; +import { NestJsBundler } from '../profiles/nestjs-bundler'; + +function sleep(ms: number) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} + +describe('NestJsBundler', () => { + const distFolder = join(__dirname, 'test-data/nestjs/dist'); + const inputFilename = join(__dirname, 'test-data/nestjs/src/index.ts'); + const outputFilename = join(__dirname, 'test-data/nestjs/dist/index.js'); + + afterEach(async () => { + if (existsSync(outputFilename)) { + rmSync(distFolder, { + recursive: true, + force: true, + }); + } + writeFileSync(inputFilename, "console.log('tear down')"); + }); + + it('should build src to dist', async () => { + const date = Date.now().toString(); + const contents = `console.log("modified ${date}");`; + writeFileSync(inputFilename, contents); + + const bundler = new NestJsBundler({ + projectRoot: join(__dirname, 'test-data/nestjs'), + watch: false, + }); + await bundler.build(); + + const file = readFileSync(outputFilename, 'utf-8'); + expect(file).toContain(contents); + }); + + it('should watch for file changes and rebuild', async () => { + const date = Date.now().toString(); + const contents = `console.log("modified ${date}");`; + + const bundler = new NestJsBundler({ + projectRoot: join(__dirname, 'test-data/nestjs'), + watch: true, + }); + + // wait for watch to init + await sleep(1000); + + writeFileSync(inputFilename, contents); + + // wait for watch to run + await sleep(1000); + + await bundler.close(); + + const file = readFileSync(outputFilename, 'utf-8'); + expect(file).toContain(contents); + }, 3000); +}); diff --git a/platform/create-bundle/src/__tests__/test-data/nestjs/src/index.ts b/platform/create-bundle/src/__tests__/test-data/nestjs/src/index.ts new file mode 100644 index 00000000..b46d41b9 --- /dev/null +++ b/platform/create-bundle/src/__tests__/test-data/nestjs/src/index.ts @@ -0,0 +1 @@ +console.log('tear down'); diff --git a/platform/bundlers/src/__tests__/test-data/nestjs/tsconfig.json b/platform/create-bundle/src/__tests__/test-data/nestjs/tsconfig.json similarity index 100% rename from platform/bundlers/src/__tests__/test-data/nestjs/tsconfig.json rename to platform/create-bundle/src/__tests__/test-data/nestjs/tsconfig.json diff --git a/platform/create-bundle/src/create-bundle-bin.ts b/platform/create-bundle/src/create-bundle-bin.ts new file mode 100644 index 00000000..c136cd54 --- /dev/null +++ b/platform/create-bundle/src/create-bundle-bin.ts @@ -0,0 +1,12 @@ +#!/usr/bin/env node +import { NestJsBundler } from './profiles/nestjs-bundler'; + +const projectRoot = process.cwd(); +const watchMode = process.argv.includes('--watch'); + +// TODO add support for other bundle profiles based on flags as necessary + +new NestJsBundler({ + projectRoot, + watch: !!watchMode, +}); diff --git a/platform/create-bundle/src/index.ts b/platform/create-bundle/src/index.ts new file mode 100644 index 00000000..f65f7860 --- /dev/null +++ b/platform/create-bundle/src/index.ts @@ -0,0 +1 @@ +export { NestJsBundler } from './profiles/nestjs-bundler'; diff --git a/platform/create-bundle/src/profiles/nestjs-bundler.ts b/platform/create-bundle/src/profiles/nestjs-bundler.ts new file mode 100644 index 00000000..30777030 --- /dev/null +++ b/platform/create-bundle/src/profiles/nestjs-bundler.ts @@ -0,0 +1,101 @@ +import { build } from 'esbuild'; +import { join, resolve } from 'path'; +import { rmSync } from 'fs'; +import { esbuildDecorators } from '@anatine/esbuild-decorators'; +import chokidar, { FSWatcher } from 'chokidar'; + +export interface NestJsBundlerOptions { + projectRoot: string; + watch?: boolean; + timeout?: number; +} + +export class NestJsBundler { + options: NestJsBundlerOptions; + + tsConfigPath: string; + + entryPoint: string; + + outDir: string; + + externalModules: string[]; + + watcher: FSWatcher; + + constructor(options: NestJsBundlerOptions) { + this.options = options; + + this.tsConfigPath = join(options.projectRoot, 'tsconfig.json'); + this.entryPoint = join(options.projectRoot, 'src', 'index.ts'); + this.outDir = join(options.projectRoot, 'dist'); + this.externalModules = [ + // nestjs + 'express', + 'reflect-metadata', + 'rxjs', + 'source-map-support', + 'aws-lambda', + 'class-transformer', + 'class-validator', + '@nestjs/common', + '@nestjs/core', + '@nestjs/terminus', + '@vendia/serverless-express', + 'express', + 'node-cache', + 'reflect-metadata', + 'rxjs', + // chromium + 'puppeteer', + // 'chrome-aws-lambda', + '@sparticuz/chromium', + 'puppeteer-core', + ]; + + rmSync(this.outDir, { recursive: true, force: true }); + + if (options.watch) { + const watchDirectory = join(options.projectRoot, 'src'); + + console.log(`Running in watch mode on ${watchDirectory}`); + + this.watcher = chokidar + .watch(watchDirectory) + .on('change', async (event, path) => { + await build({ + entryPoints: [this.entryPoint], + outdir: this.outDir, + bundle: true, + platform: 'node', + target: 'es2021', + external: this.externalModules, + tsconfig: this.tsConfigPath, + plugins: [esbuildDecorators()], + }); + }); + } + } + + async close() { + await this.watcher.close(); + } + + async build() { + try { + await build({ + entryPoints: [this.entryPoint], + outdir: this.outDir, + bundle: true, + platform: 'node', + target: 'es2021', + external: this.externalModules, + tsconfig: this.tsConfigPath, + plugins: [esbuildDecorators()], + }); + } catch (e) { + console.error(`Build failed: ${e}`); + process.exit(1); + } + } +} diff --git a/platform/bundlers/tsconfig.json b/platform/create-bundle/tsconfig.json similarity index 100% rename from platform/bundlers/tsconfig.json rename to platform/create-bundle/tsconfig.json diff --git a/rush.json b/rush.json index 8225d162..68323c22 100644 --- a/rush.json +++ b/rush.json @@ -41,8 +41,8 @@ ], "projects": [ { - "packageName": "@cats-cradle/bundlers", - "projectFolder": "platform/bundlers", + "packageName": "@cats-cradle/create-bundle", + "projectFolder": "platform/create-bundle", "reviewCategory": "rigs", "decoupledLocalDependencies": ["@cats-cradle/eslint-config"], "tags": [], @@ -180,8 +180,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/validation-schemas", "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": [], "shouldPublish": true @@ -193,8 +192,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/validation-schemas", "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": [] }, @@ -205,8 +203,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/validation-schemas", "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": [] }, @@ -238,8 +235,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/validation-schemas", "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"], "shouldPublish": true @@ -251,8 +247,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/eslint-config", "@cats-cradle/nestjs-modules", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"] }, @@ -262,8 +257,7 @@ "reviewCategory": "apis", "decoupledLocalDependencies": [ "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"] }, @@ -274,8 +268,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/validation-schemas", "@cats-cradle/eslint-config", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"], "shouldPublish": true @@ -287,8 +280,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/eslint-config", "@cats-cradle/nestjs-modules", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"], "shouldPublish": true @@ -300,8 +292,7 @@ "decoupledLocalDependencies": [ "@cats-cradle/eslint-config", "@cats-cradle/nestjs-modules", - "@cats-cradle/create-artifact", - "@cats-cradle/bundlers" + "@cats-cradle/create-artifact" ], "tags": ["deploy-app"] }