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): Build rwjs/web to cjs with new build system #10826

Merged
merged 24 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4e003b1
Convert tsc bin to mts
dac09 Jun 7, 2024
794466b
Add build.mts
dac09 Jun 7, 2024
169d622
Update build script for rwjs/web
dac09 Jun 7, 2024
669c595
Update buildDefaults in framework tools to include .mts
dac09 Jun 7, 2024
bf128bb
Restore tsc bin to how it was to check CJS build first
dac09 Jun 7, 2024
db802ad
Add react imports to everything that has JSX
dac09 Jun 7, 2024
40b4b7b
Remove React from eslint auto import rule
dac09 Jun 7, 2024
209cbe6
Fix linting errors caused by eslint rule removal
dac09 Jun 7, 2024
88877ca
Update build.mts with comments
dac09 Jun 7, 2024
d853319
Merge branch 'main' of github.com:redwoodjs/redwood into chore/web-pa…
dac09 Jun 17, 2024
5c2e4f9
Update comments
dac09 Jun 17, 2024
58e08c7
Bump tsx version
dac09 Jun 17, 2024
92d4d31
Handle webpack entrry
dac09 Jun 17, 2024
8561805
Add react to webpack entry
dac09 Jun 17, 2024
1cbc42d
Merge branch 'main' into chore/web-package-cjsonly-new-build
dac09 Jun 17, 2024
c6eb4a7
export default for bundle removal
dac09 Jun 18, 2024
517fed0
Merge branch 'main' into chore/web-package-cjsonly-new-build
dac09 Jun 18, 2024
ea1be4c
Just do export default
dac09 Jun 18, 2024
2db4ec9
Merge branch 'main' into chore/web-package-cjsonly-new-build
dac09 Jun 19, 2024
5e43bde
Update yarn.lock
dac09 Jun 19, 2024
f8c34e2
Bump tsx version to match other packages
dac09 Jun 19, 2024
2a80b84
Merge branch 'main' into chore/web-package-cjsonly-new-build
dac09 Jun 20, 2024
fd6bb0e
Merge branch 'main' into chore/web-package-cjsonly-new-build
dac09 Jun 21, 2024
aad789c
Merge branch 'main' into chore/web-package-cjsonly-new-build
Josh-Walker-GM Jun 22, 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
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ module.exports = {
browser: true,
},
globals: {
React: 'readonly', // We auto-import React via Babel.
window: 'off', // Developers should use `global` instead of window. Since window is undefined in NodeJS.
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const handler = async ({ force }) => {
path.join(
getPaths().base,
// NOTE we're copying over the index.js before babel transform
'node_modules/@redwoodjs/web/src/entry/index.js',
'node_modules/@redwoodjs/web/src/entry/index.jsx',
),
)
.toString()
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/FormError.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import type { GraphQLError } from 'graphql'

export interface ServerParseError extends Error {
Expand Down
2 changes: 1 addition & 1 deletion packages/framework-tools/src/buildDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const defaultBuildOptions: ESBuildOptions = {
metafile: true,
}

export const defaultPatterns = ['./src/**/*.{ts,js,tsx,jsx}']
export const defaultPatterns = ['./src/**/*.{ts,js,tsx,jsx,mts}']
export const defaultIgnorePatterns = [
'**/__tests__',
'**/*.test.{ts,js}',
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/rsc-link.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef } from 'react'
import React, { forwardRef } from 'react'

export interface LinkProps {
to: string
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/plugins/vite-plugin-remove-from-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ export default function removeFromBundle(
}
}

// Currently configured for CJS only.
const EMPTY_MODULE = {
code: `module.exports = {}`,
code: `export default {};`,
}

export function excludeOnMatch(modulesToExclude: ModulesToExclude, id: string) {
Expand Down
48 changes: 48 additions & 0 deletions packages/web/build.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// import { writeFileSync } from 'node:fs'

import {
build,
defaultBuildOptions,
defaultIgnorePatterns,
} from '@redwoodjs/framework-tools'

// CJS build
await build({
entryPointOptions: {
ignore: [...defaultIgnorePatterns],
},
buildOptions: {
...defaultBuildOptions,
// ⭐ No special tsconfig here
// outdir: 'dist/cjs', DONT DO THIS JUST YET
outdir: 'dist',
packages: 'external',
},
})

/** THIS IS IN PART 2 ~ making this a dual module
Will enable in follow up PR

ESM build
await build({
entryPointOptions: {
ignore: [...defaultIgnorePatterns, 'src/entry/**'],
},
buildOptions: {
...defaultBuildOptions,
// ⭐ No special tsconfig here
// tsconfig: 'tsconfig.build.json',
format: 'esm',
packages: 'external',
},
})

// Place a package.json file with `type: commonjs` in the dist 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/esm folder so that
// all .js files are treated as ES Module files.
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' }))

*/
7 changes: 4 additions & 3 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
"src/entry"
],
"scripts": {
"build": "yarn build:js && yarn build:types",
"build:js": "babel src -d dist --extensions \".js,.jsx,.ts,.tsx\"",
"build": "tsx ./build.mts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-web.tgz",
"build:types": "tsc --build --verbose",
"build:types": "tsc --build --verbose tsconfig.json",
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "vitest run",
Expand All @@ -55,6 +54,7 @@
"@babel/core": "^7.22.20",
"@babel/plugin-transform-runtime": "7.24.3",
"@babel/runtime": "7.24.5",
"@redwoodjs/framework-tools": "workspace:*",
"@rollup/plugin-babel": "6.0.4",
"@testing-library/jest-dom": "6.4.6",
"@testing-library/react": "14.3.1",
Expand All @@ -64,6 +64,7 @@
"react": "19.0.0-beta-04b058868c-20240508",
"react-dom": "19.0.0-beta-04b058868c-20240508",
"tstyche": "2.0.0",
"tsx": "4.15.2",
"typescript": "5.4.5",
"vitest": "1.6.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/apollo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import type {
ApolloClientOptions,
setLogVerbosity,
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/web/src/apollo/suspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Eventually we will have one ApolloProvider, not multiple.
*/

import { useContext } from 'react'
import React, { useContext } from 'react'

import type {
ApolloCache,
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/DevFatalErrorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (typeof window !== 'undefined') {
window.Buffer = window.Buffer || require('buffer').Buffer
}

import { useState } from 'react'
import React, { useState } from 'react'

import type { GraphQLError } from 'graphql'
import StackTracey from 'stacktracey'
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/FatalErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

type PropsInfallibleErrorBoundary = Partial<{
children: React.ReactNode
}>
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/GraphQLHooksProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import type {
OperationVariables,
useBackgroundQuery as apolloUseBackgroundQuery,
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/MetaTags.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import { Helmet as HelmetHead } from 'react-helmet-async'

// Ideally we wouldn't include this for non experiment builds
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/RedwoodProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import { Helmet, HelmetProvider } from 'react-helmet-async'

interface RedwoodProviderProps {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/cell/createCell.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import { fragmentRegistry } from '../../apollo/fragmentRegistry.js'
import { getOperationName } from '../../graphql.js'
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/cell/createServerCell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="react/experimental" />

import { Suspense } from 'react'
import React, { Suspense } from 'react'

// Class components are not supported on the server
// https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#when-to-use-server-and-client-components
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/cell/createSuspendingCell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Suspense } from 'react'
import React, { Suspense } from 'react'

import type { OperationVariables, QueryReference } from '@apollo/client'
import { useApolloClient } from '@apollo/client'
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/components/htmlTags.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
import { Fragment } from 'react'

declare const window: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

import { hydrateRoot, createRoot } from 'react-dom/client'

import App from '~redwood-app-root'
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8911,6 +8911,7 @@ __metadata:
"@babel/runtime": "npm:7.24.5"
"@babel/runtime-corejs3": "npm:7.24.5"
"@redwoodjs/auth": "workspace:*"
"@redwoodjs/framework-tools": "workspace:*"
"@rollup/plugin-babel": "npm:6.0.4"
"@testing-library/jest-dom": "npm:6.4.6"
"@testing-library/react": "npm:14.3.1"
Expand All @@ -8928,6 +8929,7 @@ __metadata:
stacktracey: "npm:2.1.8"
ts-toolbelt: "npm:9.6.0"
tstyche: "npm:2.0.0"
tsx: "npm:4.15.2"
typescript: "npm:5.4.5"
vitest: "npm:1.6.0"
peerDependencies:
Expand Down
Loading