Skip to content

Instantly share code, notes, and snippets.

@ridakk
Created August 16, 2019 12:19
Show Gist options
  • Save ridakk/a46ba5ef37d85d78b1e69b212342deef to your computer and use it in GitHub Desktop.
Save ridakk/a46ba5ef37d85d78b1e69b212342deef to your computer and use it in GitHub Desktop.
async function calculating hash of static folders and adding to DefinePlugin with webpack.merge
const fs = require('fs');
const webpack = require('webpack');
const merge = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const config = require('config');
const { hashElement } = require('folder-hash');
const base = require('./webpack.base');
const packageJson = require('./package.json');
const definePluginConfig = {
VERSION: JSON.stringify(packageJson.version),
DEBUG: config.debug,
MOCK_CONTENT: false,
};
if (config.mock) {
definePluginConfig.MOCK_CONTENT = JSON.stringify(fs.readFileSync('./mock.txt').toString().split('\n'));
}
module.exports = async () => {
const hash = await hashElement('build', {
folders: {
exclude: ['info', 'fonts'],
},
files: {
exclude: ['*bundle.*.js', '*.html'],
},
});
const hashes = {};
for (let i = 0, len = hash.children.length; i < len; i++) {
const folder = hash.children[i];
hashes[`${folder.name.toUpperCase()}_HASH`] = JSON.stringify(folder.hash);
}
base.plugins.shift();
return merge(base, {
mode: 'production',
output: {
filename: 'bundle.[contenthash].min.js',
},
devtool: false,
performance: {
maxEntrypointSize: 900000,
maxAssetSize: 900000,
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
comments: false,
},
},
}),
],
},
plugins: [
new webpack.DefinePlugin(Object.assign({}, definePluginConfig, hashes)),
],
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment