Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I use this plugin for react-app,it wiil not inject to the html #133

Open
zhangjiana opened this issue Jul 4, 2019 · 1 comment
Open

Comments

@zhangjiana
Copy link

I found this problem.
react-app use the [email protected]。 Its 'htmlWebpackPluginBeforeHtmlGeneration'
has deprecated.
so when I run my app, I found it will not inject to my html.

if (!compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration) { return; }
I require html-webpack-plugin manually, and modify the code below
`
compiler.hooks.compilation.tap('AutoDllPlugin', function (compilation) {
if (!HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration) {
return;
}

      HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration.tapAsync('AutoDllPlugin', doCompilation);
    });

`
It works!

But I don't know the right way to solve this problem.
please help me, thank you !

@arrowing
Copy link

arrowing commented Aug 12, 2019

I write a custom plugin to sovle it.

/* A plugin to compitible html-webpack-plugin v4 otherwise failed of injection */

let fs = require("fs");
let path = require("path");
let HtmlWebpackPlugin = require("html-webpack-plugin");

function getAutoDLLFiles() {
  // cacheDir : projectPath\node_modules\.cache\autodll-webpack-plugin
  var cacheDir = require("autodll-webpack-plugin/node_modules/find-cache-dir")({
    name: "autodll-webpack-plugin"
  });

  if (cacheDir) {
    let files = fs.readdirSync(cacheDir);
    let scripts, styles;
    let childDir = files.filter(
      item => item.indexOf("development_instance") > -1 // eg. development_instance_0_d58c4bb0bc931d01a844ded15994e547
    )[0];

    cacheDir = path.join(cacheDir, childDir);

    if (cacheDir) {
      // get files name
      files = fs.readdirSync(cacheDir);
      scripts = files.filter(item => item.endsWith(".js"));
      styles = files.filter(item => item.endsWith(".css"));
    }

    return { scripts, styles };
  }

  return { scripts: [], styles: [] };
}

class AutoDLLInjectPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    let _this = this;

    compiler.hooks.compilation.tap("AutoDllPlugin", function(compilation) {
      if (!HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration) {
        return;
      }

      HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration.tapAsync(
        "AutoDllPlugin",
        function(htmlPluginData, callback) {
          // htmlPluginData.assets.js
          // ["/../dist/vendors/vendor.320d23.js", "/activity\\index.0aa46b.js"];

          let { scripts, styles } = getAutoDLLFiles();

          if (_this.options.fileMap) {
            scripts = scripts.map(_this.options.fileMap);
            styles = styles.map(_this.options.fileMap);
          }

          htmlPluginData.assets.js = [].concat(
            scripts,
            htmlPluginData.assets.js
          );

          htmlPluginData.assets.css = [].concat(
            styles,
            htmlPluginData.assets.css
          );

          // console.log(htmlPluginData.assets.js);
          callback(null, htmlPluginData);
        }
      );
    });
  }
}

module.exports = AutoDLLInjectPlugin;

Usage:
Put the following code after html-webpack-plugin, like:

  let AutoDLLInjectPlugin = require("../plugins/AutoDLLInjectPlugin.js");
  config.plugins.push(
    new AutoDLLInjectPlugin({
      fileMap: filename => `/vendors/${filename}`
    })
  );

or

plugins: [
    new HtmlWebpackPlugin({
      filename: path.relative(
        path.join(projectPath, "src/pages"),
        html
      ),
      template: hbs,
      inject: true,
      chunks: [entry]
    }),
    new AutoDLLInjectPlugin({
      fileMap: filename => `/vendors/${filename}`
    })
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants