Create-react-app: Problem updating react-dev-utils to support webpack4

Created on 14 Jun 2018  路  9Comments  路  Source: facebook/create-react-app

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!

question

Most helpful comment

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...

All 9 comments

react-dev-utils has not been released as latest with this change. Please install it from the next tag.

Hi @Timer , installing the dependency with the @next tag doesn't work either

This is the version I get from installing the next tag

  "version": "6.0.0-next.66cc7a90",

And this is the code, same problem, can you reopen this issue? Thank you!

/**
 * 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;

We haven't released a new version of next since webpack 4 support was added.

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

We will be publishing the new version soon but unfortunately I can't tell you when exactly that will happen.

I am also waiting for an update on this, have the same problem as @MiguelCrespo
@Timer , please reopen this bug, as it is still very valid or point to a solution, thank you.

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...

Thanks, @cmoad, that resolved my issue!

you are my personal hero! thank you!

Was this page helpful?
0 / 5 - 0 ratings