Skip to content

Commit 87cc2ab

Browse files
committed
feat(esbuild): added esbuild transpiler and make it default
1 parent 890f3d0 commit 87cc2ab

16 files changed

+1260
-1319
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.21.2

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"typescript.tsdk": "node_modules/typescript/lib",
1818
"editor.codeActionsOnSave": {
19-
"source.fixAll.eslint": true
19+
"source.fixAll.eslint": "explicit"
2020
},
2121
"files.exclude": {
2222
"**/.classpath": true,

README.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Migration library for `Mongodb` and `Mongoose` written in `TypeScript`
1616
- ACID transactions provided by MongoDB
1717
- `error` and `success` logs for `up`/`down` migrations
1818
- Infinite rrror log with `append` NodeJS streaming technique
19-
- 100% TypeScript support with JIT compilation provided by [@gapi/cli](https://github.com/Stradivario/gapi-cli) and [ParcelJS](https://parceljs.org)
19+
- 100% TypeScript support with JIT compilation provided by [esbuild](https://esbuild.github.io/)
2020

2121
## Installation
2222

@@ -61,6 +61,7 @@ export default async () => {
6161
migrationsDir: './migrations',
6262
defaultTemplate: 'es6',
6363
typescript: true,
64+
builder: 'ESBUILD',
6465
outDir: './.xmigrate',
6566
/* Custom datetime formatting can be applied like so */
6667
// dateTimeFormat: () => new Date().toISOString(),
@@ -255,7 +256,7 @@ export async function down(client: MongoClient) {
255256
}
256257
```
257258

258-
## TypeScript migrations
259+
## TypeScript migrations (Deprecated use option `builder: 'ESBUILD' in your xmigrate config file`)
259260

260261
To be able to run migrations with TypeScript you need to set `typescript: true` inside `xmigrate.js` and install `@gapi/cli` globally
261262

@@ -367,12 +368,6 @@ Up migration error log
367368

368369
When you change your configuration file to `xmigrate.ts` it will automatically transpile to `ES5` and will be loaded
369370

370-
This type of configuration requires `@gapi/cli` to be installed since we will transpile configuration with it!
371-
372-
```bash
373-
npm i -g @gapi/cli
374-
```
375-
376371
```typescript
377372
import { Config } from '@rxdi/xmigrate';
378373

package-lock.json

+168
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@rxdi/core": "^0.7.37",
2525
"chalk": "2.4.2",
26+
"esbuild": "^0.20.2",
2627
"esm": "3.2.25",
2728
"mongodb": "3.3.3",
2829
"mongoose": "5.7.6"

src/default.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Config } from './injection.tokens';
1+
import { BuilderType, Config } from './injection.tokens';
22

33
export const DEFAULT_CONFIG: Config = {
44
changelogCollectionName: 'migrations',
55
migrationsDir: 'migrations',
66
defaultTemplate: 'es6',
77
typescript: true,
88
outDir: './.xmigrate',
9+
builder: BuilderType.ESBUILD,
910
// dateTimeFormat: () => '1212',
1011
logger: {
1112
folder: './migrations-log',

src/helpers/log-factory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Logger {
2525
});
2626
}
2727
log(res: unknown) {
28-
return new Promise((resolve) => {
28+
return new Promise<void | Error>((resolve) => {
2929
if (!this.successFinished) {
3030
return this.successLogger.write(
3131
this.getLogTemplate(res, '🚀'),
@@ -36,7 +36,7 @@ export class Logger {
3636
});
3737
}
3838
error(res: unknown) {
39-
return new Promise((resolve) => {
39+
return new Promise<void | Error>((resolve) => {
4040
if (!this.errorFinished) {
4141
return this.errorLogger.write(this.getLogTemplate(res, '🔥'), resolve);
4242
}

src/helpers/typescript-builder.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
import { spawn } from 'child_process';
2+
import { exit } from 'process';
23

3-
export const TranspileTypescript = (paths: string[], outDir: string) => {
4+
/* Deprecated */
5+
export const TranspileTypescript = (entryPoints: string[], outdir: string) => {
6+
console.warn(
7+
'***Deprecated Warning*** using `gapi` as a build for migrations is deprecated consider using builder type esbuild',
8+
);
49
return new Promise((resolve) => {
510
const child = spawn('npx', [
611
'gapi',
712
'build',
813
'--glob',
9-
`${paths.toString()}`,
14+
`${entryPoints.toString()}`,
1015
'--outDir',
11-
outDir,
16+
outdir,
1217
]);
1318
// child.stdout.pipe(process.stdout);
1419
child.stderr.pipe(process.stderr);
1520
child.on('close', (code: number) => resolve(code));
1621
});
1722
};
23+
24+
export const TranspileTypescriptESBuild = async (
25+
entryPoints: string[],
26+
outdir: string,
27+
) => {
28+
try {
29+
return (await import('esbuild')).build({
30+
entryPoints,
31+
bundle: true,
32+
sourcemap: true,
33+
minify: false,
34+
platform: 'node',
35+
format: 'cjs',
36+
outdir,
37+
logLevel: 'info',
38+
});
39+
} catch (e) {
40+
console.error(e);
41+
exit(1);
42+
}
43+
};

src/injection.tokens.ts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface LoggerConfig {
2929
};
3030
}
3131

32+
export enum BuilderType {
33+
ESBUILD = 'ESBUILD',
34+
GAPI = 'GAPI',
35+
}
36+
3237
export interface Config {
3338
mongodb: {
3439
url: string;
@@ -44,6 +49,7 @@ export interface Config {
4449
logger?: LoggerConfig;
4550
defaultTemplate?: TemplateTypes;
4651
typescript?: boolean;
52+
builder?: BuilderType;
4753
}
4854

4955
export type Tasks =

src/migrations.module.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import { DEFAULT_CONFIG } from './default.config';
77
import { ensureDir } from './helpers';
88
import { includes, nextOrDefault } from './helpers/args-extractors';
99
import { LogFactory } from './helpers/log-factory';
10-
import { TranspileTypescript } from './helpers/typescript-builder';
1110
import {
11+
TranspileTypescript,
12+
TranspileTypescriptESBuild,
13+
} from './helpers/typescript-builder';
14+
import {
15+
BuilderType,
1216
CommandInjector,
1317
Config,
1418
LoggerConfig,
@@ -82,10 +86,18 @@ export class MigrationsModule {
8286
'./.xmigrate/config.temp',
8387
);
8488
const TranspileAndWriteTemp = async (stats: Stats) => {
85-
await TranspileTypescript(
86-
[`/${configFilename}.ts`],
87-
config.outDir,
88-
);
89+
if (config.builder === BuilderType.GAPI) {
90+
await TranspileTypescript(
91+
[`/${configFilename}.ts`],
92+
config.outDir,
93+
);
94+
} else {
95+
await TranspileTypescriptESBuild(
96+
[`./${configFilename}.ts`],
97+
config.outDir,
98+
);
99+
}
100+
89101
console.log('Transpile complete!');
90102
await promisify(writeFile)(
91103
'./.xmigrate/config.temp',

0 commit comments

Comments
 (0)