-
Notifications
You must be signed in to change notification settings - Fork 3
Step 1 — Switch from React to Preact #2
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
base: 00-react
Are you sure you want to change the base?
Conversation
| "private": true, | ||
| "dependencies": { | ||
| "react": "^16.3.2", | ||
| "react-dom": "^16.3.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.
Getting rid of react and react-dom
| "react": "^16.3.2", | ||
| "react-dom": "^16.3.2", | ||
| "preact": "^8.2.7", | ||
| "preact-compat": "^3.18.0", |
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.
And replace it with preact and preact-compat
| // Hack the webpack config to load Preact instead of React | ||
| module.exports = webpackConfig => | ||
| (webpackConfig.resolve.alias = Object.assign({}, webpackConfig.resolve.alias, { | ||
| react: 'preact-compat', |
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.
whenever webpack is loading react, it will grab preact-compat instead
| // Map React's testing classes to Preact | ||
| const newJestConfig = function() { | ||
| const config = jestConfig.apply(this, arguments); | ||
| config.moduleNameMapper = Object.assign({}, config.moduleNameMapper, { |
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.
whenever jest tries to load or mock any react* modules, it will get the preact* equivalents.
| }; | ||
|
|
||
| // HACK ALERT! | ||
| module.constructor._cache[ |
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.
HACK ALERT!
Preload Node's module cache with the hacked jest config we just made.
| import './App.css'; | ||
|
|
||
| // This is just a Thing | ||
| class Thing extends Component { |
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.
Just a Thing to show off shallow/deep rendering during testing.
| * Remove __self and __source from the list of attributes/properties. | ||
| * babel-preset-react adds these for use with the React DevTools | ||
| */ | ||
| function removeDevAttributes(vnode) { |
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.
babel-preset-react adds __self and __source as props to every Component.
We need this small patch to make them go away and have reliable snapshot testing.
See preactjs/preact-render-to-string#43 (comment) for more info.
It's time to get hacking!
The main idea here is that we alias
react*modules with theirpreact*equivalents and pull inpreact-compatto make sure the API stays the same. This is done both for Node andwebpack.For testing, it's a bit trickier, we pull in
preact-render-to-jsonand put it on theglobalscope to be available in all tests.Due to
babel-preset-react, we need to remove__selfand__sourcefrom the output JSON to be able to reliably use snapshot testing injest.File sizes:
Source Map Analyzer output:
