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
feat: replace CRA with Vite [LIBS-598] #847
base: alpha
Are you sure you want to change the base?
Conversation
BREAKING CHANGE: Supported Node versions are 18.x or 20+
reporter.info('Compiling service worker...') | ||
await compileServiceWorker({ config, paths, mode }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, CRA compiled the service worker in production builds. We already have our own compile function that we use for dev builds, so we use that here now
const relative = normalizeExtension(path.relative(inputDir, source)) | ||
const relative = path.relative(inputDir, source) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was what broke JSX files in platform apps/libs: as an example, consider index.jsx
has import App from 'App.jsx'
. At compile time, App.jsx
gets converted to App.js
, but the import text in index
is still from App.jsx
(it doesn't get modified despite the filename change), and then an error is thrown that says "Can't find App.jsx
"
@@ -82,12 +81,11 @@ exports.verifyEntrypoints = ({ | |||
|
|||
const getEntrypointWrapper = async ({ entrypoint, paths }) => { | |||
const relativeEntrypoint = entrypoint.replace(/^(\.\/)?src\//, '') | |||
const outRelativeEntrypoint = normalizeExtension(relativeEntrypoint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was also mishandling .jsx
entrypoints
.replace(/'.\/D2App\/app'/g, `'./D2App/${outRelativeEntrypoint}'`) | ||
.replace(/'.\/D2App\/app\.jsx'/g, `'./D2App/${relativeEntrypoint}'`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The placeholder filename in the shell is now called app.jsx
// Skip index.html, (plugin.html,) and `static` directory; | ||
// CRA's workbox-webpack-plugin handles it smartly | ||
globIgnores: [ | ||
'static/**/*', | ||
paths.launchPath, | ||
paths.pluginLaunchPath, | ||
// skip moment locales -- they result in many network requests and | ||
// slow down service worker installation | ||
'**/moment-locales/*', | ||
'**/*.map', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few changes in this file now that it's taking over from CRA
// (see https://regex101.com/r/z4Hy9k/1/ for RegEx details) | ||
dontCacheBustURLsMatching: /\.[a-z0-9]{8}\.|\d+\.\d+\.\d+/, | ||
// (see https://regex101.com/r/z4Hy9k/3/ for RegEx details) | ||
dontCacheBustURLsMatching: /[.-][A-Za-z0-9-_]{8}\.|\d+\.\d+\.\d+/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated for the chunks emitted from Vite's Rollup
// Precache all of the assets generated by your build process. | ||
// Their URLs are injected into the manifest variable below. | ||
// This variable must be present somewhere in your service worker file, | ||
// even if you decide not to use precaching. See https://cra.link/PWA. | ||
// Includes all built assets and index.html | ||
// Injection point for the precache manifest from workbox-build, | ||
// a manifest of app assets to fetch and cache upon SW installation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few things are cleaned up in this file now that our own compilation script is handling service workers instead of CRA's
// Note that the CRA precache manifest files start with './' | ||
// TODO: Make this extensible with a d2.config.js option | ||
export const CRA_MANIFEST_EXCLUDE_PATTERNS = [ | ||
/^\.\/static\/js\/moment-locales\//, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was used to modify the manifest output by CRA; it's no longer needed
<!-- The entrypoint for Vite --> | ||
<script type="module" src="./src/index.jsx"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vite wants index.html
to be here at the root directory level. This script tag is the entrypoint for the rest of the app
@@ -17,7 +17,7 @@ | |||
] | |||
}, | |||
"devDependencies": { | |||
"@dhis2/cli-style": "^10.4.3", | |||
"@dhis2/cli-style": "^10.5.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -29,7 +29,7 @@ | |||
"@babel/preset-react": "^7.0.0", | |||
"@babel/preset-typescript": "^7.6.0", | |||
"@dhis2/app-shell": "11.2.0", | |||
"@dhis2/cli-helpers-engine": "^3.2.0", | |||
"@dhis2/cli-helpers-engine": "^3.2.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part 1 of the changes -- LIBS-598
Includes:
react-scripts
is removed.[jt]sx
extensions in app files.jsx
extensions@dhis2/cli-helpers-engine
to let Vite's dev server use the console, which enables helpful interactive outputFollowing changes will look like:
env
var management, for exampleIf I remember right, these are the relevant breaking changes so far:
^18 || >=20
.[jt]sx
extension is not really supported, unfortunately. This is the largest change. I added some workarounds invite.config.mjs
to allow compiling of JSX syntax in files without.[jt]sx
extensions, so apps can at least start once they install this new version, but consumers will need to change their file extensions to get the full HMR behavior. This design by Vite is intended to save computations by not needing to run a JSX parser on files without JSX, so it's limited to.[jt]sx
files. There are more notes inside the config file. I'm investigating easy ways like a codemod to run through a repo and update filenames of files containing JSXreact-scripts
revealed a bug in@dhis2/cli-style
-- it was missing a dependency thatreact-scripts
filled. Consumers will need to upgrade@dhis2/cli-style
to^10.5.2
Here are some other general notes:
.tsx
and it worked smoothly (albeit with many type errors).mjs
extension to avoid a warning about thecjs
package of Vite being deprecated