Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(CRWA): convert to ESM #8159

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/create-redwood-app/build.mjs
Expand Up @@ -9,6 +9,7 @@ const result = await esbuild.build({
bundle: true,
minify: true,

format: 'esm',
platform: 'node',
target: ['node18'],
packages: 'external',
Expand Down
1 change: 1 addition & 0 deletions packages/create-redwood-app/package.json
@@ -1,6 +1,7 @@
{
"name": "create-redwood-app",
"version": "5.0.0",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/redwoodjs/redwood.git",
Expand Down
19 changes: 14 additions & 5 deletions packages/create-redwood-app/src/create-redwood-app.js
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import path from 'path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { trace, SpanStatusCode } from '@opentelemetry/api'
import chalk from 'chalk'
Expand All @@ -14,14 +15,22 @@ import yargs from 'yargs/yargs'

import { RedwoodTUI, ReactiveTUIContent, RedwoodStyling } from '@redwoodjs/tui'

import { name, version } from '../package'

// In ESM, for relative paths, the extension is important.
// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#mandatory-file-extensions.
import {
UID,
startTelemetry,
shutdownTelemetry,
recordErrorViaTelemetry,
} from './telemetry'
} from './telemetry.js'

// JSON modules aren't stable yet, but if they were we could use them instead.
// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#json-modules.
const __dirname = path.dirname(fileURLToPath(import.meta.url))

const { name, version } = fs.readJSONSync(
path.resolve(__dirname, '../package.json')
)

// Telemetry
const { telemetry } = Parser(hideBin(process.argv))
Expand Down Expand Up @@ -168,7 +177,7 @@ async function executeCompatibilityCheck(templateDir) {
*/
function checkNodeAndYarnVersion(templateDir) {
return new Promise((resolve) => {
const { engines } = require(path.join(templateDir, 'package.json'))
const { engines } = fs.readJSONSync(path.join(templateDir, 'package.json'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use require now, we'd have to create it using createRequire.


checkNodeVersionCb(engines, (_error, result) => {
return resolve([result.isSatisfied, result.versions])
Expand Down
10 changes: 9 additions & 1 deletion packages/create-redwood-app/src/telemetry.js
@@ -1,3 +1,6 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api'
import opentelemetry, { SpanStatusCode } from '@opentelemetry/api'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
Expand All @@ -9,10 +12,15 @@ import {
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import ci from 'ci-info'
import envinfo from 'envinfo'
import fs from 'fs-extra'
import system from 'systeminformation'
import { v4 as uuidv4 } from 'uuid'

import { name as packageName, version as packageVersion } from '../package'
// JSON modules aren't stable yet, but if they were we could use them instead.
// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#json-modules.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was interesting to me, so I did some digging.
It's still experimental even in Node 20: https://nodejs.org/docs/v20.0.0/api/esm.html#json-modules

It looks like it'll change syntax slightly before it fully ships (assert will change to with)
https://github.com/tc39/proposal-import-attributes
Scrolling to the very bottom there's an interesting "History" section. That section also mentions that compatibility with assert will probably remain even after the official syntax switches to with

TIL 🙂

const { name: packageName, version: packageVersion } = fs.readJSONSync(
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../package.json')
)

/**
* @type NodeTracerProvider
Expand Down