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.

html-plugin integration

See original GitHub issue

hey @faceyspacey I am using 2.0.11 All happening at https://github.com/cescoferraro/react-boil and deployed https://boil.cescoferraro.xyz/

GOAL

Insert all css in a html file using the html-webpack-plugin

REASON

In a pwa context, you need to provide a static html file so a service worker can cache it then next time a user hit the website’s url, it serves the static html instead of going through the network. There is no problem of adding ALL the css because all the files are supposed to be cached by the sw by the time the user visits the website again. The below snippet creates a html page that loads the js chunks in the right order!

new HtmlWebpackPlugin({
filename: "html/index.html",
template:  "./server/index.html",
showErrors: true,
chunks:['bootstrap','vendor', 'main'],
chunksSortMode: (a, b) => { 
    var order = ['bootstrap','vendor', 'main']; 
    return order.indexOf(a.names[0]) - order.indexOf(b.names[0]); 
}
})

But It I hit boil.cescoferraro.xyz/ with the app properly cached by chrome and than use the redux-first router link to jump to '/user/345345I wont see the syles being applied. Css tags wont be inject by the .js files. while Universal component tags that get asynchronously added get inject just fine. Also if I hit the url straight fromuser/234324`, ssr kicks in a deliver the right set of css files.

TRIED

code

<% for (var chunk in htmlWebpackPlugin.files.js) { %>
 <%= htmlWebpackPlugin.files.js[chunk] %>
 lsdkmfsdmdfkldsf
<% } %>

<% for (var chunk in htmlWebpackPlugin.files.css) { %>
 <%= htmlWebpackPlugin.files.css[chunk] %>
 lsdkmfsdmdfkldsf
<% } %>

<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
<%= htmlWebpackPlugin.files.chunks[chunk].entry %>
<% } %>

output

 https://boil.cescoferraro.xyz/js/bootstrap_2a26b113ed4fbfd76873.js
 lsdkmfsdmdfkldsf

 https://boil.cescoferraro.xyz/js/vendor.js
 lsdkmfsdmdfkldsf

 https://boil.cescoferraro.xyz/js/main_2a26b113ed4fbfd76873.js
 lsdkmfsdmdfkldsf





https://boil.cescoferraro.xyz/js/bootstrap_2a26b113ed4fbfd76873.js

https://boil.cescoferraro.xyz/js/vendor.js

https://boil.cescoferraro.xyz/js/main_2a26b113ed4fbfd76873.js

It seems that the html-plugin does not consider the output of this plugin as being a css file

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
mike1808commented, Sep 22, 2017

@faceyspacey so, I figured out how to include css chunks into HTML file.

I wrote small plugin for html plugin based on cssChunckHash function from webpack-flush-chunks:

/* eslint-disable */

function CssChunkHash(options) {
}

CssChunkHash.prototype.createCssHash = function ({ assetsByChunkName, publicPath }) {
    return Object.keys(assetsByChunkName).reduce((hash, name) => {
        if (!assetsByChunkName[name] || !assetsByChunkName[name].find) return hash;
        const file = assetsByChunkName[name].find(file => file.endsWith('.css'));
        if (file) hash[name] = `${publicPath}${file}`;
        return hash;
    }, {});
};


CssChunkHash.prototype.apply = function (compiler) {
    const self = this;
    compiler.plugin('compilation', function (compilation) {
        compilation.plugin('html-webpack-plugin-before-html-generation', function (htmlPluginData, callback) {
            const webpack = compilation.getStats().toJson();
            const cssHash = self.createCssHash(webpack);
            htmlPluginData.assets.cssHash = cssHash;
            callback(null, htmlPluginData);
        });
    });
};

module.exports = CssChunkHash;

Then, just include it in webpack config and use the following code in your html template (ejs in my case):

<script type="text/javascript">
    window.__CSS_CHUNKS__ = JSON.parse('<%= JSON.stringify(htmlWebpackPlugin.files.cssHash) %>')
</script>
1reaction
faceyspaceycommented, Aug 26, 2017

This isn’t my forte, as I don’t use HtmlWebpackPlugin, but basically these tools are intended for pretty manual setups that wouldn’t use that. So the recommended way is to take full control of the html wrapper code and generate it yourself. I’m sorry to say.

However, @cescoferraro 's solution perhaps could be formalized more and become the recommended solution??

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Plug-Ins - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >
jQuery Plugin Integration Steps in a website or Html template
Steps for jQuery plugin integration: · 1) Download jQuery file (optional) · 2) Download jQuery plugin (Nivo Slider) file · 3) Simplify jQuery...
Read more >
HtmlWebpackPlugin | webpack
The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash...
Read more >
HTML5: embed – integration point for plugins (NEW)
The embed element represents an integration point for external content — typically, non-HTML content such as an application or some other type of ......
Read more >
How to Create a Custom WordPress Plugin for ... - YouTube
You can build a 100% custom WordPress plugin that works with any theme ... WordPress Plugin for Your Website (PHP, HTML, CSS, &...
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