Skip to content

Commit 68ed27a

Browse files
authored
chore(esm): convert @redwoodjs/project-config to ESM (redwoodjs#9870)
I'm still working through a few things. The motivation for tackling `@redwoodjs/project-config` first was 1) most of our other package's depend on it 2) it's small 3) it would make merging the PR that converts the CLI's Jest tests to Vitest easier because Vitest can't mock require (see redwoodjs#9863). I used [`arethetypeswrong/cli`](https://github.com/arethetypeswrong/arethetypeswrong.github.io) extensively. Right now I'm deeming the "Masquerading as ESM" error it emits acceptable. The code between the ESM and CJS files doesn't differ in functionality, only syntax; shipping two declaration copies of all the declaration files is shipping extra code. Mark Erikson did something similar at first at least here: > Unfortunately, no build tool that I knew of at that time did this by default, and the idea of shipping 99%-duplicate typedefs bothered me. So, I opted to not try to fix this "FalseCJS" issue for our packages (at least for the time being). (Source: https://blog.isquaredsoftware.com/2023/08/esm-modernization-lessons/#typescript-declarations.) Note that FalseCJS's fancier name is "Masquerading as CJS". We have "Masquerading as ESM", not CJS. I'm not sure if it's an issue that it's flipped yet. ``` $ attw ./redwoodjs-project-config.tgz @redwoodjs/project-config v6.0.7 Build tools: - [email protected] - [email protected] 👺 Import resolved to an ESM type declaration file, but a CommonJS JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md ┌───────────────────┬─────────────────────────────┐ │ │ "@redwoodjs/project-config" │ ├───────────────────┼─────────────────────────────┤ │ node10 │ 🟢 │ ├───────────────────┼─────────────────────────────┤ │ node16 (from CJS) │ 👺 Masquerading as ESM │ ├───────────────────┼─────────────────────────────┤ │ node16 (from ESM) │ 🟢 (ESM) │ ├───────────────────┼─────────────────────────────┤ │ bundler │ 🟢 │ └───────────────────┴─────────────────────────────┘ ``` Regarding the `.js` extensions (which are necessary for relative imports in ESM) in TS code, see redwoodjs#8456.
1 parent 49347fd commit 68ed27a

File tree

10 files changed

+46
-36
lines changed

10 files changed

+46
-36
lines changed

packages/project-config/build.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
3+
import * as esbuild from 'esbuild'
4+
5+
const options = {
6+
entryPoints: ['./src/index.ts'],
7+
outdir: 'dist',
8+
9+
platform: 'node',
10+
target: ['node20'],
11+
bundle: true,
12+
packages: 'external',
13+
14+
logLevel: 'info',
15+
metafile: true,
16+
}
17+
18+
await esbuild.build({
19+
...options,
20+
format: 'esm',
21+
outExtension: { '.js': '.mjs' },
22+
})
23+
24+
await esbuild.build({
25+
...options,
26+
format: 'cjs',
27+
outExtension: { '.js': '.cjs' },
28+
})

packages/project-config/build.mjs

-22
This file was deleted.

packages/project-config/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
"directory": "packages/project-config"
88
},
99
"license": "MIT",
10+
"type": "module",
1011
"exports": {
11-
".": "./dist/index.js"
12+
"types": "./dist/index.d.ts",
13+
"import": "./dist/index.mjs",
14+
"default": "./dist/index.cjs"
1215
},
1316
"types": "./dist/index.d.ts",
1417
"files": [
1518
"dist"
1619
],
1720
"scripts": {
18-
"build": "yarn node ./build.mjs && run build:types",
21+
"build": "yarn node ./build.js && run build:types",
1922
"build:pack": "yarn pack -o redwoodjs-project-config.tgz",
2023
"build:types": "tsc --build --verbose",
2124
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",

packages/project-config/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import toml from '@iarna/toml'
44
import merge from 'deepmerge'
55
import { env as envInterpolation } from 'string-env-interpolation'
66

7-
import { getConfigPath } from './configPath'
7+
import { getConfigPath } from './configPath.js'
88

99
export enum TargetEnum {
1010
NODE = 'node',

packages/project-config/src/configPath.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { findUp } from './findUp'
1+
import { findUp } from './findUp.js'
22

33
const CONFIG_FILE_NAME = 'redwood.toml'
44

packages/project-config/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './config'
2-
export * from './configPath'
3-
export * from './paths'
4-
export * from './findUp'
1+
export * from './config.js'
2+
export * from './configPath.js'
3+
export * from './paths.js'
4+
export * from './findUp.js'

packages/project-config/src/paths.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from 'path'
33

44
import fg from 'fast-glob'
55

6-
import { getConfig } from './config'
7-
import { getConfigPath } from './configPath'
6+
import { getConfig } from './config.js'
7+
import { getConfigPath } from './configPath.js'
88

99
export interface NodeTargetPaths {
1010
base: string

packages/project-config/tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"extends": "../../tsconfig.compilerOption.json",
33
"compilerOptions": {
4+
"moduleResolution": "NodeNext",
5+
"module": "NodeNext",
46
"baseUrl": ".",
57
"rootDir": "src",
68
"outDir": "dist",

packages/vite/bins/rw-vite-dev.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import { createServer } from 'vite'
33
import yargsParser from 'yargs-parser'
44

5-
import projectConfig from '@redwoodjs/project-config'
5+
import { getPaths } from '@redwoodjs/project-config'
66

7-
const rwPaths = projectConfig.getPaths()
7+
const rwPaths = getPaths()
88

99
const startDevServer = async () => {
1010
const configFile = rwPaths.web.viteConfig

packages/vite/src/__tests__/viteNestedPages.test.mts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { transformWithEsbuild } from 'vite'
66
import { test, describe, beforeEach, afterAll, beforeAll, it, expect, vi } from 'vitest'
77

88
import * as babel from '@babel/core'
9-
import projectConfig from '@redwoodjs/project-config'
9+
import { getPaths } from '@redwoodjs/project-config'
1010

1111
import {
1212
Flags,
@@ -41,7 +41,6 @@ async function transform(srcPath: string) {
4141
const __filename = fileURLToPath(import.meta.url)
4242
const __dirname = path.dirname(__filename)
4343
const FIXTURE_PATH = path.join(__dirname, 'fixtures/nestedPages')
44-
const { getPaths } = projectConfig
4544

4645
test('transform', async () => {
4746
vi.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {

0 commit comments

Comments
 (0)