question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem updating react-dev-utils to support webpack4

See original GitHub issue

Is this a bug report?

Yes, or I think so

Did you try recovering your dependencies?

Yes, I tried with a blank project

Which terms did you search for in User Guide?

N/A

Description

Hi, I ejected my project from create-react-app a time ago and now I’m modifying the code to support webpack 4 and I saw you already updated create-react-app and react-dev-utils to support it, so I updated react-dev-utils dependency to the latest version (5.0.1 at this time) but I just get the old webpack3 code in InterpolateHtmlPlugin.js instead of the one that is in this repo

...
"react-dev-utils": "5.0.1",
...
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

// This Webpack plugin lets us interpolate custom variables into `index.html`.
// Usage: `new InterpolateHtmlPlugin({ 'MY_VARIABLE': 42 })`
// Then, you can use %MY_VARIABLE% in your `index.html`.

// It works in tandem with HtmlWebpackPlugin.
// Learn more about creating plugins like this:
// https://github.com/ampedandwired/html-webpack-plugin#events

'use strict';
const escapeStringRegexp = require('escape-string-regexp');

class InterpolateHtmlPlugin {
  constructor(replacements) {
    this.replacements = replacements;
  }

  apply(compiler) {
    compiler.plugin('compilation', compilation => {
      compilation.plugin(
        'html-webpack-plugin-before-html-processing',
        (data, callback) => {
          // Run HTML through a series of user-specified string replacements.
          Object.keys(this.replacements).forEach(key => {
            const value = this.replacements[key];
            data.html = data.html.replace(
              new RegExp('%' + escapeStringRegexp(key) + '%', 'g'),
              value
            );
          });
          callback(null, data);
        }
      );
    });
  }
}

module.exports = InterpolateHtmlPlugin;

When I am expecting this code

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

// This Webpack plugin lets us interpolate custom variables into `index.html`.
// Usage: `new InterpolateHtmlPlugin({ 'MY_VARIABLE': 42 })`
// Then, you can use %MY_VARIABLE% in your `index.html`.

// It works in tandem with HtmlWebpackPlugin.
// Learn more about creating plugins like this:
// https://github.com/ampedandwired/html-webpack-plugin#events

'use strict';
const escapeStringRegexp = require('escape-string-regexp');

class InterpolateHtmlPlugin {
  constructor(replacements) {
    this.replacements = replacements;
  }

  apply(compiler) {
    compiler.hooks.compilation.tap('InterpolateHtmlPlugin', compilation => {
      compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
        'InterpolateHtmlPlugin',
        data => {
          // Run HTML through a series of user-specified string replacements.
          Object.keys(this.replacements).forEach(key => {
            const value = this.replacements[key];
            data.html = data.html.replace(
              new RegExp('%' + escapeStringRegexp(key) + '%', 'g'),
              value
            );
          });
        }
      );
    });
  }
}

module.exports = InterpolateHtmlPlugin;

Maybe the npm package was not correctly updated? Or maybe I am doing something wrong?

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
cmoadcommented, Aug 1, 2018

I believe I was able to get past this issue.

yarn add -D interpolate-html-plugin

Replace var InterpolateHtmlPlugin = require('react-dev-utils/interpolate-html-plugin'); with var InterpolateHtmlPlugin = require('interpolate-html-plugin');.

Make sure the InterpolateHtmlPlugin appears after HtmlWebpackPlugin in the plugin list.

Now on to the next issue…

3reactions
miguelcrespocommented, Jun 14, 2018

Thank you for your answer @iansu, can we release a new version of react-dev-utils for this?

I think this change worth a release because people like me who are looking to support webpack 4 have problems with this dependency at the moment

Read more comments on GitHub >

github_iconTop Results From Across the Web

When I update react-dev-utils to the @next version. I can't find ...
config.js file which is working with webpack4 . How can I fix this or do I need these two utils in my typescript...
Read more >
How to Upgrade to React 17 and Webpack 5 Step by Step
1. Delete package-lock.json (not package. · 2. Delete node_modules in your project folder. · 3. Remove "babel-eslint" (or whatever the error ...
Read more >
react-dev-utils - npm
webpack utilities used by Create React App. Latest version: 12.0.1, ... Start using react-dev-utils in your project by running `npm i ...
Read more >
react-dev-utils | Yarn - Package Manager
Creates a webpack compiler instance for WebpackDevServer with built-in helpful messages. The args object accepts a number of properties: appName string : The ......
Read more >
To v5 from v4 - webpack
Upgrade webpack 4 and its plugins/loaders · Make sure your build has no errors or warnings · Make sure to use mode ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found