Coder Social home page Coder Social logo

Comments (1)

arrowing avatar arrowing commented on June 19, 2024

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}`
    })
  ]

from autodll-webpack-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.