I have a single HTML file and some SASS and JS, and for Production I want to bundle these together into a single HTML file which contains the SASS compiled into CSS into an inline <style>
element, and the JS combined and minified into an inline <script>
element. Not, where the css is embedded in the JS and added to the DOM via JS or in separate files.
I've got as far as getting the SASS compiled into .css
and <link
ed to and the JS into a <script src="
ed .js
. I've also got PurgeCss working (I think - it's stripping a lot of probably unused selectors).
How do I inline the CSS and JS in their own tags inside the HTML?
Please don't just say to use https://www.npmjs.com/package/html-inline-css-webpack-plugin or reference Inline JavaScript and CSS with webpack, because this seems to be plagued with errors and version/dependency mismatches. If this is the best answer, how do you actually make it work? What versions of all the dependencies actually work together?
Here is my webpack.config
which works as far as I've got:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const PurgecssPlugin = require('purgecss-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const glob = require('glob');
const purgecssFromHtml = require('purgecss-from-html');
const PATHS = {
src: path.join(__dirname, 'src')
}
module.exports = {
module: {
rules: [
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
//'style-loader',
{
loader: 'css-loader',
options: {
//importLoaders: 1,
url: false
},
},
'sass-loader',
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ["babel-loader"]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src", "index.html")
}),
new MiniCssExtractPlugin({
filename: "[name].css"
}),
new PurgecssPlugin({
paths: glob.sync(`${PATHS.src}/*.*`),
styleExtensions: ['.css'],
whitelist: ['whitelisted'],
extractors: [
{
extractor: purgecssFromHtml,
extensions: ['html']
}
]
})
]
};
src/index.js
:
import "./scss/theme.scss";
import "./file1.js"
import "./file2.js"