diff options
author | Robert Kaussow <mail@geeklabor.de> | 2022-01-06 15:58:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-06 15:58:10 +0300 |
commit | 5c5e2d59cbae04f6342f684ae1c34da0bdaf577f (patch) | |
tree | 7c2a2910aabd03a7f5983a20f77d02d35d8e6c5a /webpack.plugins.js | |
parent | 2ac2a9faababa34508be349619741e4b3eae19cf (diff) |
refactor: replace gulp by webpack and npm scripts (#258)
BREAKING CHANGE: We have replaced `gulp` with `webpack` and `npm scripts` to build this theme. If you build it on your own or use build commands during the deployment, you may have to adjust your setup.
BREAKING CHANGE: The `GeekblogIcons` font is using the icon name as Unicode now. As a consequence, you have to replace all references to Icons from this font if you have customized the theme.
BREAKING CHANGE: We have refactored the search integration to split Hugo templates from JavaScript code. To get it working again, you need to adjust the `outputFormats` and `outputs` in your Hugo configuration file, as [documented](https://geekdocs.de/usage/configuration/#site-configuration).
Diffstat (limited to 'webpack.plugins.js')
-rw-r--r-- | webpack.plugins.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/webpack.plugins.js b/webpack.plugins.js new file mode 100644 index 0000000..b7d48b9 --- /dev/null +++ b/webpack.plugins.js @@ -0,0 +1,57 @@ +const fs = require("fs") +const crypto = require("crypto") +const path = require("path") +const { validate } = require("schema-utils") + +class SRIPlugin { + static defaultOptions = { + algorithm: "sha512", + sourceFile: "assets.json" + } + + constructor(options = {}) { + const schema = { + type: "object", + properties: { + outputFile: { + type: "string" + }, + algorithm: { + type: "string" + } + } + } + + this.options = { ...SRIPlugin.defaultOptions, ...options } + + validate(schema, options, { + name: "SRI Plugin", + baseDataPath: "options" + }) + } + + apply(compiler) { + compiler.hooks.done.tap("SRIPlugin", (manifest) => { + let data = JSON.parse(fs.readFileSync(this.options.sourceFile, "utf8")) + let outputFile = this.options.outputFile ? this.options.outputFile : this.options.sourceFile + + const checksum = (str, algorithm = this.options.algorithm, encoding = "base64") => + crypto.createHash(algorithm).update(str, "utf8").digest(encoding) + const fileSum = (file, algorithm) => checksum(fs.readFileSync(file), algorithm) + const calculateSRI = (file, algorithm = this.options.algorithm) => + `${algorithm}-${fileSum(path.join(".", "static", file), algorithm)}` + + Object.keys(data).forEach((key) => { + let element = data[key] + element.integrity = calculateSRI(element.src) + }) + + fs.writeFileSync(outputFile, JSON.stringify(data, null, 2), { + encoding: "utf8", + flag: "w" + }) + }) + } +} + +module.exports = SRIPlugin |