npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@classflow/react-dev-utils

v0.5.2

Published

Webpack utilities used by Create React App

Downloads

5

Readme

react-dev-utils

This package includes some utilities used by Create React App. Please refer to its documentation:

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 = require('path');
var HtmlWebpackPlugin = require('html-dev-plugin');
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');

// Webpack config
var publicUrl = '/my-custom-url';

module.exports = {
  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">
    new InterpolateHtmlPlugin({
      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.
    new HtmlWebpackPlugin({
      inject: true,
      template: path.resolve('public/index.html'),
    }),
    // ...
  ],
  // ...
}

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 = require('path');
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');

// Webpack config
module.exports = {
  // ...
  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
    new WatchMissingNodeModulesPlugin(path.resolve('node_modules'))
  ],
  // ...
}

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 = require('path');
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');

if (!checkRequiredFiles([
  path.resolve('public/index.html'),
  path.resolve('src/index.js')
])) {
  process.exit(1);
}

clearConsole(): void

Clears the console, hopefully in a cross-platform way.

var clearConsole = require('react-dev-utils/clearConsole');

clearConsole();
console.log('Just cleared the screen!');

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,
} = require('react-dev-utils/FileSizeReporter');

measureFileSizesBeforeBuild(buildFolder).then(previousFileSizes => {
  return cleanAndRebuild().then(webpackStats => {
    printFileSizesAfterBuild(webpackStats, previousFileSizes);
  });
});

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 = require('webpack');
var config = require('../config/webpack.config.dev');
var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');

var compiler = webpack(config);

compiler.plugin('invalid', function() {
  console.log('Compiling...');
});

compiler.plugin('done', function(stats) {
  var rawMessages = stats.toJson({}, true);
  var messages = formatWebpackMessages(rawMessages);
  if (!messages.errors.length && !messages.warnings.length) {
    console.log('Compiled successfully!');
  }
  if (messages.errors.length) {
    console.log('Failed to compile.');
    messages.errors.forEach(e => console.log(e));
    return;
  }
  if (messages.warnings.length) {
    console.log('Compiled with warnings.');
    messages.warnings.forEach(w => console.log(w));
  }
});

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 = require('react-dev-utils/getProcessForPort');

getProcessForPort(3000);

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 = require('path');
var openBrowser = require('react-dev-utils/openBrowser');

if (openBrowser('http://localhost:3000')) {
  console.log('The browser tab has been opened!');
}

prompt(message: string, isYesDefault: boolean): Promise<boolean>

This function displays a console prompt to the user.

By convention, "no" should be the conservative choice. If you mistype the answer, we'll always take it as a "no". You can control the behavior on <Enter> with isYesDefault.

var prompt = require('react-dev-utils/prompt');

prompt(
  'Are you sure you want to eat all the candy?',
  /* isYesDefault */ false
).then(shouldEat => {
  if (shouldEat) {
    console.log('You have successfully consumed all the candy.');
  } else {
    console.log('Phew, candy is still available!');
  }
});

webpackHotDevClient.js

This is an alternative client for WebpackDevServer that shows a syntax error overlay.

It currently supports only Webpack 1.x.

// Webpack development config
module.exports = {
  // ...
  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'
  ],
  // ...
}