Skip to content

Instantly share code, notes, and snippets.

@djalmaaraujo
Last active January 27, 2018 01:48
Show Gist options
  • Save djalmaaraujo/0d9b2243518b68f239fc916111b2bcac to your computer and use it in GitHub Desktop.
Save djalmaaraujo/0d9b2243518b68f239fc916111b2bcac to your computer and use it in GitHub Desktop.
fix-hot-reload.js
const replaceString = require('replace-string')
const fs = require('fs')
// This is a fix to avoid ejecting the app form create-react-app
// Explanation here:
// https://github.com/facebook/create-react-app/pull/1588#issuecomment-355781108
const webpackConfigDevFile = './node_modules/react-scripts/config/webpack.config.dev.js'
const replaceWebpackConfigDevFile = () => {
return new Promise((res, rej) => {
fs.readFile(webpackConfigDevFile, 'utf8', (err, data) => {
if (err) {
return rej(err)
}
let result = data
result = replaceString(result, `const publicUrl = '';`, `const publicUrl = 'http://localhost:3000';`)
result = replaceString(result, `const publicPath = '/';`, `const publicPath = 'http://localhost:3000/';`)
result = replaceString(result, `require.resolve('react-dev-utils/webpackHotDevClient')`, `require.resolve('react-dev-utils-custom-hmr/webpackHotDevClient')`)
fs.writeFile(webpackConfigDevFile, result, 'utf8', (err) => {
if (err) rej(err)
res(true)
})
})
})
}
// Usage
replaceWebpackConfigDevFile().then((res) => console.log('Replaced hot reload config... Starting the server...')).catch(err => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment