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(esm/cjs): Make the rwjs/vite package dual #10934

Merged
merged 45 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f5da2bf
Build cjs and esm outputs
Jul 9, 2024
124f3e9
Get project to build, take special note of how I got babel imports to…
Jul 9, 2024
4787b92
Undo building bundled to esm for now, until I understand it better
Jul 9, 2024
32a776e
Build bundled rsdw to ESM with plugin to prevent dynamic import error
Jul 10, 2024
50d14ec
Remove CJS wrapper
Jul 10, 2024
d3de305
Change __dirname
Jul 10, 2024
fafd84d
Add mjs and mts to resolveFile
Jul 10, 2024
ae462bd
Add CJS types, not quite working yet
Jul 10, 2024
e5b2cb9
TODO For now skip building CJS types, because of special syntax used …
Jul 10, 2024
b5a06d9
Commento
Jul 10, 2024
84dd77e
Fix SSR by dynamically importing rscWorker imports
Jul 11, 2024
e958601
Dynamically import RSC stuff in runFeServer too
Jul 11, 2024
3d18e62
Make dbAuth dual package too
Jul 11, 2024
ab5b97a
Skip middleware packages in cjsInterop
Jul 11, 2024
f4330d5
Get tests to pass
Jul 11, 2024
2b76b98
Get tests to stop complaining
Jul 11, 2024
cf4b154
Add types field
Jul 11, 2024
286350d
Try using fileURLToPath
Jul 11, 2024
889d94a
Update mocking of rwjs/auth-dbauth-api
Jul 11, 2024
7b0328f
Remove cjsWrapper from pkgJson > files
Jul 12, 2024
7f7d64f
Merge branch 'main' into chore/vitepkg-dual
dac09 Jul 12, 2024
34e79ff
Merge branch 'chore/vitepkg-dual' of github.com:dac09/redwood into ch…
Jul 12, 2024
58febbd
Explain commonjs plugin in vite/build.mts
Jul 12, 2024
50a0db3
Merge branch 'main' of github.com:redwoodjs/redwood into chore/vitepk…
Jul 15, 2024
7317f19
Generate .cts index file, remove resolution-modes as not needed anymore
Jul 15, 2024
d41a479
Remove packged package
Jul 15, 2024
cfe0130
Fix CJS types
Jul 15, 2024
977aeea
Align vitest interopDefault match Vite
Jul 15, 2024
33b0ec5
Merge branch 'main' into chore/vitepkg-dual
dac09 Jul 15, 2024
af63377
Update comments
Jul 15, 2024
c190f8f
Merge branch 'chore/vitepkg-dual' of github.com:dac09/redwood into ch…
Jul 15, 2024
eb2adc0
Undo making dbauth-middleware dual
Jul 15, 2024
760756d
Formatting
Jul 15, 2024
784b2e0
Lock package versions
Jul 15, 2024
34b28f5
Leave the ^ babel packages alone for now, handle it separately
Jul 15, 2024
7d2e380
Remove un-needed declaration
dac09 Jul 15, 2024
58d5bf7
Update __dirname variable names
dac09 Jul 16, 2024
d864fbf
Merge branch 'main' of github.com:redwoodjs/redwood into chore/vitepk…
dac09 Jul 16, 2024
e017f30
Add attw and publint to cSpell
dac09 Jul 16, 2024
3694bd4
Add attw and publint scripts
dac09 Jul 16, 2024
8f5a384
Undo accidental change to web/package.json
dac09 Jul 16, 2024
26eda46
Add concurrently, update attw script
dac09 Jul 16, 2024
cc2b8b2
Update attw script
dac09 Jul 16, 2024
a06e16e
Restore attw in packages/web
dac09 Jul 16, 2024
d3b9057
Vitest script
dac09 Jul 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"typescript.tsdk": "node_modules/typescript/lib",
"peacock.color": "#b85833",
"cSpell.words": [
"attw",
"autoplay",
"baremetal",
"bazinga",
Expand All @@ -38,6 +39,7 @@
"pino",
"Pistorius",
"prerendering",
"publint",
"redwoodjs",
"rsdw",
"RWJS",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-providers/dbAuth/middleware/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
dbAuthSession,
} from '@redwoodjs/auth-dbauth-api'
import type { GetCurrentUser } from '@redwoodjs/graphql-server'
import { MiddlewareResponse } from '@redwoodjs/web/dist/server/middleware'
import { MiddlewareResponse } from '@redwoodjs/web/middleware'
import type {
Middleware,
MiddlewareRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/cjs",
},
}
4 changes: 2 additions & 2 deletions packages/auth-providers/supabase/middleware/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
Middleware,
MiddlewareRequest,
MiddlewareResponse,
} from '@redwoodjs/web/middleware' with { 'resolution-mode': 'import' }
} from '@redwoodjs/web/middleware'

import { clearAuthState } from './util'
import { clearAuthState } from './util.js'

export interface SupabaseAuthMiddlewareOptions {
getCurrentUser: GetCurrentUser
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-providers/supabase/middleware/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { throwSupabaseSettingsError } from '@redwoodjs/auth-supabase-api'
import type {
MiddlewareRequest,
MiddlewareResponse,
} from '@redwoodjs/web/middleware' with { 'resolution-mode': 'import' }
} from '@redwoodjs/web/middleware'
/**
* Creates Supabase Server Client used to get the session cookie (only)
* from a given collection of auth cookies
Expand Down
2 changes: 1 addition & 1 deletion packages/project-config/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const getBaseDirFromFile = (file: string) => {
*/
export const resolveFile = (
filePath: string,
extensions: string[] = ['.js', '.tsx', '.ts', '.jsx'],
extensions: string[] = ['.js', '.tsx', '.ts', '.jsx', '.mjs', '.mts'],
Tobbe marked this conversation as resolved.
Show resolved Hide resolved
): string | null => {
for (const extension of extensions) {
const p = `${filePath}${extension}`
Expand Down
41 changes: 41 additions & 0 deletions packages/vite/attw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { $ } from 'zx'

interface Problem {
kind: string
entrypoint?: string
resolutionKind?: string
}

/***
* Excluded entry points:
* - ./bins/rw-vite-build.mjs: this is only used in the build handler
* - SsrRouter, Router: this should be moved out of the Vite package anyway, and is only used in ESM
* - ./react-node-loader: used to run the Worker
* -
*/

await $({
nothrow: true,
})`yarn attw -P --exclude-entrypoints ./bins/rw-vite-build.mjs ./SsrRouter ./Router ./react-node-loader -f json > .attw.json`
const output = await $`cat .attw.json`
await $`rm .attw.json`

const json = JSON.parse(output.stdout)

if (!json.analysis.problems || json.analysis.problems.length === 0) {
console.log('No errors found')
process.exit(0)
}

if (
json.analysis.problems.every(
(problem: Problem) => problem.resolutionKind === 'node10',
)
) {
console.log("Only found node10 problems, which we don't care about")
process.exit(0)
}

console.log('Errors found')
console.log(json.analysis.problems)
process.exit(1)
55 changes: 50 additions & 5 deletions packages/vite/build.mts
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
import { build, defaultIgnorePatterns } from '@redwoodjs/framework-tools'
import {
build,
defaultBuildOptions,
defaultIgnorePatterns,
} from '@redwoodjs/framework-tools'

import { writeFileSync } from 'node:fs'
import { commonjs } from '@hyrious/esbuild-plugin-commonjs'

import * as esbuild from 'esbuild'

// CJS Build
await build({
entryPointOptions: {
ignore: [...defaultIgnorePatterns, '**/bundled'],
},
buildOptions: {
...defaultBuildOptions,
outdir: 'dist/cjs',
packages: 'external',
},
})

// ESM Build
await build({
entryPointOptions: {
ignore: [...defaultIgnorePatterns, '**/bundled'],
}
},
buildOptions: {
...defaultBuildOptions,
format: 'esm',
packages: 'external',
},
})

// We bundle some react packages with the "react-server" condition
// so that we don't need to specify it at runtime.

await esbuild.build({
entryPoints: ['src/bundled/*'],
outdir: 'dist/bundled',

format: 'esm',
bundle: true,
conditions: ['react-server'],
platform: 'node',
target: ['node20'],

// ⭐ Without this plugin, we get "Error: Dynamic require of "util" is not supported"
// when trying to run the built files. This plugin will
// "just rewrite that file to replace "require(node-module)" to a toplevel static import statement." (see issue)
// https://github.com/evanw/esbuild/issues/2113
// https://github.com/evanw/esbuild/pull/2067
plugins: [commonjs()],
logLevel: 'info',
})

// Place a package.json file with `type: commonjs` in the dist/cjs folder so that
// all .js files are treated as CommonJS files.
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))

// Place a package.json file with `type: module` in the dist folder so that
// all .js files are treated as ES Module files.
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' }))

// Add CommonJS types by creating common JS types. Using a .d.ts file, because .d.cts files don't auto resolve
// Notice I haven't specified thee types in package.json for these - it's following the naming conversion TSC wants.
writeFileSync('dist/cjs/index.d.ts', 'export type * from "../index.d.ts"')
writeFileSync('dist/cjs/buildFeServer.d.ts', 'export type * from "../buildFeServer.d.ts"')
writeFileSync('dist/cjs/client.d.ts', 'export type * from "../client.d.ts"')
writeFileSync('dist/cjs/clientSsr.d.ts', 'export type * from "../clientSsr.d.ts"')
writeFileSync('dist/cjs/ClientRouter.d.ts', 'export type * from "../ClientRouter.d.ts"')
3 changes: 0 additions & 3 deletions packages/vite/cjsWrapper.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/vite/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ declare module 'react-server-dom-webpack/server' {
}

declare module 'vite-plugin-cjs-interop'

declare module '@babel/parser/index.cjs' {
export * from '@babel/parser'
}
45 changes: 28 additions & 17 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@redwoodjs/vite",
"version": "7.0.0",
"type": "module",
"description": "Vite configuration package for Redwood",
"repository": {
"type": "git",
Expand All @@ -11,33 +12,37 @@
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"default": "./cjsWrapper.js"
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
Tobbe marked this conversation as resolved.
Show resolved Hide resolved
},
"./client": {
"types": "./dist/client.d.ts",
"default": "./dist/client.js"
"require": "./dist/cjs/client.js",
"import": "./dist/client.js"
},
"./clientSsr": {
"types": "./dist/clientSsr.d.ts",
"default": "./dist/clientSsr.js"
"require": "./dist/cjs/clientSsr.js",
"import": "./dist/clientSsr.js"
},
"./Router": {
"types": "./dist/ClientRouter.d.ts",
"react-server": "./dist/ServerRouter.js",
"default": "./dist/ClientRouter.js"
"require": "./dist/cjs/ClientRouter.js",
"import": "./dist/ClientRouter.js"
},
"./SsrRouter": {
"types": "./dist/SsrRouter.d.ts",
"react-server": "./dist/ServerRouter.js",
"default": "./dist/SsrRouter.js"
"import": "./dist/SsrRouter.js"
},
"./buildFeServer": {
"types": "./dist/buildFeServer.d.ts",
"default": "./dist/buildFeServer.js"
"require": "./dist/cjs/buildFeServer.js",
"import": "./dist/buildFeServer.js"
},
"./react-node-loader": {
"types": "./dist/react-server-dom-webpack/node-loader.d.ts",
"default": "./dist/react-server-dom-webpack/node-loader.js"
},
"./bins/rw-vite-build.mjs": "./bins/rw-vite-build.mjs"
Expand All @@ -51,14 +56,16 @@
},
"files": [
"dist",
"inject",
"cjsWrapper.js"
"inject"
],
"scripts": {
"build": "tsx build.mts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-vite.tgz",
"build:types": "tsc --build --verbose",
"test": "vitest run",
"build:types": "tsc --build --verbose ./tsconfig.json",
"test:attw": "tsx ./attw.ts",
"test:publint": "yarn publint",
"test:vitest": "vitest run",
"test": "concurrently npm:test:vitest npm:test:attw npm:test:publint",
"test:watch": "vitest watch"
},
"dependencies": {
Expand Down Expand Up @@ -94,12 +101,16 @@
"yargs-parser": "21.1.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.15.3",
"@hyrious/esbuild-plugin-commonjs": "0.2.4",
"@types/busboy": "^1",
"@types/cookie": "^0",
"@types/express": "4",
"@types/react": "^18.2.55",
"@types/yargs-parser": "21.0.3",
"concurrently": "8.2.2",
"glob": "10.3.15",
"publint": "0.2.8",
"rollup": "4.18.0",
"tsx": "4.15.6",
"typescript": "5.4.5",
Expand Down
17 changes: 10 additions & 7 deletions packages/vite/src/ClientRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

import React, { useMemo } from 'react'

import { analyzeRoutes } from '@redwoodjs/router/dist/analyzeRoutes'
import { AuthenticatedRoute } from '@redwoodjs/router/dist/AuthenticatedRoute'
import { LocationProvider, useLocation } from '@redwoodjs/router/dist/location'
import { namedRoutes } from '@redwoodjs/router/dist/namedRoutes'
import type { RouterProps } from '@redwoodjs/router/dist/router'
import { RouterContextProvider } from '@redwoodjs/router/dist/router-context'
import { analyzeRoutes } from '@redwoodjs/router/dist/analyzeRoutes.js'
import { AuthenticatedRoute } from '@redwoodjs/router/dist/AuthenticatedRoute.js'
import {
LocationProvider,
useLocation,
} from '@redwoodjs/router/dist/location.js'
import { namedRoutes } from '@redwoodjs/router/dist/namedRoutes.js'
import { RouterContextProvider } from '@redwoodjs/router/dist/router-context.js'
import type { RouterProps } from '@redwoodjs/router/dist/router.js'

import { rscFetch } from './rsc/rscFetchForClientRouter'
import { rscFetch } from './rsc/rscFetchForClientRouter.js'

export const Router = ({ useAuth, paramTypes, children }: RouterProps) => {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/ServerRouter.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Router } from '@redwoodjs/router/dist/server-router'
export { Router } from '@redwoodjs/router/dist/server-router.js'
15 changes: 9 additions & 6 deletions packages/vite/src/SsrRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import React, { useMemo } from 'react'

import { analyzeRoutes } from '@redwoodjs/router/dist/analyzeRoutes'
import { LocationProvider, useLocation } from '@redwoodjs/router/dist/location'
import { namedRoutes } from '@redwoodjs/router/dist/namedRoutes'
import type { RouterProps } from '@redwoodjs/router/dist/router'

import { renderRoutesFromDist } from './clientSsr'
import { analyzeRoutes } from '@redwoodjs/router/dist/analyzeRoutes.js'
import {
LocationProvider,
useLocation,
} from '@redwoodjs/router/dist/location.js'
import { namedRoutes } from '@redwoodjs/router/dist/namedRoutes.js'
import type { RouterProps } from '@redwoodjs/router/dist/router.js'

import { renderRoutesFromDist } from './clientSsr.js'

export const Router = ({ paramTypes, children }: RouterProps) => {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/buildFeServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildWeb } from '@redwoodjs/internal/dist/build/web'
import { buildWeb } from '@redwoodjs/internal/dist/build/web.js'
import { getConfig, getPaths } from '@redwoodjs/project-config'

import { buildRouteHooks } from './buildRouteHooks.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/buildRouteHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getRouteHookBabelPlugins,
transformWithBabel,
} from '@redwoodjs/babel-config'
import { findRouteHooksSrc } from '@redwoodjs/internal/dist/files'
import { findRouteHooksSrc } from '@redwoodjs/internal/dist/files.js'
import type { Paths } from '@redwoodjs/project-config'
import { getPaths } from '@redwoodjs/project-config'

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/buildRouteManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import url from 'node:url'

import type { Manifest as ViteBuildManifest } from 'vite'

import { getProjectRoutes } from '@redwoodjs/internal/dist/routes'
import { getProjectRoutes } from '@redwoodjs/internal/dist/routes.js'
import { getAppRouteHook, getPaths } from '@redwoodjs/project-config'

import type { RWRouteManifest } from './types.js'
Expand Down
Loading
Loading