From 18c1c99351190bc4a5a4be28f3f6285de371a4d0 Mon Sep 17 00:00:00 2001 From: Jahred Hope Date: Fri, 28 Dec 2018 12:44:59 +1100 Subject: [PATCH] feat: Apply plugins to each config (#15) The plugin may now be applied to each individual configuration that should be included in the render stats instead of applying to the MultiCompiler. Applying to the MultiCompiler is still supported at this stage. Call `render()` to choose the configuration that's code will be used to perform the render. For support with Webpack v5 the documentation now shows applying a compiler to a plugin instead of the other way around. ```js // Previously: multiCompiler.apply(new HtmlRenderPlugin()) new HtmlRenderPlugin().apply(multiCompiler); ``` --- README.md | 303 ++++-------------- options.schema.json | 0 package.json | 3 +- src/index.js | 190 +++++++---- src/renderHtml.js | 13 +- src/schemas/HtmlRenderWebpackPlugin.json | 56 ++++ .../async/__snapshots__/async.test.js.snap | 216 ++++++++++++- tests/test-cases/async/async.test.js | 23 +- tests/test-cases/async/src/render.js | 2 +- tests/test-cases/async/webpack.config.js | 8 +- .../__snapshots__/in-config.test.js.snap | 156 +++++++++ tests/test-cases/in-config/in-config.test.js | 95 ++++++ .../{basic => in-config}/src/client.js | 0 .../{basic => in-config}/src/render.js | 0 .../webpack.default.config.js} | 0 .../in-config/webpack.directory.config.js | 17 + .../in-config/webpack.paths.config.js | 29 ++ .../in-config/webpack.route-info.config.js | 27 ++ .../in-config/webpack.routes.config.js | 20 ++ .../in-config/webpack.zero-config.config.js | 14 + .../__snapshots__/legacy-apply.test.js.snap} | 10 +- .../legacy-apply.test.js} | 2 +- tests/test-cases/legacy-apply/src/client.js | 4 + tests/test-cases/legacy-apply/src/render.js | 8 + .../test-cases/legacy-apply/webpack.config.js | 32 ++ .../multi-compiler-plugin.test.js.snap | 156 +++++++++ .../multi-compiler-plugin.test.js | 132 ++++++++ .../multi-compiler-plugin/src/client.js | 4 + .../multi-compiler-plugin/src/render.js | 8 + .../multi-compiler-plugin/webpack.config.js | 32 ++ .../single-config/webpack.config.js | 2 +- tests/test-cases/watch/watch.test.js | 9 - tests/test-cases/watch/webpack.config.js | 13 +- .../__snapshots__/webpack-stats.test.js.snap | 2 +- tests/test-cases/webpack-stats/src/render.js | 2 +- .../webpack-stats/webpack-stats.test.js | 10 - .../webpack-stats/webpack.config.js | 16 +- yarn.lock | 44 ++- 38 files changed, 1272 insertions(+), 386 deletions(-) create mode 100644 options.schema.json create mode 100644 src/schemas/HtmlRenderWebpackPlugin.json create mode 100644 tests/test-cases/in-config/__snapshots__/in-config.test.js.snap create mode 100644 tests/test-cases/in-config/in-config.test.js rename tests/test-cases/{basic => in-config}/src/client.js (100%) rename tests/test-cases/{basic => in-config}/src/render.js (100%) rename tests/test-cases/{basic/webpack.config.js => in-config/webpack.default.config.js} (100%) create mode 100644 tests/test-cases/in-config/webpack.directory.config.js create mode 100644 tests/test-cases/in-config/webpack.paths.config.js create mode 100644 tests/test-cases/in-config/webpack.route-info.config.js create mode 100644 tests/test-cases/in-config/webpack.routes.config.js create mode 100644 tests/test-cases/in-config/webpack.zero-config.config.js rename tests/test-cases/{basic/__snapshots__/basic.test.js.snap => legacy-apply/__snapshots__/legacy-apply.test.js.snap} (86%) rename tests/test-cases/{basic/basic.test.js => legacy-apply/legacy-apply.test.js} (99%) create mode 100644 tests/test-cases/legacy-apply/src/client.js create mode 100644 tests/test-cases/legacy-apply/src/render.js create mode 100644 tests/test-cases/legacy-apply/webpack.config.js create mode 100644 tests/test-cases/multi-compiler-plugin/__snapshots__/multi-compiler-plugin.test.js.snap create mode 100644 tests/test-cases/multi-compiler-plugin/multi-compiler-plugin.test.js create mode 100644 tests/test-cases/multi-compiler-plugin/src/client.js create mode 100644 tests/test-cases/multi-compiler-plugin/src/render.js create mode 100644 tests/test-cases/multi-compiler-plugin/webpack.config.js diff --git a/README.md b/README.md index 34f5a0d..3af3682 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ $ yarn add webpack html-render-webpack-plugin ```js module.exports = { ..., - plugins: [new HtmlRenderPlugin()] + plugins: [new HtmlRenderPlugin().render()] }; ``` @@ -46,9 +46,9 @@ export default ({ assetsByChunkName }) => { ```html - - - + + + ``` @@ -56,7 +56,35 @@ See [the full example below](#example-client-assets). ## Multiple configuration setup -Create your webpack build with [multiple webpack configurations](https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations). +Pass an instance of HtmlRenderPlugin to each configuration that should be available during render. Call `.render()` on the configuration that should be used to render. + +**webpack.config.js** + +```js +const HtmlRenderPlugin = require("html-render-webpack-plugin"); + +const htmlRenderPlugin = new HtmlRenderPlugin(); +module.exports = [ + { + name: "render", + target: "node", // Creates assets that render HTML that runs well in node + plugins: [htmlRenderPlugin.render()] + }, + { + name: "client", + target: "web", // Creates files that run on the browser + plugins: [htmlRenderPlugin] + } +]; +``` + +See [examples](#examples) for more details. + +### Alternative multiple configuration setup + +Instead of applying the plugin to each configuration you can apply the plugin to the parent [MultiCompiler](https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations). + +This is not recommended and may eventually deprecated in future releases. **webpack.config.js** @@ -86,7 +114,7 @@ Apply the plugin to your compiler. ```js const HtmlRenderPlugin = require("html-render-webpack-plugin"); -multiCompiler.apply(new HtmlRenderPlugin()); +new HtmlRenderPlugin().apply(multiCompiler); ``` Start the build @@ -106,8 +134,6 @@ const server = new DevServer(multiCompiler, { server.listen(8080, "localhost", () => {}); ``` -See [examples](#examples) for more details. - # Options ## Option: renderEntry _string_ @@ -117,19 +143,21 @@ See [examples](#examples) for more details. The entry to use when rendering. Override when using [object syntax](https://webpack.js.org/concepts/entry-points/#object-syntax) in webpack entry. ```js -new HtmlRenderPlugin({ - renderEntry: "myRender" -}); ``` **webpack.config.js** -``` -{ +```js +module.exports = { entry: { - myRender: './src/myRender.js', - } -} + myRender: "./src/myRender.js" + }, + plugins: [ + new HtmlRenderPlugin({ + renderEntry: "myRender" + }).render() + ] +}; ``` ## Option: renderDirectory _string_ @@ -221,81 +249,10 @@ new HtmlRenderPlugin({ # Examples -## Example: Basic - -**build.js** - -```js -const path = require("path"); -const webpack = require("webpack"); -const config = require("./webpack.config"); -const HtmlRenderPlugin = require("html-render-webpack-plugin"); - -const compiler = webpack(config); - -// Apply the plugin directly to the MultiCompiler -compiler.apply(new HtmlRenderPlugin()); - -compiler.run((error, stats) => { - console.log("Build complete"); -}); -``` - -**webpack.config.js** - -```js -module.exports = [ - { - name: "client", - target: "web", - output: { - filename: "client-[name]-[contenthash].js", - } - entry: path.resolve("src", "client.js") - }, - { - name: "render", - target: "node", - output: { - libraryExport: "default", - libraryTarget: "umd2", - filename: "render-[name]-[contenthash].js", - }, - entry: render: path.resolve("src", "render.js"), - }), -] -``` - ## Example: Client assets An example of using `mapStatsToParams` to create `