From 50b20f01247a0c4745e86fc16c5a68db206835c8 Mon Sep 17 00:00:00 2001 From: Tomi Pakarinen Date: Tue, 4 Dec 2018 00:18:38 +0200 Subject: [PATCH 1/3] Added ExtractHtmlManifestPlugin --- .../ExtractHtmlManifestPlugin.js | 48 +++++++++++++++++++ packages/react-dev-utils/README.md | 35 ++++++++++++++ packages/react-dev-utils/package.json | 1 + .../react-scripts/config/webpack.config.js | 6 +++ 4 files changed, 90 insertions(+) create mode 100644 packages/react-dev-utils/ExtractHtmlManifestPlugin.js diff --git a/packages/react-dev-utils/ExtractHtmlManifestPlugin.js b/packages/react-dev-utils/ExtractHtmlManifestPlugin.js new file mode 100644 index 00000000000..945bd87668d --- /dev/null +++ b/packages/react-dev-utils/ExtractHtmlManifestPlugin.js @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// This Webpack plugin lets us interpolate custom variables into `index.html`. +// Usage: `new InterpolateHtmlPlugin(HtmlWebpackPlugin, { 'MY_VARIABLE': 42 })` +// Then, you can use %MY_VARIABLE% in your `index.html`. + +// It works in tandem with HtmlWebpackPlugin. +// Learn more about creating plugins like this: +// https://github.com/ampedandwired/html-webpack-plugin#events + +'use strict'; +var fs = require('fs'); +var path = require('path'); + +class ExtractHtmlManifestPlugin { + constructor(htmlWebpackPlugin, opts) { + this.htmlWebpackPlugin = htmlWebpackPlugin; + this.opts = Object.assign( + { + fileName: 'html-manifest.json', + serialize: function(manifest) { + return JSON.stringify(manifest, null, 2); + }, + }, + opts || {} + ); + } + + apply(compiler) { + compiler.hooks.compilation.tap('ExtractHtmlManifestPlugin', compilation => { + this.htmlWebpackPlugin + .getHooks(compilation) + .beforeAssetTagGeneration.tap('ExtractHtmlManifestPlugin', data => { + var outputFolder = compiler.options.output.path; + var outputFile = path.resolve(outputFolder, this.opts.fileName); + var output = this.opts.serialize(data.assets); + fs.writeFileSync(outputFile, output, 'utf8'); + }); + }); + } +} + +module.exports = ExtractHtmlManifestPlugin; diff --git a/packages/react-dev-utils/README.md b/packages/react-dev-utils/README.md index d615f66b5cf..56edcc98166 100644 --- a/packages/react-dev-utils/README.md +++ b/packages/react-dev-utils/README.md @@ -89,6 +89,41 @@ module.exports = { }; ``` +#### `new ExtractHtmlManifestPlugin(htmlWebpackPlugin: HtmlWebpackPlugin, { fileName?: string, serializer?: (any) => string } )` + +This Webpack plugin generates manifest of assets injected to `index.html` by HtmlWebpackPlugin.
+It works in tandem with [HtmlWebpackPlugin](https://github.com/ampedandwired/html-webpack-plugin) 4.x. + +```js +var path = require('path'); +var HtmlWebpackPlugin = require('html-webpack-plugin'); +var ExtractHtmlManifestPlugin = require('react-dev-utils/ExtractHtmlManifestPlugin'); + +// Webpack config +var publicUrl = '/my-custom-url'; + +module.exports = { + output: { + // ... + publicPath: publicUrl + '/', + }, + // ... + plugins: [ + // Generates an `index.html` file with the