Skip to content

Conversation

@Tobbe
Copy link
Member

@Tobbe Tobbe commented May 6, 2025

Steps for ESM support

  1. Add "type": "module" to all three package.json files
  2. Add @cedarjs/testing as a devDependency in your root package.json
  3. Rename web/config/postcss.config.js and web/config/tailwind.config.js to end with .cjs
  4. Update the tailwind path in postcss.config.cjs to end with .cjs
  5. Update the tailwind path in prettier.config.cjs to end with .cjs
  6. Update all relative imports to specify a .js extension. Take care to add index.js if the relative import is an index import.
  7. Remove all three jest.config.js
  8. Install vitest: yarn add --exact vitest (right now I'm focusing on web tests, so I run that command inside web/)
  9. Explicitly import describe etc everywhere you need it, or add vitest config to make it globally available
  10. Update your vite config to include vitest settings. See example below.
  11. Use yarn why -R vitest and yarn why -R vite to make sure there's only one version of vite and one version of vitest being used. Add "resolutions" as needed to your root package.json.
  12. Read https://vitest.dev/guide/migration.html#jest

vite.config.ts

/// <reference types="vitest/config" />

import dns from 'dns'

import { defineConfig } from 'vite'

import redwood from '@cedarjs/vite'

// So that Vite will load on localhost instead of `127.0.0.1`.
// See: https://vitejs.dev/config/server-options.html#server-host.
dns.setDefaultResultOrder('verbatim')

export default defineConfig({
  plugins: [redwood()],
  test: {
    environment: 'jsdom',
    // globals for now... See question below
    globals: true,
  },
})

Questions

ESM imports

With ESM you need to specify file extension for relative imports. So import { fooFunc } from './some/path/foo' needs to be import { fooFunc } from './some/path/foo.js'
In RW/Cedar we have directory named imports, so you can do import SomeComponent from 'src/components/SomeComponent' and it’ll be expanded to import SomeComponent from './src/components/SomeComponent/SomeComponent'
That now needs to be expanded to import SomeComponent from './src/components/SomeComponent/SomeComponent.js'
What would you prefer the syntax you use as a Cedar user is? Should I just keep it as it is, and still let you do import SomeComponent from 'src/components/SomeComponent' or should I require you add .js to make it import SomeComponent from 'src/components/SomeComponent.js' to be consistent with other imports? Or should I support both with and without .js? Or should we just get rid of the magic and require you do full path imports?

Vitest

Should we ship with describe, expect etc globally available, or should we explicitly import it where it's needed? I'm leaning towards explicit imports, but let me know what you think

Issues

TODO

  • Verify that it's possible to override all CedarJS's vite config (like ssr: noExternal: [] for example
  • Set up another test project, so we have both a CJS and an ESM project

Major areas of work

  • yarn rw exec - used to rely on Babel to support RW features
  • yarn rw prerender - used to rely on Babel to support RW features
  • yarn rw test - used to rely on Jest for running tests, and Babel for RW features
  • yarn rw dataMigrate - used to rely on Babel to support RW features

Breaking changes

@Tobbe Tobbe added this to the next-release milestone May 6, 2025
@Tobbe Tobbe added release:feature This PR introduces a new feature changesets-ok Override the changesets check labels May 6, 2025
@nx-cloud
Copy link

nx-cloud bot commented May 6, 2025

View your CI Pipeline Execution ↗ for commit 9681d08.

Command Status Duration Result
nx run-many -t test --minWorkers=1 --maxWorkers=4 ❌ Failed 1m 34s View ↗
nx run-many -t build:pack --exclude create-redm... ✅ Succeeded 8s View ↗
nx run-many -t build ✅ Succeeded 8s View ↗
nx run-many -t test:types ✅ Succeeded 7s View ↗

☁️ Nx Cloud last updated this comment at 2025-05-06 13:25:40 UTC

@Tobbe Tobbe force-pushed the tobbe-crxa-esm branch from 9681d08 to 6208a06 Compare May 19, 2025 17:10
@Tobbe Tobbe changed the title feat(crxa): Prep for ESM apps feat(ccra): Prep for ESM apps May 19, 2025
@Tobbe Tobbe added release:breaking This PR is a breaking change and removed release:feature This PR introduces a new feature labels May 21, 2025
@netlify
Copy link

netlify bot commented Jun 27, 2025

Deploy Preview for cedarjs canceled.

Name Link
🔨 Latest commit 53be851
🔍 Latest deploy log https://app.netlify.com/projects/cedarjs/deploys/689ad391f28be90008f6eaa8

@Tobbe Tobbe changed the title feat(ccra): Prep for ESM apps feat(ccja): Prep for ESM apps Jul 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changesets-ok Override the changesets check release:breaking This PR is a breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants