Skip to content

Commit

Permalink
Add polyfill for React.createContext
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelalmeidatk committed Oct 11, 2019
1 parent cf07abb commit cd9bdca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/using-inferno/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ This example uses [Inferno](https://github.com/infernojs/inferno), an insanely f
Here's how we did it:

- Use `next.config.js` to customize our webpack config to support [inferno-compat](https://www.npmjs.com/package/inferno-compat)
- Create `lib/inferno-compat.js` to polyfill the `React.createContext` API (required by Next.js) that is not available by `inferno-compat`
11 changes: 11 additions & 0 deletions examples/using-inferno/lib/inferno-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const React = require('inferno-compat')
const createContext = require('create-react-context/lib/implementation')

Object.keys(React).forEach(key => {
if (key === 'default' || key === '__esModule') return
exports[key] = React[key]
})

// bypass export of React.createContext
exports.createContext = createContext
exports.default = React.default
1 change: 1 addition & 0 deletions examples/using-inferno/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"create-react-context": "0.3.0",
"inferno": "7.3.2",
"inferno-compat": "7.3.2",
"inferno-server": "7.3.2",
Expand Down
5 changes: 3 additions & 2 deletions examples/using-inferno/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const moduleAlias = require('module-alias')
const path = require('path')

// For the development version, we'll use React.
// Because, it support react hot loading and so on.
if (!dev) {
moduleAlias.addAlias('react', 'inferno-compat')
moduleAlias.addAlias('react', path.resolve('./lib/inferno-compat.js'))
moduleAlias.addAlias('react-dom/server', 'inferno-server')
moduleAlias.addAlias('react-dom', 'inferno-compat')
moduleAlias.addAlias('react-dom', path.resolve('./lib/inferno-compat.js'))
}

const { createServer } = require('http')
Expand Down

0 comments on commit cd9bdca

Please sign in to comment.