├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── .npmignore ├── .prettierignore ├── .prettierrc ├── .travis.yml ├── Contributing.md ├── License.md ├── README.md ├── example ├── package-lock.json ├── package.json ├── src │ ├── index.js │ └── main.css └── webpack.config.js ├── jest.config.js ├── package-lock.json ├── package.json ├── src └── index.ts ├── test ├── __snapshots__ │ └── plugin.test.ts.snap ├── fixtures │ ├── another.js │ ├── css.js │ ├── index.js │ └── main.css └── plugin.test.ts └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.{json,yml,md,babelrc,remarkrc}] 12 | indent_style = space 13 | indent_size = 2 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/* 3 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tamia/typescript" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | Thumbs.db 4 | .idea/ 5 | .vscode/ 6 | *.sublime-project 7 | *.sublime-workspace 8 | *.log 9 | yarn.lock 10 | .eslintcache 11 | coverage/ 12 | Changelog.md 13 | dist/ 14 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | __tests__/ 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | package.json 2 | dist/ 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "singleQuote": true, 4 | "trailingComma": "es5", 5 | "overrides": [ 6 | { 7 | "files": "*.md", 8 | "options": { 9 | "printWidth": 70, 10 | "useTabs": false, 11 | "trailingComma": "none", 12 | "proseWrap": "never" 13 | } 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: 3 | directories: 4 | - node_modules 5 | node_js: 6 | - 10 7 | - 11 8 | - 12 9 | after_success: 10 | - npx semantic-release 11 | branches: 12 | except: 13 | - /^v\d+\.\d+\.\d+$/ 14 | -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | We love pull requests. And following this guidelines will make your pull request easier to merge. 4 | 5 | If you want to contribute but don’t know what to do, take a look at these two labels: [help wanted](https://github.com/styleguidist/mini-html-webpack-plugin/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [good first issue](https://github.com/styleguidist/mini-html-webpack-plugin/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22). 6 | 7 | ## Prerequisites 8 | 9 | - If it’s your first pull request, watch [this amazing course](http://makeapullrequest.com/) by [Kent C. Dodds](https://twitter.com/kentcdodds). 10 | - Install [EditorConfig](http://editorconfig.org/) plugin for your code editor to make sure it uses correct settings. 11 | - Fork the repository and clone your fork. 12 | - Install dependencies: `npm install`. 13 | 14 | ## Development workflow 15 | 16 | Run linters and tests: 17 | 18 | ```bash 19 | npm test 20 | ``` 21 | 22 | Or run tests in watch mode: 23 | 24 | ```bash 25 | npm run test:watch 26 | ``` 27 | 28 | To update Jest snapshots: 29 | 30 | ```bash 31 | npm run test:jest -- -u 32 | ``` 33 | 34 | **Don’t forget to add tests and update documentation for your changes.** 35 | 36 | **Please update npm lock file (`package-lock.json`) if you add or update dependencies.** 37 | 38 | ## Other notes 39 | 40 | - If you have commit access to repository and want to make big change or not sure about something, make a new branch and open pull request. 41 | - We’re using [Prettier](https://github.com/prettier/prettier) to format JavaScript, so don’t worry much about code formatting. 42 | - Don’t commit generated files, like minified JavaScript. 43 | - Don’t change version number and change log. 44 | 45 | ## Need help? 46 | 47 | Feel free to ask. 48 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | # The MIT License 2 | 3 | Copyright 2019 Juho Vepsalainen, contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mini-html-webpack-plugin: a miniature version of html-webpack-plugin with only necessary features 2 | 3 | [![npm](https://img.shields.io/npm/v/mini-html-webpack-plugin.svg)](https://www.npmjs.com/package/mini-html-webpack-plugin) [![Build Status](https://travis-ci.org/styleguidist/mini-html-webpack-plugin.svg)](https://travis-ci.org/styleguidist/mini-html-webpack-plugin) 4 | 5 | The plugin writes CSS and JS asset paths for you automatically. It works with webpack 4 or higher. 6 | 7 | **It does not work with html-webpack-plugin plugins!** 8 | 9 | ## Usage 10 | 11 | ```sh 12 | npm install mini-html-webpack-plugin 13 | ``` 14 | 15 | ```javascript 16 | const { MiniHtmlWebpackPlugin } = require('mini-html-webpack-plugin'); 17 | 18 | const config = { 19 | plugins: [ 20 | new MiniHtmlWebpackPlugin({ 21 | // Optional, defaults to `index.html` 22 | filename: 'demo.html', 23 | // Optional 24 | publicPath: 'demo/', 25 | context: { 26 | title: 'Webpack demo', 27 | // Optional, defaults to `{ lang: 'en' }` 28 | htmlAttributes: { 29 | lang: 'en' 30 | }, 31 | // Optional, any additional HTML attached within 32 | head: '', 33 | // Optional, any additional HTML attached within 34 | body: '', 35 | // Optional 36 | cssAttributes: { 37 | rel: 'preload', 38 | as: 'style' 39 | }, 40 | // Optional 41 | jsAttributes: { 42 | defer: true 43 | } 44 | }, 45 | // Optional, use this for choosing chunks to include to your page. 46 | // See the expanded example below. 47 | chunks: ['app'] 48 | }) 49 | ] 50 | }; 51 | ``` 52 | 53 | ### Multiple pages 54 | 55 | It's possible to use `MiniHtmlWebpackPlugin` to develop sites with multiple pages. It can be combined with webpack's bundle splitting so you can share common code across different pages. 56 | 57 | To achieve this, you'll have to define `entry` against each the code for each page and define `MiniHtmlWebpackPlugin` to match them. In practice you might want to abstract this pairing but to give you the full idea, consider the example below. 58 | 59 | ```javascript 60 | const { MiniHtmlWebpackPlugin } = require('mini-html-webpack-plugin'); 61 | 62 | const config = { 63 | entry: { 64 | app: './app.js', 65 | another: './another.js' 66 | }, 67 | plugins: [ 68 | new MiniHtmlWebpackPlugin({ 69 | filename: 'index.html', 70 | chunks: ['app'], 71 | }), 72 | new MiniHtmlWebpackPlugin({ 73 | filename: 'another.html', 74 | chunks: ['another'], 75 | }, 76 | ], 77 | }; 78 | ``` 79 | 80 | ### HTML minification 81 | 82 | ```javascript 83 | const minify = require('html-minifier').minify; 84 | const { MiniHtmlWebpackPlugin } = require('mini-html-webpack-plugin'); 85 | 86 | const config = { 87 | plugins: [ 88 | new MiniHtmlWebpackPlugin({ 89 | context: { 90 | title: 'Minification demo' 91 | }, 92 | template: (context) => 93 | minify(MiniHtmlWebpackPlugin.defaultTemplate(context)) 94 | }) 95 | ] 96 | }; 97 | ``` 98 | 99 | ### Custom templates 100 | 101 | Use [@vxna/mini-html-webpack-template](https://www.npmjs.com/package/@vxna/mini-html-webpack-template) to add an app container div, a favicon, meta tags, inline JavaScript or CSS. 102 | 103 | Or define a template function to generate your own code. 104 | 105 | The template function may return a string or a `Promise` resolving to a string. 106 | 107 | ```js 108 | const { 109 | MiniHtmlWebpackPlugin, 110 | generateAttributes, 111 | generateCSSReferences, 112 | generateJSReferences 113 | } = require('mini-html-webpack-plugin'); 114 | 115 | const config = { 116 | plugins: [ 117 | new MiniHtmlWebpackPlugin({ 118 | filename: 'demo.html', 119 | publicPath: 'demo/', 120 | // `context` is available in `template` below 121 | context: { 122 | title: 'Webpack demo', 123 | htmlAttributes: { 124 | lang: 'en' 125 | }, 126 | cssAttributes: { 127 | rel: 'preload', 128 | as: 'style' 129 | }, 130 | jsAttributes: { 131 | defer: true 132 | } 133 | }, 134 | template: ({ 135 | css, 136 | js, 137 | publicPath, 138 | title, 139 | htmlAttributes, 140 | cssAttributes, 141 | jsAttributes 142 | }) => { 143 | const htmlAttrs = generateAttributes(htmlAttributes); 144 | 145 | const cssTags = generateCSSReferences({ 146 | files: css, 147 | attributes: cssAttributes, 148 | publicPath 149 | }); 150 | 151 | const jsTags = generateJSReferences({ 152 | files: js, 153 | attributes: jsAttributes, 154 | publicPath 155 | }); 156 | 157 | return ` 158 | 159 | 160 | 161 | ${title} 162 | ${cssTags} 163 | 164 | 165 | ${jsTags} 166 | 167 | `; 168 | } 169 | }) 170 | ] 171 | }; 172 | ``` 173 | 174 | ## License 175 | 176 | MIT. 177 | -------------------------------------------------------------------------------- /example/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "requires": true, 3 | "lockfileVersion": 1, 4 | "dependencies": { 5 | "@types/json-schema": { 6 | "version": "7.0.6", 7 | "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.6.tgz", 8 | "integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==" 9 | }, 10 | "ajv": { 11 | "version": "6.12.6", 12 | "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", 13 | "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", 14 | "requires": { 15 | "fast-deep-equal": "^3.1.1", 16 | "fast-json-stable-stringify": "^2.0.0", 17 | "json-schema-traverse": "^0.4.1", 18 | "uri-js": "^4.2.2" 19 | } 20 | }, 21 | "ajv-keywords": { 22 | "version": "3.5.2", 23 | "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.5.2.tgz", 24 | "integrity": "sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==" 25 | }, 26 | "ansi-styles": { 27 | "version": "4.3.0", 28 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", 29 | "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", 30 | "requires": { 31 | "color-convert": "^2.0.1" 32 | } 33 | }, 34 | "big.js": { 35 | "version": "5.2.2", 36 | "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", 37 | "integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==" 38 | }, 39 | "camelcase": { 40 | "version": "5.3.1", 41 | "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", 42 | "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" 43 | }, 44 | "chalk": { 45 | "version": "3.0.0", 46 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-3.0.0.tgz", 47 | "integrity": "sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==", 48 | "requires": { 49 | "ansi-styles": "^4.1.0", 50 | "supports-color": "^7.1.0" 51 | } 52 | }, 53 | "color-convert": { 54 | "version": "2.0.1", 55 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", 56 | "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", 57 | "requires": { 58 | "color-name": "~1.1.4" 59 | } 60 | }, 61 | "color-name": { 62 | "version": "1.1.4", 63 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", 64 | "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" 65 | }, 66 | "css-loader": { 67 | "version": "5.0.0", 68 | "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-5.0.0.tgz", 69 | "integrity": "sha512-9g35eXRBgjvswyJWoqq/seWp+BOxvUl8IinVNTsUBFFxtwfEYvlmEn6ciyn0liXGbGh5HyJjPGCuobDSfqMIVg==", 70 | "requires": { 71 | "camelcase": "^6.1.0", 72 | "cssesc": "^3.0.0", 73 | "icss-utils": "^5.0.0", 74 | "loader-utils": "^2.0.0", 75 | "postcss": "^8.1.1", 76 | "postcss-modules-extract-imports": "^3.0.0", 77 | "postcss-modules-local-by-default": "^4.0.0", 78 | "postcss-modules-scope": "^3.0.0", 79 | "postcss-modules-values": "^4.0.0", 80 | "postcss-value-parser": "^4.1.0", 81 | "schema-utils": "^3.0.0", 82 | "semver": "^7.3.2" 83 | }, 84 | "dependencies": { 85 | "camelcase": { 86 | "version": "6.1.0", 87 | "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.1.0.tgz", 88 | "integrity": "sha512-WCMml9ivU60+8rEJgELlFp1gxFcEGxwYleE3bziHEDeqsqAWGHdimB7beBFGjLzVNgPGyDsfgXLQEYMpmIFnVQ==" 89 | } 90 | } 91 | }, 92 | "cssesc": { 93 | "version": "3.0.0", 94 | "resolved": "https://registry.npmjs.org/cssesc/-/cssesc-3.0.0.tgz", 95 | "integrity": "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==" 96 | }, 97 | "decamelize": { 98 | "version": "1.2.0", 99 | "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", 100 | "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" 101 | }, 102 | "emojis-list": { 103 | "version": "3.0.0", 104 | "resolved": "https://registry.npmjs.org/emojis-list/-/emojis-list-3.0.0.tgz", 105 | "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==" 106 | }, 107 | "fast-deep-equal": { 108 | "version": "3.1.3", 109 | "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", 110 | "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" 111 | }, 112 | "fast-json-stable-stringify": { 113 | "version": "2.1.0", 114 | "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", 115 | "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" 116 | }, 117 | "find-up": { 118 | "version": "4.1.0", 119 | "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", 120 | "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", 121 | "requires": { 122 | "locate-path": "^5.0.0", 123 | "path-exists": "^4.0.0" 124 | } 125 | }, 126 | "has-flag": { 127 | "version": "4.0.0", 128 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", 129 | "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" 130 | }, 131 | "icss-utils": { 132 | "version": "5.0.0", 133 | "resolved": "https://registry.npmjs.org/icss-utils/-/icss-utils-5.0.0.tgz", 134 | "integrity": "sha512-aF2Cf/CkEZrI/vsu5WI/I+akFgdbwQHVE9YRZxATrhH4PVIe6a3BIjwjEcW+z+jP/hNh+YvM3lAAn1wJQ6opSg==" 135 | }, 136 | "import-local": { 137 | "version": "3.0.2", 138 | "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.0.2.tgz", 139 | "integrity": "sha512-vjL3+w0oulAVZ0hBHnxa/Nm5TAurf9YLQJDhqRZyqb+VKGOB6LU8t9H1Nr5CIo16vh9XfJTOoHwU0B71S557gA==", 140 | "requires": { 141 | "pkg-dir": "^4.2.0", 142 | "resolve-cwd": "^3.0.0" 143 | } 144 | }, 145 | "indexes-of": { 146 | "version": "1.0.1", 147 | "resolved": "https://registry.npmjs.org/indexes-of/-/indexes-of-1.0.1.tgz", 148 | "integrity": "sha1-8w9xbI4r00bHtn0985FVZqfAVgc=" 149 | }, 150 | "json-schema-traverse": { 151 | "version": "0.4.1", 152 | "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", 153 | "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" 154 | }, 155 | "json5": { 156 | "version": "2.2.3", 157 | "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", 158 | "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==" 159 | }, 160 | "loader-utils": { 161 | "version": "2.0.4", 162 | "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.4.tgz", 163 | "integrity": "sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==", 164 | "requires": { 165 | "big.js": "^5.2.2", 166 | "emojis-list": "^3.0.0", 167 | "json5": "^2.1.2" 168 | } 169 | }, 170 | "locate-path": { 171 | "version": "5.0.0", 172 | "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", 173 | "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", 174 | "requires": { 175 | "p-locate": "^4.1.0" 176 | } 177 | }, 178 | "lru-cache": { 179 | "version": "6.0.0", 180 | "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", 181 | "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", 182 | "requires": { 183 | "yallist": "^4.0.0" 184 | } 185 | }, 186 | "mini-css-extract-plugin": { 187 | "version": "1.1.0", 188 | "resolved": "https://registry.npmjs.org/mini-css-extract-plugin/-/mini-css-extract-plugin-1.1.0.tgz", 189 | "integrity": "sha512-0bTS+Fg2tGe3dFAgfiN7+YRO37oyQM7/vjFvZF1nXSCJ/sy0tGpeme8MbT4BCpUuUphKwTh9LH/uuTcWRr9DPA==", 190 | "requires": { 191 | "loader-utils": "^2.0.0", 192 | "schema-utils": "^3.0.0", 193 | "webpack-sources": "^1.1.0" 194 | } 195 | }, 196 | "nanoid": { 197 | "version": "3.2.0", 198 | "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.2.0.tgz", 199 | "integrity": "sha512-fmsZYa9lpn69Ad5eDn7FMcnnSR+8R34W9qJEijxYhTbfOWzr22n1QxCMzXLK+ODyW2973V3Fux959iQoUxzUIA==" 200 | }, 201 | "p-limit": { 202 | "version": "2.3.0", 203 | "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", 204 | "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", 205 | "requires": { 206 | "p-try": "^2.0.0" 207 | } 208 | }, 209 | "p-locate": { 210 | "version": "4.1.0", 211 | "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", 212 | "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", 213 | "requires": { 214 | "p-limit": "^2.2.0" 215 | } 216 | }, 217 | "p-try": { 218 | "version": "2.2.0", 219 | "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", 220 | "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" 221 | }, 222 | "path-exists": { 223 | "version": "4.0.0", 224 | "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", 225 | "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==" 226 | }, 227 | "path-parse": { 228 | "version": "1.0.7", 229 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", 230 | "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" 231 | }, 232 | "pkg-dir": { 233 | "version": "4.2.0", 234 | "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", 235 | "integrity": "sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==", 236 | "requires": { 237 | "find-up": "^4.0.0" 238 | } 239 | }, 240 | "postcss": { 241 | "version": "8.2.15", 242 | "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.2.15.tgz", 243 | "integrity": "sha512-2zO3b26eJD/8rb106Qu2o7Qgg52ND5HPjcyQiK2B98O388h43A448LCslC0dI2P97wCAQRJsFvwTRcXxTKds+Q==", 244 | "requires": { 245 | "colorette": "^1.2.2", 246 | "nanoid": "^3.1.23", 247 | "source-map": "^0.6.1" 248 | }, 249 | "dependencies": { 250 | "colorette": { 251 | "version": "1.2.2", 252 | "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.2.tgz", 253 | "integrity": "sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==" 254 | } 255 | } 256 | }, 257 | "postcss-modules-extract-imports": { 258 | "version": "3.0.0", 259 | "resolved": "https://registry.npmjs.org/postcss-modules-extract-imports/-/postcss-modules-extract-imports-3.0.0.tgz", 260 | "integrity": "sha512-bdHleFnP3kZ4NYDhuGlVK+CMrQ/pqUm8bx/oGL93K6gVwiclvX5x0n76fYMKuIGKzlABOy13zsvqjb0f92TEXw==" 261 | }, 262 | "postcss-modules-local-by-default": { 263 | "version": "4.0.0", 264 | "resolved": "https://registry.npmjs.org/postcss-modules-local-by-default/-/postcss-modules-local-by-default-4.0.0.tgz", 265 | "integrity": "sha512-sT7ihtmGSF9yhm6ggikHdV0hlziDTX7oFoXtuVWeDd3hHObNkcHRo9V3yg7vCAY7cONyxJC/XXCmmiHHcvX7bQ==", 266 | "requires": { 267 | "icss-utils": "^5.0.0", 268 | "postcss-selector-parser": "^6.0.2", 269 | "postcss-value-parser": "^4.1.0" 270 | } 271 | }, 272 | "postcss-modules-scope": { 273 | "version": "3.0.0", 274 | "resolved": "https://registry.npmjs.org/postcss-modules-scope/-/postcss-modules-scope-3.0.0.tgz", 275 | "integrity": "sha512-hncihwFA2yPath8oZ15PZqvWGkWf+XUfQgUGamS4LqoP1anQLOsOJw0vr7J7IwLpoY9fatA2qiGUGmuZL0Iqlg==", 276 | "requires": { 277 | "postcss-selector-parser": "^6.0.4" 278 | } 279 | }, 280 | "postcss-modules-values": { 281 | "version": "4.0.0", 282 | "resolved": "https://registry.npmjs.org/postcss-modules-values/-/postcss-modules-values-4.0.0.tgz", 283 | "integrity": "sha512-RDxHkAiEGI78gS2ofyvCsu7iycRv7oqw5xMWn9iMoR0N/7mf9D50ecQqUo5BZ9Zh2vH4bCUR/ktCqbB9m8vJjQ==", 284 | "requires": { 285 | "icss-utils": "^5.0.0" 286 | } 287 | }, 288 | "postcss-selector-parser": { 289 | "version": "6.0.4", 290 | "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-6.0.4.tgz", 291 | "integrity": "sha512-gjMeXBempyInaBqpp8gODmwZ52WaYsVOsfr4L4lDQ7n3ncD6mEyySiDtgzCT+NYC0mmeOLvtsF8iaEf0YT6dBw==", 292 | "requires": { 293 | "cssesc": "^3.0.0", 294 | "indexes-of": "^1.0.1", 295 | "uniq": "^1.0.1", 296 | "util-deprecate": "^1.0.2" 297 | } 298 | }, 299 | "postcss-value-parser": { 300 | "version": "4.1.0", 301 | "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.1.0.tgz", 302 | "integrity": "sha512-97DXOFbQJhk71ne5/Mt6cOu6yxsSfM0QGQyl0L25Gca4yGWEGJaig7l7gbCX623VqTBNGLRLaVUCnNkcedlRSQ==" 303 | }, 304 | "punycode": { 305 | "version": "2.1.1", 306 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", 307 | "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" 308 | }, 309 | "rechoir": { 310 | "version": "0.7.0", 311 | "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.7.0.tgz", 312 | "integrity": "sha512-ADsDEH2bvbjltXEP+hTIAmeFekTFK0V2BTxMkok6qILyAJEXV0AFfoWcAq4yfll5VdIMd/RVXq0lR+wQi5ZU3Q==", 313 | "requires": { 314 | "resolve": "^1.9.0" 315 | } 316 | }, 317 | "resolve": { 318 | "version": "1.17.0", 319 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", 320 | "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", 321 | "requires": { 322 | "path-parse": "^1.0.6" 323 | } 324 | }, 325 | "resolve-cwd": { 326 | "version": "3.0.0", 327 | "resolved": "https://registry.npmjs.org/resolve-cwd/-/resolve-cwd-3.0.0.tgz", 328 | "integrity": "sha512-OrZaX2Mb+rJCpH/6CpSqt9xFVpN++x01XnN2ie9g6P5/3xelLAkXWVADpdz1IHD/KFfEXyE6V0U01OQ3UO2rEg==", 329 | "requires": { 330 | "resolve-from": "^5.0.0" 331 | } 332 | }, 333 | "resolve-from": { 334 | "version": "5.0.0", 335 | "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz", 336 | "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==" 337 | }, 338 | "schema-utils": { 339 | "version": "3.0.0", 340 | "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz", 341 | "integrity": "sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==", 342 | "requires": { 343 | "@types/json-schema": "^7.0.6", 344 | "ajv": "^6.12.5", 345 | "ajv-keywords": "^3.5.2" 346 | } 347 | }, 348 | "semver": { 349 | "version": "7.5.3", 350 | "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz", 351 | "integrity": "sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==", 352 | "requires": { 353 | "lru-cache": "^6.0.0" 354 | } 355 | }, 356 | "source-list-map": { 357 | "version": "2.0.1", 358 | "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz", 359 | "integrity": "sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==" 360 | }, 361 | "source-map": { 362 | "version": "0.6.1", 363 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 364 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" 365 | }, 366 | "supports-color": { 367 | "version": "7.2.0", 368 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", 369 | "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", 370 | "requires": { 371 | "has-flag": "^4.0.0" 372 | } 373 | }, 374 | "uniq": { 375 | "version": "1.0.1", 376 | "resolved": "https://registry.npmjs.org/uniq/-/uniq-1.0.1.tgz", 377 | "integrity": "sha1-sxxa6CVIRKOoKBVBzisEuGWnNP8=" 378 | }, 379 | "uri-js": { 380 | "version": "4.4.0", 381 | "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.0.tgz", 382 | "integrity": "sha512-B0yRTzYdUCCn9n+F4+Gh4yIDtMQcaJsmYBDsTSG8g/OejKBodLQ2IHfN3bM7jUsRXndopT7OIXWdYqc1fjmV6g==", 383 | "requires": { 384 | "punycode": "^2.1.0" 385 | } 386 | }, 387 | "util-deprecate": { 388 | "version": "1.0.2", 389 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", 390 | "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" 391 | }, 392 | "webpack-nano": { 393 | "version": "1.1.0", 394 | "resolved": "https://registry.npmjs.org/webpack-nano/-/webpack-nano-1.1.0.tgz", 395 | "integrity": "sha512-fxHIn3m3G1NWvGyGbDu0aS3nolkcgTp3SgsbDdKh1OlW2UzluI+vKeHgQDOg85qQzlJ0X/Sn2kTG3UNMi1NpUw==", 396 | "requires": { 397 | "chalk": "^3.0.0", 398 | "import-local": "^3.0.2", 399 | "rechoir": "^0.7.0", 400 | "yargs-parser": "^18.1.3" 401 | } 402 | }, 403 | "webpack-sources": { 404 | "version": "1.4.3", 405 | "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.4.3.tgz", 406 | "integrity": "sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==", 407 | "requires": { 408 | "source-list-map": "^2.0.0", 409 | "source-map": "~0.6.1" 410 | } 411 | }, 412 | "yallist": { 413 | "version": "4.0.0", 414 | "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", 415 | "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" 416 | }, 417 | "yargs-parser": { 418 | "version": "18.1.3", 419 | "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", 420 | "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", 421 | "requires": { 422 | "camelcase": "^5.0.0", 423 | "decamelize": "^1.2.0" 424 | } 425 | } 426 | } 427 | } 428 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "build": "wp" 4 | }, 5 | "dependencies": { 6 | "css-loader": "^5.0.0", 7 | "mini-css-extract-plugin": "^1.1.0", 8 | "webpack-nano": "^1.1.0" 9 | }, 10 | "main": "./src/index.js" 11 | } 12 | -------------------------------------------------------------------------------- /example/src/index.js: -------------------------------------------------------------------------------- 1 | import './main.css'; 2 | 3 | const component = (text = 'Hello world') => { 4 | const element = document.createElement('div'); 5 | element.innerHTML = text; 6 | return element; 7 | }; 8 | 9 | document.body.appendChild(component()); 10 | -------------------------------------------------------------------------------- /example/src/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: orange; 3 | } 4 | -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | // eslint-disable-next-line @typescript-eslint/no-var-requires 2 | const { MiniHtmlWebpackPlugin } = require('../'); 3 | // eslint-disable-next-line @typescript-eslint/no-var-requires 4 | const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 5 | 6 | module.exports = { 7 | mode: 'production', 8 | module: { 9 | rules: [ 10 | { 11 | test: /\.css$/, 12 | use: [MiniCssExtractPlugin.loader, 'css-loader'], 13 | }, 14 | ], 15 | }, 16 | plugins: [ 17 | new MiniHtmlWebpackPlugin(), 18 | new MiniCssExtractPlugin({ 19 | filename: '[name].css', 20 | }), 21 | ], 22 | }; 23 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | globals: { 4 | 'ts-jest': { 5 | // Disable type-checking in Jest tests since we test separately 6 | isolatedModules: true, 7 | }, 8 | }, 9 | }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mini-html-webpack-plugin", 3 | "version": "0.0.0-development", 4 | "description": "A miniature version of html-webpack-plugin with only necessary features", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "files": [ 8 | "dist/index.js", 9 | "dist/index.d.ts" 10 | ], 11 | "scripts": { 12 | "prepublishOnly": "npm run build", 13 | "build": "rimraf ./dist && tsc", 14 | "watch": "tsc --watch", 15 | "pretest": "npm run lint", 16 | "test": "npm run test:jest", 17 | "posttest": "npm run format && npm run build", 18 | "lint": "eslint **/*.ts --cache --fix --ignore-path .gitignore", 19 | "format": "prettier --write \"**/*.{ts,md}\"", 20 | "test:jest": "jest", 21 | "test:watch": "jest --watch", 22 | "test:coverage": "jest --coverage" 23 | }, 24 | "keywords": [ 25 | "webpack", 26 | "html", 27 | "template" 28 | ], 29 | "author": "Juho Vepsalainen", 30 | "contributors": [ 31 | { 32 | "name": "Artem Sapegin", 33 | "url": "http://sapegin.me" 34 | } 35 | ], 36 | "license": "MIT", 37 | "dependencies": { 38 | "webpack-sources": "^2.0.1" 39 | }, 40 | "homepage": "https://github.com/styleguidist/mini-html-webpack-plugin", 41 | "repository": "styleguidist/mini-html-webpack-plugin", 42 | "engines": { 43 | "node": ">=10" 44 | }, 45 | "devDependencies": { 46 | "@types/jest": "^26.0.14", 47 | "@types/webpack-sources": "^2.0.0", 48 | "@typescript-eslint/eslint-plugin": "^2.26.0", 49 | "@typescript-eslint/parser": "^2.26.0", 50 | "css-loader": "^5.0.0", 51 | "eslint": "^6.8.0", 52 | "eslint-config-tamia": "^7.1.2", 53 | "husky": "^4.2.3", 54 | "jest": "^26.5.3", 55 | "lint-staged": "^10.1.0", 56 | "memfs": "^3.2.0", 57 | "mini-css-extract-plugin": "^1.1.0", 58 | "prettier": "^2.1.2", 59 | "rimraf": "^3.0.2", 60 | "ts-jest": "^26.4.1", 61 | "typescript": "^4.0.3", 62 | "webpack": "^5.1.0" 63 | }, 64 | "peerDependencies": { 65 | "webpack": ">=4" 66 | }, 67 | "lint-staged": { 68 | "*.{ts,md}": [ 69 | "prettier --write" 70 | ], 71 | "*.ts": [ 72 | "eslint --fix" 73 | ] 74 | }, 75 | "husky": { 76 | "hooks": { 77 | "pre-commit": "lint-staged" 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import webpack, { Compilation, WebpackPluginInstance } from 'webpack'; 3 | 4 | type Attributes = Record; 5 | 6 | type Context = { 7 | title?: string; 8 | htmlAttributes?: Attributes; 9 | cssAttributes?: Attributes; 10 | jsAttributes?: Attributes; 11 | }; 12 | 13 | type Files = Record; 14 | 15 | type TemplateParameters = { 16 | css?: string[]; 17 | js?: string[]; 18 | body?: string; 19 | head?: string; 20 | publicPath: string; 21 | } & Context; 22 | 23 | type Options = { 24 | filename?: string; 25 | publicPath?: string; 26 | context?: Context; 27 | template?: (args: TemplateParameters) => string | Promise; 28 | chunks?: string[]; 29 | }; 30 | 31 | function getFiles( 32 | entrypoints: Compilation['entrypoints'], 33 | chunks?: string[] 34 | ): Files { 35 | const ret: Files = {}; 36 | 37 | entrypoints.forEach((entry) => { 38 | if (chunks && !chunks.includes(entry.name)) { 39 | return; 40 | } 41 | 42 | entry.getFiles().forEach((file: string) => { 43 | const extension = path.extname(file).replace(/\./, ''); 44 | 45 | if (!ret[extension]) { 46 | ret[extension] = []; 47 | } 48 | 49 | ret[extension].push(file); 50 | }); 51 | }); 52 | 53 | return ret; 54 | } 55 | 56 | function generateAttributes(attributes = {}) { 57 | const stringAttributes = Object.entries(attributes); 58 | 59 | if (stringAttributes.length === 0) { 60 | return ''; 61 | } 62 | 63 | return ( 64 | ' ' + 65 | stringAttributes 66 | .map((attr) => { 67 | if (attr[1] === true) { 68 | return attr[0]; 69 | } 70 | return `${attr[0]}="${attr[1]}"`; 71 | }) 72 | .join(' ') 73 | ); 74 | } 75 | 76 | function generateCSSReferences({ 77 | files = [], 78 | publicPath = '', 79 | attributes = {}, 80 | }: { 81 | files: string[]; 82 | publicPath: string; 83 | attributes: (Attributes & { rel?: string }) | undefined; 84 | }): string { 85 | if (!attributes) { 86 | return ''; 87 | } 88 | 89 | const allAttributes = { 90 | ...attributes, 91 | rel: 'rel' in attributes ? attributes.rel : 'stylesheet', 92 | }; 93 | 94 | return files 95 | .map( 96 | (file) => 97 | `` 98 | ) 99 | .join(''); 100 | } 101 | 102 | function generateJSReferences({ 103 | files = [], 104 | publicPath = '', 105 | attributes = {}, 106 | }: { 107 | files: string[]; 108 | publicPath: string; 109 | attributes: Attributes | undefined; 110 | }): string { 111 | if (!attributes) { 112 | return ''; 113 | } 114 | 115 | return files 116 | .map( 117 | (file) => 118 | `` 121 | ) 122 | .join(''); 123 | } 124 | 125 | function defaultTemplate({ 126 | css = [], 127 | js = [], 128 | publicPath = '', 129 | title = '', 130 | htmlAttributes = { 131 | lang: 'en', 132 | }, 133 | head = '', 134 | body = '', 135 | cssAttributes = {}, 136 | jsAttributes = {}, 137 | }: TemplateParameters) { 138 | const htmlAttrs = generateAttributes(htmlAttributes); 139 | 140 | const cssTags = generateCSSReferences({ 141 | files: css, 142 | attributes: cssAttributes, 143 | publicPath, 144 | }); 145 | 146 | const jsTags = generateJSReferences({ 147 | files: js, 148 | attributes: jsAttributes, 149 | publicPath, 150 | }); 151 | 152 | return ` 153 | 154 | 155 | 156 | ${title} 157 | ${head}${cssTags} 158 | 159 | 160 | ${body}${jsTags} 161 | 162 | `; 163 | } 164 | 165 | function isWebpack4() { 166 | return webpack.version.split('.')[0] === '4'; 167 | } 168 | 169 | class MiniHtmlWebpackPlugin implements WebpackPluginInstance { 170 | private options: Options; 171 | 172 | public constructor(options: Options) { 173 | this.options = options || {}; 174 | this.webpack4plugin = this.webpack4plugin.bind(this); 175 | this.webpack5plugin = this.webpack5plugin.bind(this); 176 | } 177 | 178 | private webpack4plugin(compilation: Compilation, callback: () => {}) { 179 | const { 180 | filename = 'index.html', 181 | publicPath = '', 182 | template, 183 | context, 184 | chunks, 185 | } = this.options; 186 | const files = getFiles(compilation.entrypoints, chunks); 187 | const options = Object.assign({}, { publicPath }, context, files); 188 | 189 | Promise.resolve((template || defaultTemplate)(options)).then((source) => { 190 | // eslint-disable-next-line @typescript-eslint/no-var-requires 191 | const sources = require('webpack-sources'); 192 | 193 | compilation.assets[filename] = new sources.RawSource(source, true); 194 | callback(); 195 | }); 196 | } 197 | 198 | private webpack5plugin(compilation: Compilation) { 199 | const { 200 | filename = 'index.html', 201 | publicPath = '', 202 | template, 203 | context, 204 | chunks, 205 | } = this.options; 206 | // TODO: Consider separating getFiles result to a structure as it can override other contents if file extension matches. 207 | return Promise.resolve( 208 | (template || defaultTemplate)({ 209 | publicPath, 210 | ...context, 211 | ...getFiles(compilation.entrypoints, chunks), 212 | }) 213 | ).then((source) => { 214 | // webpacks 5 exports `webpack-sources` to avoid cache problems 215 | // eslint-disable-next-line @typescript-eslint/no-var-requires 216 | const { sources } = require('webpack'); 217 | 218 | compilation.emitAsset(filename, new sources.RawSource(source, true)); 219 | }); 220 | } 221 | 222 | public apply(compiler: webpack.Compiler) { 223 | const pluginName = 'MiniHtmlWebpackPlugin'; 224 | 225 | if (isWebpack4()) { 226 | // @ts-ignore: Ignore for webpack 4 due to different typing 227 | compiler.hooks.emit.tapAsync(pluginName, this.webpack4plugin); 228 | } else { 229 | compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { 230 | compilation.hooks.processAssets.tapPromise( 231 | { 232 | name: pluginName, 233 | // https://github.com/webpack/webpack/blob/master/lib/Compilation.js#L3280 234 | stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, 235 | }, 236 | () => this.webpack5plugin(compilation) 237 | ); 238 | }); 239 | } 240 | } 241 | } 242 | 243 | export { 244 | MiniHtmlWebpackPlugin, 245 | defaultTemplate, 246 | generateAttributes, 247 | generateCSSReferences, 248 | generateJSReferences, 249 | }; 250 | -------------------------------------------------------------------------------- /test/__snapshots__/plugin.test.ts.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`additional body 1`] = ` 4 | " 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Demo
13 | 14 | " 15 | `; 16 | 17 | exports[`additional head 1`] = ` 18 | " 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | " 29 | `; 30 | 31 | exports[`custom async template 1`] = `"
Pizza
"`; 32 | 33 | exports[`custom attributes 1`] = ` 34 | " 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | " 45 | `; 46 | 47 | exports[`custom chunks 1`] = ` 48 | " 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | " 59 | `; 60 | 61 | exports[`custom chunks 2`] = ` 62 | " 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | " 73 | `; 74 | 75 | exports[`custom filename 1`] = ` 76 | " 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | " 87 | `; 88 | 89 | exports[`custom lang 1`] = ` 90 | " 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | " 101 | `; 102 | 103 | exports[`custom publicPath 1`] = ` 104 | " 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | " 115 | `; 116 | 117 | exports[`custom template 1`] = `"
Pizza
"`; 118 | 119 | exports[`custom title 1`] = ` 120 | " 121 | 122 | 123 | 124 | Pizza 125 | 126 | 127 | 128 | 129 | 130 | " 131 | `; 132 | 133 | exports[`default options 1`] = ` 134 | " 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | " 145 | `; 146 | 147 | exports[`mini-css-extract-plugin 1`] = ` 148 | " 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | " 159 | `; 160 | -------------------------------------------------------------------------------- /test/fixtures/another.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/styleguidist/mini-html-webpack-plugin/bfd82daceec6462699965f20d6d2e9c8d7889b7e/test/fixtures/another.js -------------------------------------------------------------------------------- /test/fixtures/css.js: -------------------------------------------------------------------------------- 1 | import "./main.css"; 2 | 3 | -------------------------------------------------------------------------------- /test/fixtures/index.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/styleguidist/mini-html-webpack-plugin/bfd82daceec6462699965f20d6d2e9c8d7889b7e/test/fixtures/index.js -------------------------------------------------------------------------------- /test/fixtures/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: green; 3 | } 4 | -------------------------------------------------------------------------------- /test/plugin.test.ts: -------------------------------------------------------------------------------- 1 | import webpack, { Configuration } from 'webpack'; 2 | import { createFsFromVolume, Volume } from 'memfs'; 3 | import MiniCssExtractPlugin from 'mini-css-extract-plugin'; 4 | import { MiniHtmlWebpackPlugin } from '../src'; 5 | 6 | function compile(config: Configuration, filenames = ['index.html']) { 7 | return new Promise((resolve, reject) => { 8 | const compiler = webpack(config); 9 | 10 | // @ts-ignore: There's a type mismatch but this should work based on webpack source 11 | compiler.outputFileSystem = createFsFromVolume(new Volume()); 12 | const memfs = compiler.outputFileSystem; 13 | 14 | compiler.run((err, stats) => { 15 | if (err) { 16 | return reject(err); 17 | } 18 | 19 | if (stats.hasErrors()) { 20 | return reject(stats.toString('errors-only')); 21 | } 22 | 23 | const ret = {}; 24 | 25 | filenames.forEach((filename) => { 26 | // @ts-ignore: The type is wrong here 27 | ret[filename] = memfs.readFileSync(`./dist/${filename}`, { 28 | encoding: 'utf-8', 29 | }); 30 | }); 31 | 32 | return resolve(ret); 33 | }); 34 | }); 35 | } 36 | 37 | const getConfig = ( 38 | options: {}, 39 | config: { title?: string } = {} 40 | ): Configuration => 41 | Object.assign( 42 | { 43 | entry: { main: './test/fixtures/index.js' }, 44 | plugins: [new MiniHtmlWebpackPlugin(options)], 45 | }, 46 | config 47 | ); 48 | 49 | test('default options', async () => { 50 | const result = await compile(getConfig({})); 51 | 52 | expect(result['index.html']).toMatchSnapshot(); 53 | }); 54 | 55 | test('mini-css-extract-plugin', async () => { 56 | const result = await compile({ 57 | mode: 'production', 58 | entry: { main: './test/fixtures/css.js' }, 59 | module: { 60 | rules: [ 61 | { 62 | test: /\.css$/, 63 | use: [MiniCssExtractPlugin.loader, 'css-loader'], 64 | }, 65 | ], 66 | }, 67 | plugins: [ 68 | new MiniHtmlWebpackPlugin({}), 69 | // @ts-ignore: MiniCssExtractPlugin types are broken 70 | new MiniCssExtractPlugin({ 71 | filename: '[name].css', 72 | }), 73 | ], 74 | }); 75 | 76 | expect(result['index.html']).toMatchSnapshot(); 77 | }); 78 | 79 | test('custom chunks', async () => { 80 | const result = await compile( 81 | { 82 | entry: { 83 | index: './test/fixtures/index.js', 84 | another: './test/fixtures/another.js', 85 | }, 86 | plugins: [ 87 | new MiniHtmlWebpackPlugin({ 88 | filename: 'index.html', 89 | chunks: ['index'], 90 | }), 91 | new MiniHtmlWebpackPlugin({ 92 | filename: 'another.html', 93 | chunks: ['another'], 94 | }), 95 | ], 96 | }, 97 | ['index.html', 'another.html'] 98 | ); 99 | 100 | // This should contain only reference to the index chunk and the related 101 | // runtime. 102 | expect(result['index.html']).toMatchSnapshot(); 103 | 104 | // This should contain only reference to the another chunk and the related 105 | // runtime. 106 | expect(result['another.html']).toMatchSnapshot(); 107 | }); 108 | 109 | test('custom title', async () => { 110 | const result = await compile( 111 | getConfig({ context: { title: 'Pizza' } }) 112 | ).then(); 113 | 114 | expect(result['index.html']).toMatchSnapshot(); 115 | }); 116 | 117 | test('custom lang', async () => { 118 | const result = await compile( 119 | getConfig({ context: { htmlAttributes: { lang: 'ru' } } }) 120 | ); 121 | 122 | expect(result['index.html']).toMatchSnapshot(); 123 | }); 124 | 125 | test('additional head', async () => { 126 | const result = await compile( 127 | getConfig({ 128 | context: { 129 | head: 130 | '', 131 | }, 132 | }) 133 | ); 134 | 135 | expect(result['index.html']).toMatchSnapshot(); 136 | }); 137 | 138 | test('additional body', async () => { 139 | const result = await compile( 140 | getConfig({ context: { body: '
Demo
' } }) 141 | ); 142 | 143 | expect(result['index.html']).toMatchSnapshot(); 144 | }); 145 | 146 | test('custom template', async () => { 147 | const result = await compile( 148 | getConfig({ 149 | context: { title: 'Pizza', htmlAttributes: { lang: 'it' } }, 150 | template: ({ title }: { title: string }) => `
${title}
`, 151 | }) 152 | ); 153 | 154 | expect(result['index.html']).toMatchSnapshot(); 155 | }); 156 | 157 | test('custom async template', async () => { 158 | const result = await compile( 159 | getConfig({ 160 | context: { title: 'Pizza' }, 161 | template: ({ title }: { title: string }) => { 162 | const delay = (ms: number) => 163 | new Promise((resolve) => setTimeout(resolve, ms)); 164 | return delay(50).then(() => `
${title}
`); 165 | }, 166 | }) 167 | ); 168 | 169 | expect(result['index.html']).toMatchSnapshot(); 170 | }); 171 | 172 | test('custom filename', async () => { 173 | const filename = 'pizza.html'; 174 | const result = await compile(getConfig({ filename }), [filename]); 175 | 176 | expect(result[filename]).toMatchSnapshot(); 177 | }); 178 | 179 | test('custom publicPath', async () => { 180 | const result = await compile(getConfig({ publicPath: 'pizza/' })); 181 | 182 | expect(result['index.html']).toMatchSnapshot(); 183 | }); 184 | 185 | test('custom attributes', async () => { 186 | const result = await compile( 187 | getConfig({ 188 | context: { 189 | cssAttributes: { 190 | rel: 'preload', 191 | as: 'style', 192 | }, 193 | jsAttributes: { 194 | defer: true, 195 | }, 196 | }, 197 | }) 198 | ); 199 | 200 | expect(result['index.html']).toMatchSnapshot(); 201 | }); 202 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": ["src"], 3 | "compilerOptions": { 4 | "declarationDir": "./dist", 5 | "outDir": "./dist", 6 | "rootDir": "./src", 7 | "target": "es5", 8 | "module": "commonjs", 9 | "lib": ["dom", "esnext"], 10 | "importHelpers": false, 11 | "declaration": true, 12 | "sourceMap": false, 13 | "strict": true, 14 | "noUnusedLocals": true, 15 | "noUnusedParameters": true, 16 | "noImplicitReturns": true, 17 | "noFallthroughCasesInSwitch": true, 18 | "moduleResolution": "node", 19 | "baseUrl": "./", 20 | "paths": { 21 | "*": ["src/*", "node_modules/*"] 22 | }, 23 | "esModuleInterop": true 24 | } 25 | } 26 | --------------------------------------------------------------------------------