-
Notifications
You must be signed in to change notification settings - Fork 3
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
Step 1 — Switch from React to Preact #2
base: 00-react
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,19 +3,23 @@ | |
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"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 commentThe reason will be displayed to describe this comment to others. Learn more. And replace it with |
||
"react-scripts": "1.1.4" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test --env=jsdom", | ||
"start": "node scripts/start", | ||
"build": "node scripts/build", | ||
"test": "node scripts/test --env=jsdom", | ||
"eject": "react-scripts eject", | ||
"preanalyze": "npm run build", | ||
"analyze": "source-map-explorer --html build/static/js/main.* > packages.html" | ||
}, | ||
"devDependencies": { | ||
"module-alias": "^2.0.6", | ||
"preact-compat-enzyme": "^0.2.5", | ||
"preact-render-to-json": "^3.6.6", | ||
"preact-test-utils": "^0.1.3", | ||
"source-map-explorer": "^1.5.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
process.env.BABEL_ENV = 'production'; | ||
process.env.NODE_ENV = 'production'; | ||
|
||
const webpackPreact = require('./patch/webpackPreact'); | ||
const webpackConfig = require('react-scripts/config/webpack.config.prod'); | ||
webpackPreact(webpackConfig); | ||
|
||
require('react-scripts/scripts/build'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. whenever |
||
'react-dom': 'preact-compat' | ||
})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
process.env.BABEL_ENV = 'development'; | ||
process.env.NODE_ENV = 'development'; | ||
|
||
const webpackPreact = require('./patch/webpackPreact'); | ||
const webpackConfig = require('react-scripts/config/webpack.config.dev'); | ||
webpackPreact(webpackConfig); | ||
|
||
require('react-scripts/scripts/start'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
|
||
process.env.BABEL_ENV = 'test'; | ||
process.env.NODE_ENV = 'test'; | ||
|
||
// Load the create-react-app config. | ||
const jestConfig = require('react-scripts/scripts/utils/createJestConfig'); | ||
|
||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. whenever |
||
'^react-dom/server$': '<rootDir>/node_modules/preact-render-to-string/dist/index.js', | ||
'^react-dom/test-utils$': '<rootDir>/node_modules/preact-test-utils/lib/index.js', | ||
'^react-dom$': '<rootDir>/node_modules/preact-compat-enzyme/lib/index.js', | ||
'^react-test-renderer/shallow$': '<rootDir>/node_modules/preact-test-utils/lib/index.js', | ||
'^react-test-renderer$': '<rootDir>/node_modules/preact-test-utils/lib/index.js', | ||
'^react-addons-test-utils$': '<rootDir>/node_modules/preact-test-utils/lib/index.js', | ||
'^react$': '<rootDir>/node_modules/preact-compat-enzyme/lib/index.js' | ||
}); | ||
return config; | ||
}; | ||
|
||
// HACK ALERT! | ||
module.constructor._cache[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HACK ALERT!Preload Node's module cache with the hacked |
||
require.resolve('react-scripts/scripts/utils/createJestConfig') | ||
].exports = newJestConfig; | ||
|
||
// Run the build. | ||
require('react-scripts/scripts/test'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,13 @@ import React, { Component } from 'react'; | |
import logo from './logo.svg'; | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Just a |
||
render() { | ||
return <div className="thing" />; | ||
} | ||
} | ||
|
||
class App extends Component { | ||
render() { | ||
return ( | ||
|
@@ -13,6 +20,7 @@ class App extends Component { | |
<p className="App-intro"> | ||
To get started, edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<Thing /> | ||
</div> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`can render into snapshots 1`] = ` | ||
<div | ||
class="App" | ||
> | ||
<header | ||
class="App-header" | ||
> | ||
<img | ||
alt="logo" | ||
class="App-logo" | ||
src="logo.svg" | ||
/> | ||
<h1 | ||
class="App-title" | ||
> | ||
Welcome to React | ||
</h1> | ||
</header> | ||
<p | ||
class="App-intro" | ||
> | ||
To get started, edit | ||
<code> | ||
src/App.js | ||
</code> | ||
and save to reload. | ||
</p> | ||
<div | ||
class="thing" | ||
/> | ||
</div> | ||
`; | ||
|
||
exports[`can shallow render into snapshots 1`] = ` | ||
<div | ||
class="App" | ||
> | ||
<header | ||
class="App-header" | ||
> | ||
<img | ||
alt="logo" | ||
class="App-logo" | ||
src="logo.svg" | ||
/> | ||
<h1 | ||
class="App-title" | ||
> | ||
Welcome to React | ||
</h1> | ||
</header> | ||
<p | ||
class="App-intro" | ||
> | ||
To get started, edit | ||
<code> | ||
src/App.js | ||
</code> | ||
and save to reload. | ||
</p> | ||
<Thing /> | ||
</div> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import render, { shallowRender } from 'preact-render-to-json'; | ||
|
||
/** | ||
* 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
if (vnode) { | ||
if (vnode.children) { | ||
vnode.children.forEach(n => removeDevAttributes(n)); | ||
} | ||
if (vnode.props) { | ||
if (vnode.props.__source) { | ||
delete vnode.props.__source; | ||
} | ||
if (vnode.props.__self) { | ||
delete vnode.props.__self; | ||
} | ||
} | ||
} | ||
return vnode; | ||
} | ||
|
||
/** | ||
* Render Preact JSX + Components to an HTML string. | ||
* | ||
* @param {VNode} vnode JSX VNode to render. | ||
* @param {Object} [context={}] Optionally pass an initial context object through the render path. | ||
* @param {Object} [options={}] Rendering options | ||
*/ | ||
global.render = (vnode, context) => removeDevAttributes(render(vnode, context)); | ||
|
||
global.shallowRender = (vnode, context) => removeDevAttributes(shallowRender(vnode, context)); |
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
andreact-dom