react-dev-utils
This package includes some utilities used by Create React App.
Please refer to its documentation:
- Getting Started – How to create a new app.
- User Guide – How to develop apps bootstrapped with Create React App.
Usage in Create React App Projects
These utilities come by default with Create React App, which includes it by default. You don’t need to install it separately in Create React App projects.
Usage Outside of Create React App
If you don’t use Create React App, or if you ejected, you may keep using these utilities. Their development will be aligned with Create React App, so major versions of these utilities may come out relatively often. Feel free to fork or copy and paste them into your projects if you’d like to have more control over them, or feel free to use the old versions. Not all of them are React-specific, but we might make some of them more React-specific in the future.
Entry Points
There is no single entry point. You can only import individual top-level modules.
new InterpolateHtmlPlugin(replacements: {[key:string]: string})
This Webpack plugin lets us interpolate custom variables into index.html
.
It works in tandem with HtmlWebpackPlugin 2.x via its events.
var path = ;var HtmlWebpackPlugin = ;var InterpolateHtmlPlugin = ; // Webpack configvar publicUrl = '/my-custom-url'; moduleexports = output: // ... publicPath: publicUrl + '/' // ... plugins: // Makes the public URL available as %PUBLIC_URL% in index.html, e.g.: // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> PUBLIC_URL: publicUrl // You can pass any key-value pairs, this was just an example. // WHATEVER: 42 will replace %WHATEVER% with 42 in index.html. // Generates an `index.html` file with the <script> injected. inject: true template: path // ... // ...
new ModuleScopePlugin(appSrc: string)
This Webpack plugin ensures that relative imports from app's source directory don't reach outside of it.
var path = ;var ModuleScopePlugin = ; moduleexports = // ... resolve: // ... plugins: pathsappSrc // ... // ... // ...
new WatchMissingNodeModulesPlugin(nodeModulesPath: string)
This Webpack plugin ensures npm install <library>
forces a project rebuild.
We’re not sure why this isn't Webpack's default behavior.
See #186 for details.
var path = ;var WatchMissingNodeModulesPlugin = ; // Webpack configmoduleexports = // ... plugins: // ... // If you require a missing module and then `npm install` it, you still have // to restart the development server for Webpack to discover it. This plugin // makes the discovery automatic so you don't have to restart. // See https://github.com/facebookincubator/create-react-app/issues/186 path // ...
checkRequiredFiles(files: Array<string>): boolean
Makes sure that all passed files exist.
Filenames are expected to be absolute.
If a file is not found, prints a warning message and returns false
.
var path = ;var checkRequiredFiles = ; if ! process;
clearConsole(): void
Clears the console, hopefully in a cross-platform way.
var clearConsole = ; ;console;
eslintFormatter(results: Object): string
This is our custom ESLint formatter that integrates well with Create React App console output.
You can use the default one instead if you prefer so.
const eslintFormatter = ; // In your webpack config:// ...module: rules: test: /\.$/ include: pathsappSrc enforce: 'pre' use: loader: 'eslint-loader' options: // Pass the formatter: formatter: eslintFormatter
FileSizeReporter
measureFileSizesBeforeBuild(buildFolder: string): Promise<OpaqueFileSizes>
Captures JS and CSS asset sizes inside the passed buildFolder
. Save the result value to compare it after the build.
printFileSizesAfterBuild(webpackStats: WebpackStats, previousFileSizes: OpaqueFileSizes)
Prints the JS and CSS asset sizes after the build, and includes a size comparison with previousFileSizes
that were captured earlier using measureFileSizesBeforeBuild()
.
var measureFileSizesBeforeBuild printFileSizesAfterBuild = ; ;
formatWebpackMessages({errors: Array<string>, warnings: Array<string>}): {errors: Array<string>, warnings: Array<string>}
Extracts and prettifies warning and error messages from webpack stats object.
var webpack = ;var config = ;var formatWebpackMessages = ; var compiler = ; compiler; compiler;
getProcessForPort(port: number): string
Finds the currently running process on port
.
Returns a string containing the name and directory, e.g.,
create-react-app
in /Users/developer/create-react-app
var getProcessForPort = ; ;
launchEditor(fileName: string, lineNumber: number): void
On macOS, tries to find a known running editor process and opens the file in it. It can also be explicitly configured by REACT_EDITOR
, VISUAL
, or EDITOR
environment variables. For example, you can put REACT_EDITOR=atom
in your .env.local
file, and Create React App will respect that.
noopServiceWorkerMiddleware(): ExpressMiddleware
Returns Express middleware that serves a /service-worker.js
that resets any previously set service worker configuration. Useful for development.
openBrowser(url: string): boolean
Attempts to open the browser with a given URL.
On Mac OS X, attempts to reuse an existing Chrome tab via AppleScript.
Otherwise, falls back to opn behavior.
var path = ;var openBrowser = ; if console;
printHostingInstructions(appPackage: Object, publicUrl: string, publicPath: string, buildFolder: string, useYarn: boolean): void
Prints hosting instructions after the project is built.
Pass your parsed package.json
object as appPackage
, your the URL where you plan to host the app as publicUrl
, output.publicPath
from your Webpack configuration as publicPath
, the buildFolder
name, and whether to useYarn
in instructions.
const appPackage = ;const publicUrl = pathspublicUrl;const publicPath = configoutputpublicPath;;
WebpackDevServerUtils
choosePort(host: string, defaultPort: number): Promise<number | null>
Returns a Promise resolving to either defaultPort
or next available port if the user confirms it is okay to do. If the port is taken and the user has refused to use another port, or if the terminal is not interactive and can’t present user with the choice, resolves to null
.
createCompiler(webpack: Function, config: Object, appName: string, urls: Object, useYarn: boolean): WebpackCompiler
Creates a Webpack compiler instance for WebpackDevServer with built-in helpful messages. Takes the require('webpack')
entry point as the first argument. To provide the urls
argument, use prepareUrls()
described below.
prepareProxy(proxySetting: string, appPublicFolder: string): Object
Creates a WebpackDevServer proxy
configuration object from the proxy
setting in package.json
.
prepareUrls(protocol: string, host: string, port: number): Object
Returns an object with local and remote URLs for the development server. Pass this object to createCompiler()
described above.
webpackHotDevClient
This is an alternative client for WebpackDevServer that shows a syntax error overlay.
It currently supports only Webpack 1.x.
// Webpack development configmoduleexports = // ... entry: // You can replace the line below with these two lines if you prefer the // stock client: // require.resolve('webpack-dev-server/client') + '?/', // require.resolve('webpack/hot/dev-server'), 'react-dev-utils/webpackHotDevClient' 'src/index' // ...