Code Monkey home page Code Monkey logo

Comments (15)

alexander-akait avatar alexander-akait commented on August 21, 2024 1

There is a bug in cache for html-webpack-plugin, that is why we disable it, I am working on the new major release without this bug, cache will work without extra setup in future and without bugs

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024 1

Yeah( I am still working on it, anyway HTML generation is fast, so just check it out

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

You should not run:

setInterval(() => {
      compiler.compile()
    }, 60000)

it is bad for perf

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

Sorry I don't undestand what do you try to do

from html-webpack-plugin.

robotsmeagol avatar robotsmeagol commented on August 21, 2024

i'm trying to get data from server and inject into html, like this

const res = await axios.get('xxx.com/xxxxx')
data.html += res.data

on dev mode, i want to use timer to check whether data from server changed and regenerate html if changed

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

Please use HtmlWebpackPlugin.getHooks(compilation).beforeEmit right now (getCompilationHooks is not released), I don't recommend to run compiler.compile using times, you can send a HEAD request and check it is 304 code or not, and only when somethings changed re-run compilation

from html-webpack-plugin.

robotsmeagol avatar robotsmeagol commented on August 21, 2024

how to re-run compilation, do u mean npm run dev again?

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

@robotsmeagol to rerun compilation I recommend to make compiler.watching.invalidate(), note compiler.watching is not undefined only when you watch

from html-webpack-plugin.

robotsmeagol avatar robotsmeagol commented on August 21, 2024

i tried compiler.watching.invalidate(), but only first time this would works, after that html won't regenerate

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

What do you mean won't regenerate?

from html-webpack-plugin.

robotsmeagol avatar robotsmeagol commented on August 21, 2024

U can check on this demo
in config/Myplugin.js, i inject customData to html
customData is always 100 and never changed

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

Solution:

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

class MyPlugin {
  constructor() {
    this.pathToData = path.resolve(__dirname, "./my-data.json");
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap("MyPlugin", (compilation) => {
      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
        "MyPlugin",
        (data, cb) => {
            let content;

            try {
                content = compilation.inputFileSystem.readFileSync(this.pathToData, "utf8");
            } catch (error) {
                cb(error);

                return;
            }

            content = JSON.parse(content);

            compilation.fileDependencies.add(this.pathToData);

            data.html = data.html.replace(
                "</head>",
                `<script>window.customData=${JSON.stringify(content.value)}</script></head>`,
            );

            cb(null, data);
        },
      );
    });
  }
}

module.exports = MyPlugin;

File:

my-data.json

{
  "value": "11244123"
}

Add:

{
  watchFiles: [require("path").resolve(__dirname, "my-data.json")],
  // Other options
}

to config/webpackDevServer.config.js

And disable cache for HtmlWebpackPlugin

new HtmlWebpackPlugin(
        Object.assign(
          {},
          {
            cache: false,
            inject: true,
            template: paths.appHtml,
          },
          isEnvProduction
            ? {
                minify: {
                  removeComments: true,
                  collapseWhitespace: true,
                  removeRedundantAttributes: true,
                  useShortDoctype: true,
                  removeEmptyAttributes: true,
                  removeStyleLinkTypeAttributes: true,
                  keepClosingSlash: true,
                  minifyJS: true,
                  minifyCSS: true,
                  minifyURLs: true,
                },
              }
            : undefined,
        ),
      ),

i.e. cache: false, so webpack will automatically reload page and apply new data

from html-webpack-plugin.

HcySunYang avatar HcySunYang commented on August 21, 2024

Hi @alexander-akait , has the issue about the cache been fixed right now? And I have an addition question: if we change the cache to false, doesn't it mean that the HTML file will be regenerated so long as you make changes to your source code no matter what files you are touching, is that right?

from html-webpack-plugin.

HcySunYang avatar HcySunYang commented on August 21, 2024

The HTML can be generated fast, but it causes the dev server to reload the whole page, HMR won't work in that case, right? So in a large project, the impact is significant

from html-webpack-plugin.

alexander-akait avatar alexander-akait commented on August 21, 2024

The HTML can be generated fast, but it causes the dev server to reload the whole page, HMR won't work in that case, right? So in a large project, the impact is significant

HTML should be generated only when you make changes in HTML files, but I don't you did it often

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