Skip to content

Commit 97d7c19

Browse files
authored
feat(dataMigrate): Don't use Babel's require hook (#137)
Use esbuild, through bundle-require, instead of babel to run data migrations 🚨 Breaking: Running data migrations does not include your custom babel config anymore.
1 parent ad27685 commit 97d7c19

File tree

4 files changed

+66
-81
lines changed

4 files changed

+66
-81
lines changed

packages/cli-packages/dataMigrate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dependencies": {
2828
"@cedarjs/babel-config": "workspace:*",
2929
"@cedarjs/project-config": "workspace:*",
30+
"bundle-require": "^5.1.0",
3031
"chalk": "4.1.2",
3132
"dotenv-defaults": "5.0.2",
3233
"execa": "5.1.1",

packages/cli-packages/dataMigrate/src/__tests__/upHandler.test.ts

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ jest.mock('fs', () => require('memfs').fs)
3131

3232
const mockDataMigrations: { current: any[] } = { current: [] }
3333

34+
jest.mock('bundle-require', () => {
35+
return {
36+
bundleRequire: ({ filepath }: { filepath: string }) => {
37+
return {
38+
mod: {
39+
default: () => {
40+
if (filepath.endsWith('20230822075443-wip.ts')) {
41+
throw new Error('oops')
42+
}
43+
},
44+
},
45+
}
46+
},
47+
}
48+
})
49+
3450
jest.mock(
3551
'/redwood-app/api/dist/lib/db.js',
3652
() => {
@@ -71,74 +87,6 @@ jest.mock(
7187
{ virtual: true },
7288
)
7389

74-
jest.mock(
75-
'/redwood-app/api/db/dataMigrations/20230822075442-wip.ts',
76-
() => {
77-
return { default: () => {} }
78-
},
79-
{
80-
virtual: true,
81-
},
82-
)
83-
84-
jest.mock(
85-
'\\redwood-app\\api\\db\\dataMigrations\\20230822075442-wip.ts',
86-
() => {
87-
return { default: () => {} }
88-
},
89-
{
90-
virtual: true,
91-
},
92-
)
93-
94-
jest.mock(
95-
'/redwood-app/api/db/dataMigrations/20230822075443-wip.ts',
96-
() => {
97-
return {
98-
default: () => {
99-
throw new Error('oops')
100-
},
101-
}
102-
},
103-
{
104-
virtual: true,
105-
},
106-
)
107-
108-
jest.mock(
109-
'\\redwood-app\\api\\db\\dataMigrations\\20230822075443-wip.ts',
110-
() => {
111-
return {
112-
default: () => {
113-
throw new Error('oops')
114-
},
115-
}
116-
},
117-
{
118-
virtual: true,
119-
},
120-
)
121-
122-
jest.mock(
123-
'/redwood-app/api/db/dataMigrations/20230822075444-wip.ts',
124-
() => {
125-
return { default: () => {} }
126-
},
127-
{
128-
virtual: true,
129-
},
130-
)
131-
132-
jest.mock(
133-
'\\redwood-app\\api\\db\\dataMigrations\\20230822075444-wip.ts',
134-
() => {
135-
return { default: () => {} }
136-
},
137-
{
138-
virtual: true,
139-
},
140-
)
141-
14290
const RWJS_CWD = process.env.RWJS_CWD
14391

14492
beforeAll(() => {
@@ -196,7 +144,7 @@ describe('upHandler', () => {
196144
)
197145
})
198146

199-
it("noops if there's no pending migrations", async () => {
147+
it('noops if there are no pending migrations', async () => {
200148
mockDataMigrations.current = [ranDataMigration]
201149

202150
vol.fromNestedJSON(

packages/cli-packages/dataMigrate/src/commands/upHandler.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import fs from 'fs'
22
import path from 'path'
33

44
import type { PrismaClient } from '@prisma/client'
5+
import { bundleRequire } from 'bundle-require'
56
import { Listr } from 'listr2'
67

7-
import { registerApiSideBabelHook } from '@cedarjs/babel-config'
8-
import { getPaths } from '@cedarjs/project-config'
8+
import { getPaths, resolveFile } from '@cedarjs/project-config'
99

1010
import c from '../lib/colors'
1111
import type { DataMigrateUpOptions, DataMigration } from '../types'
@@ -15,7 +15,6 @@ export async function handler({
1515
distPath,
1616
}: DataMigrateUpOptions) {
1717
let db: any
18-
let requireHookRegistered = false
1918

2019
if (importDbClientFromDist) {
2120
if (!fs.existsSync(distPath)) {
@@ -41,10 +40,27 @@ export async function handler({
4140

4241
db = (await import(distLibDbPath)).db
4342
} else {
44-
registerApiSideBabelHook()
45-
requireHookRegistered = true
43+
const dbPath = resolveFile(path.join(getPaths().api.lib, 'db'))
4644

47-
db = require(path.join(getPaths().api.lib, 'db')).db
45+
if (!dbPath) {
46+
console.error(`Can't find your db file in ${getPaths().api.lib}`)
47+
process.exitCode = 1
48+
return
49+
}
50+
51+
// Needed plugins:
52+
// - babel-plugin-module-resolver: 'src' -> './src' etc
53+
// - rwjs-babel-directory-named-modules: 'src/services/userExamples' -> './src/services/userExamples/userExamples.ts'
54+
// - babel-plugin-auto-import: `import gql from 'graphql-tag`, `import { context } from '@cedarjs/context`
55+
// - babel-plugin-graphql-tag: ???
56+
// - rwjs-babel-glob-import-dir: Handle imports like src/services/**/*.{js,ts}
57+
// - rwjs-babel-otel-wrapping: Wrap code in OpenTelemetry spans
58+
const { mod } = await bundleRequire({
59+
filepath: dbPath,
60+
// TODO: Add plugins
61+
})
62+
63+
db = mod.db
4864
}
4965

5066
const pendingDataMigrations = await getPendingDataMigrations(db)
@@ -71,10 +87,6 @@ export async function handler({
7187
}
7288
},
7389
async task() {
74-
if (!requireHookRegistered) {
75-
registerApiSideBabelHook()
76-
}
77-
7890
try {
7991
const { startedAt, finishedAt } = await runDataMigration(
8092
db,
@@ -187,10 +199,15 @@ function sortDataMigrationsByVersion(
187199
}
188200

189201
async function runDataMigration(db: PrismaClient, dataMigrationPath: string) {
190-
const dataMigration = require(dataMigrationPath)
202+
const { mod } = await bundleRequire({
203+
filepath: dataMigrationPath,
204+
// TODO: Add plugins
205+
})
206+
207+
const dataMigration = mod.default
191208

192209
const startedAt = new Date()
193-
await dataMigration.default({ db })
210+
await dataMigration({ db })
194211
const finishedAt = new Date()
195212

196213
return { startedAt, finishedAt }

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,7 @@ __metadata:
26182618
"@prisma/client": "npm:5.20.0"
26192619
"@types/fs-extra": "npm:11.0.4"
26202620
"@types/yargs": "npm:17.0.33"
2621+
bundle-require: "npm:^5.1.0"
26212622
chalk: "npm:4.1.2"
26222623
dotenv-defaults: "npm:5.0.2"
26232624
execa: "npm:5.1.1"
@@ -13436,6 +13437,17 @@ __metadata:
1343613437
languageName: node
1343713438
linkType: hard
1343813439

13440+
"bundle-require@npm:^5.1.0":
13441+
version: 5.1.0
13442+
resolution: "bundle-require@npm:5.1.0"
13443+
dependencies:
13444+
load-tsconfig: "npm:^0.2.3"
13445+
peerDependencies:
13446+
esbuild: ">=0.18"
13447+
checksum: 10c0/8bff9df68eb686f05af952003c78e70ffed2817968f92aebb2af620cc0b7428c8154df761d28f1b38508532204278950624ef86ce63644013dc57660a9d1810f
13448+
languageName: node
13449+
linkType: hard
13450+
1343913451
"busboy@npm:^1.6.0":
1344013452
version: 1.6.0
1344113453
resolution: "busboy@npm:1.6.0"
@@ -21533,6 +21545,13 @@ __metadata:
2153321545
languageName: node
2153421546
linkType: hard
2153521547

21548+
"load-tsconfig@npm:^0.2.3":
21549+
version: 0.2.5
21550+
resolution: "load-tsconfig@npm:0.2.5"
21551+
checksum: 10c0/bf2823dd26389d3497b6567f07435c5a7a58d9df82e879b0b3892f87d8db26900f84c85bc329ef41c0540c0d6a448d1c23ddc64a80f3ff6838b940f3915a3fcb
21552+
languageName: node
21553+
linkType: hard
21554+
2153621555
"local-pkg@npm:^0.5.0":
2153721556
version: 0.5.0
2153821557
resolution: "local-pkg@npm:0.5.0"

0 commit comments

Comments
 (0)