diff --git a/README.md b/README.md index a7c3102..e82a901 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ import refresh from 'rollup-plugin-react-refresh'; module.exports = { plugins: [ process.env.NODE_ENV === 'development' && refresh() + // If your "react-refresh" is not installed directly next to "rollup-plugin-react-refresh" You might need to pass the option "reactRefreshRuntimeFilePathRelativeToPlugin". + // See https://github.com/PepsRyuu/rollup-plugin-react-refresh/pull/10 + // process.env.NODE_ENV === 'development' && refresh({ + // reactRefreshRuntimeFilePathRelativeToPlugin: 'path/to/some/other/node_modules/react-refresh/cjs/react-refresh-runtime.development.js' + // }) ] } ``` diff --git a/index.js b/index.js index c65d2ec..f5427be 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,31 @@ let fs = require('fs'); -let runtime = fs.readFileSync(require.resolve('react-refresh/cjs/react-refresh-runtime.development.js'), 'utf8'); - -runtime = runtime.replace('process.env.NODE_ENV', JSON.stringify(process.env.NODE_ENV)); +const DEFAULT_REACT_REFRESH_PATH = '../react-refresh/cjs/react-refresh-runtime.development.js'; function ReactRefresh (opts = {}) { + opts.reactRefreshRuntimeFilePathRelativeToPlugin = ( + opts.reactRefreshRuntimeFilePathRelativeToPlugin || + DEFAULT_REACT_REFRESH_PATH + ); + + let runtime; + try { + runtime = fs.readFileSync(require.resolve(opts.reactRefreshRuntimeFilePathRelativeToPlugin), 'utf8'); + } + catch(e) { + throw new Error( + `[rollup-plugin-react-refresh] ERROR +Node.js forces "rollup-plugin-react-refresh" to require "react-refresh" via a relative import. +It seems like the ${opts.reactRefreshRuntimeFilePathRelativeToPlugin == DEFAULT_REACT_REFRESH_PATH ? 'default ': ''}path "${opts.reactRefreshRuntimeFilePathRelativeToPlugin}" did not resolve the file.". +The resolution has to be from the "rollup-plugin-react-refresh" plugin folder at: "${__dirname}". +Please fix the plugin option "reactRefreshRuntimeFilePathRelativeToPlugin". +See https://github.com/PepsRyuu/rollup-plugin-react-refresh/pull/10 for more info.`, + {cause: e} + ); + } + + runtime = runtime.replace('process.env.NODE_ENV', JSON.stringify(process.env.NODE_ENV)); + return { nollupBundleInit () { return ` diff --git a/package.json b/package.json index 35310fa..f83792c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-react-refresh", - "version": "0.0.3", + "version": "0.0.4-custom", "main": "index.js", "author": "Paul Sweeney", "license": "MIT",