From 7b837b90f1a36c362b8f026a60f636256ce7696e Mon Sep 17 00:00:00 2001 From: HerrHase Date: Tue, 27 Aug 2024 08:42:48 +0200 Subject: [PATCH] adding #1 #3 --- index.js | 21 ++++++++++-- manifest.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- plugins/manifest.js | 43 ++++++++++++++++++++++++ rules/riot.js | 4 ++- 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 manifest.js create mode 100644 plugins/manifest.js diff --git a/index.js b/index.js index 81ce7d4..6a2bd7c 100644 --- a/index.js +++ b/index.js @@ -2,26 +2,39 @@ const webpack = require('webpack') const TerserPlugin = require('terser-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const { PurgeCSSPlugin } = require('purgecss-webpack-plugin') +const ManifestPlugin = require('./plugins/manifest') const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts') const SvgSpritemapPlugin = require('svg-spritemap-webpack-plugin') const path = require('path') const glob = require('glob') +/** + * function generate config for webpack + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://git.node001.net/tiny-components/webpack.git + * + */ module.exports = function tinyComponentsWebpack(files, options = {}) { // merge options with defaults const defaults = Object.assign({ + context: path.resolve(process.cwd(), ''), destination: path.resolve(process.cwd(), 'public'), + publicPath: '/', purge: { src: path.join(__dirname, 'js') } }, options) const config = { + context: defaults.context, entry: files, output: { - path: defaults.destination, - filename: 'js/[name].js', + path: defaults.destination, + filename: 'js/[name].js', + publicPath: defaults.publicPath }, resolve: { @@ -71,6 +84,10 @@ module.exports = function tinyComponentsWebpack(files, options = {}) { new PurgeCSSPlugin({ paths: glob.sync(`${defaults.purge.src}/**/*`, { nodir: true }) }), + new ManifestPlugin({ + filename: 'assets-manifest.json', + context: defaults.context + }) ], } diff --git a/manifest.js b/manifest.js new file mode 100644 index 0000000..12f3cb4 --- /dev/null +++ b/manifest.js @@ -0,0 +1,79 @@ +const fs = require('fs') +const crypto = require('crypto') + +/** + * Manifest + * + * creates manifest-file + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://git.node001.net/tiny-components/webpack.git + * + */ + +class Manifest { + + /** + * + * + * @param {string} filename + * @param {string} context + * + */ + constructor(filename, context) { + + // filename for manifest-file + this._filename = filename + + // context + this._context = context + + // results for write to file + this._results = { + + } + + } + + /** + * run through all assets from assetsByChunkName + * for each file create a hash + * + * @param {object} stats + * + */ + run(stats) { + + let files = Object.assign({}, stats.assetsByChunkName) + + Object.keys(files).forEach((key) => { + files[key].forEach((file) => { + this._results[stats.publicPath + file] = stats.publicPath + file + '?version=' + this._hash(file, stats.outputPath) + }) + }) + + fs.writeFileSync(this._context + '/' + this._filename, JSON.stringify(this._results, null, 4)) + } + + /** + * hash content of file + * + * @param {string} file + * @param {sring} stats + * @return {string} + * + */ + _hash(file, outputPath) { + + const data = fs.readFileSync(outputPath + '/' + file, 'utf8') + const hash = crypto + .createHash('md5') + .update(data) + .digest('hex') + + return hash + } +} + +module.exports = Manifest diff --git a/package.json b/package.json index ac15598..c19b50e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tiny-components/webpack", - "version": "0.4.0", + "version": "0.5.0", "description": "Webpack Wrapper", "repository": { "type": "git", diff --git a/plugins/manifest.js b/plugins/manifest.js new file mode 100644 index 0000000..626c889 --- /dev/null +++ b/plugins/manifest.js @@ -0,0 +1,43 @@ +const Manifest = require('./../manifest.js') +const fs = require('fs') + +/** + * Plugin for Manifest + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://git.node001.net/tiny-components/webpack.git + * + */ +class ManifestPlugin { + + constructor(options) { + this._filename = options.filename, + this._context = options.context + } + + apply(compiler) { + + const pluginName = ManifestPlugin.name + + // webpack module instance can be accessed from the compiler object, + // this ensures that correct version of the module is used + // (do not require/import the webpack or any symbols from it directly). + const { webpack } = compiler + + // Compilation object gives us reference to some useful constants. + const { Compilation } = webpack + + // create manifest object + const manifest = new Manifest(this._filename, this._context) + + compiler.hooks.emit.tapAsync(pluginName, (compilation, callback) => { + const stats = compilation.getStats().toJson() + + manifest.run(stats) + callback() + }) + } +} + +module.exports = ManifestPlugin diff --git a/rules/riot.js b/rules/riot.js index 45db10d..2edadfc 100644 --- a/rules/riot.js +++ b/rules/riot.js @@ -2,12 +2,14 @@ * riot.js * * npm install @riotjs/compiler @riotjs/webpack-loader --save-dev + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://git.node001.net/tiny-components/webpack.git * */ module.exports = { test: /\.riot$/, - exclude: /node_modules/, use: [{ loader: '@riotjs/webpack-loader', options: {