Created
August 16, 2019 12:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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