Skip to content

Commit bbeafc9

Browse files
authored
feat(cli): Build as ESM (#52)
1 parent c85a596 commit bbeafc9

File tree

81 files changed

+487
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+487
-313
lines changed

packages/cli-helpers/src/lib/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const getInstalledRedwoodVersion = () => {
3737
const packageJson = require('../../package.json')
3838
return packageJson.version
3939
} catch {
40-
console.error(colors.error('Could not find installed redwood version'))
40+
console.error(colors.error('Could not find installed Redmix version'))
4141
process.exit(1)
4242
}
4343
}

packages/cli-packages/dataMigrate/vitest.config.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export default defineConfig({
77
// TODO: Remove this when we've made babel-config ESM+CJS dual build
88
// https://stackoverflow.com/a/77439684/88106
99
alias: {
10-
'@redmix/babel-config': path.resolve(__dirname, '../../babel-config/src'),
10+
'@redmix/babel-config': path.resolve(
11+
import.meta.dirname,
12+
'../../babel-config/src',
13+
),
1114
},
1215
},
1316
})

packages/cli/build.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { fileURLToPath } from 'node:url'
44

55
import fg from 'fast-glob'
66

7-
import { build, defaultIgnorePatterns } from '@redmix/framework-tools'
7+
import { buildEsm, defaultIgnorePatterns } from '@redmix/framework-tools'
88

9-
await build()
9+
await buildEsm()
1010

1111
// The CLI depends on assets like templates files
1212
// that esbuild won't copy over. So we do it manually here.

packages/cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"directory": "packages/cli"
99
},
1010
"license": "MIT",
11+
"type": "module",
1112
"bin": {
1213
"redwood": "./dist/index.js",
1314
"rw": "./dist/index.js",
@@ -52,7 +53,7 @@
5253
"change-case": "4.1.2",
5354
"ci-info": "4.0.0",
5455
"concurrently": "8.2.2",
55-
"configstore": "3.1.5",
56+
"configstore": "7.0.0",
5657
"core-js": "3.38.1",
5758
"cross-env": "7.0.3",
5859
"decamelize": "5.0.1",

packages/cli/src/__tests__/cwd.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ console.log('BASE_DIR:', BASE_DIR)
1010
console.log('CLI:', CLI)
1111

1212
function rw(args, options) {
13-
console.log('test cwd', BASE_DIR)
14-
console.log('args', args)
15-
console.log('options', options)
1613
const { status, stdout, stderr } = spawnSync('node', [CLI, ...args], {
1714
cwd: BASE_DIR,
1815
...options,

packages/cli/src/commands/buildHandler.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import path from 'path'
1+
import { createRequire } from 'node:module'
2+
import path from 'node:path'
23

34
import execa from 'execa'
45
import fs from 'fs-extra'
@@ -89,6 +90,12 @@ export const handler = async ({
8990
side.includes('web') && {
9091
title: 'Building Web...',
9192
task: async () => {
93+
// Disable the new warning in Vite v5 about the CJS build being deprecated
94+
// so that users don't have to see it when this command is called with --verbose
95+
process.env.VITE_CJS_IGNORE_WARNING = 'true'
96+
97+
const createdRequire = createRequire(import.meta.url)
98+
9299
// @NOTE: we're using the vite build command here, instead of the
93100
// buildWeb function directly because we want the process.cwd to be
94101
// the web directory, not the root of the project.
@@ -98,12 +105,8 @@ export const handler = async ({
98105
// it could affect other things that run in parallel while building.
99106
// We don't have any parallel tasks right now, but someone might add
100107
// one in the future as a performance optimization.
101-
//
102-
// Disable the new warning in Vite v5 about the CJS build being deprecated
103-
// so that users don't have to see it when this command is called with --verbose
104-
process.env.VITE_CJS_IGNORE_WARNING = 'true'
105108
await execa(
106-
`node ${require.resolve(
109+
`node ${createdRequire.resolve(
107110
'@redmix/vite/bins/rw-vite-build.mjs',
108111
)} --webDir="${rwjsPaths.web.base}" --verbose=${verbose}`,
109112
{

packages/cli/src/commands/check.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,25 @@ export const aliases = ['diagnostics']
88
export const description =
99
'Get structural diagnostics for a Redwood project (experimental)'
1010

11-
export const handler = () => {
12-
recordTelemetryAttributes({
13-
command: 'check',
14-
})
15-
// Deep dive
16-
//
17-
// It seems like we have to use `require` here instead of `await import`
18-
// because of how Babel builds the `DiagnosticSeverity` export in `@redmix/structure`:
19-
//
20-
// ```js
21-
// _Object$defineProperty(exports, "DiagnosticSeverity", {
22-
// enumerable: true,
23-
// get: function () {
24-
// return _vscodeLanguageserverTypes.DiagnosticSeverity;
25-
// }
26-
// });
27-
// ```
28-
//
29-
// I'm not sure why, but with `await import`, `DiagnosticSeverity` is `undefined`
30-
// so it seems like `await import` doesn't execute the getter function.
31-
const { printDiagnostics, DiagnosticSeverity } = require('@redmix/structure')
11+
export const handler = async () => {
12+
recordTelemetryAttributes({ command: 'check' })
13+
14+
const { printDiagnostics, DiagnosticSeverity } = (
15+
await import('@redmix/structure')
16+
).default
17+
18+
console.log('DiagnosticServerity', DiagnosticSeverity)
3219

3320
printDiagnostics(getPaths().base, {
3421
getSeverityLabel: (severity) => {
3522
if (severity === DiagnosticSeverity.Error) {
3623
return c.error('error')
3724
}
25+
3826
if (severity === DiagnosticSeverity.Warning) {
3927
return c.warning('warning')
4028
}
29+
4130
return c.info('info')
4231
},
4332
})

packages/cli/src/commands/consoleHandler.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import path from 'path'
2-
import repl from 'repl'
1+
import { createRequire } from 'node:module'
2+
import path from 'node:path'
3+
import repl from 'node:repl'
34

45
import fs from 'fs-extra'
56

@@ -11,7 +12,8 @@ import { getPaths } from '../lib/index.js'
1112
const paths = getPaths()
1213

1314
const loadPrismaClient = (replContext) => {
14-
const { db } = require(path.join(paths.api.lib, 'db'))
15+
const createdRequire = createRequire(import.meta.url)
16+
const { db } = createdRequire(path.join(paths.api.lib, 'db'))
1517
// workaround for Prisma issue: https://github.com/prisma/prisma/issues/18292
1618
db[Symbol.for('nodejs.util.inspect.custom')] = 'PrismaClient'
1719
replContext.db = db

packages/cli/src/commands/destroy/cell/cell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { files as cellFiles } from '../../generate/cell/cell.js'
1+
import { files as cellFiles } from '../../generate/cell/cellHandler.js'
22
import { createYargsForComponentDestroy, createHandler } from '../helpers.js'
33

44
export const { command, description, builder } = createYargsForComponentDestroy(

packages/cli/src/commands/destroy/scaffold/scaffold.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createHandler } from '../helpers'
1+
import { createHandler } from '../helpers.js'
22

33
export const command = 'scaffold <model>'
44
export const description =

0 commit comments

Comments
 (0)