Skip to content

Commit a015679

Browse files
committed
feat: support for webpack 5
1 parent e86a571 commit a015679

File tree

9 files changed

+55
-26
lines changed

9 files changed

+55
-26
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<!-- Please fill in below sections, include details as much as possible -->
22
<!-- Leave "N/A" to any non-applicable sections instead of leaving them blank -->
33

4+
## Environment and package version
5+
<!---- Include the webpack version and package version ---->
6+
<!---- e.g. Webpack 5.0.0, HTML webpack plugin 5.0.0 , HTML inline script plugin 2.0.0 ---->
7+
48
## Reproduction link / code sample
59
<!---- Include a minimal code sample to demonstrate the issue if possible ---->
610

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1-
# HTML Inline Script Webpack Plugin for webpack 4 (html-inline-script-webpack-plugin)
1+
# HTML Inline Script Webpack Plugin for webpack (html-inline-script-webpack-plugin)
22

33
[![Latest version](https://img.shields.io/github/v/release/icelam/html-inline-script-webpack-plugin.svg?sort=semver&label=latest)](https://github.com/icelam/html-inline-script-webpack-plugin/releases)
44
[![Download count](https://img.shields.io/npm/dm/html-inline-script-webpack-plugin)](https://www.npmjs.com/package/html-inline-script-webpack-plugin)
55
[![Install size](https://packagephobia.com/badge?p=html-inline-script-webpack-plugin)](https://packagephobia.com/result?p=html-inline-script-webpack-plugin)
66
![ci](https://github.com/icelam/html-inline-script-webpack-plugin/workflows/ci/badge.svg)
77
[![Package quality](https://npm.packagequality.com/shield/html-inline-script-webpack-plugin.svg)](https://packagequality.com/#?package=html-inline-script-webpack-plugin)
8+
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/icelam/html-inline-script-webpack-plugin.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/icelam/html-inline-script-webpack-plugin/context:javascript)
9+
10+
[![NPM](https://nodei.co/npm/html-inline-script-webpack-plugin.png?compact=true)](https://npmjs.org/package/html-inline-script-webpack-plugin)
811

912
A webpack plugin for converting external script files `<script src="app.js"></script>` to inline script block `<script>...</script>`. Requires [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) to work.
1013

1114
Inspired by [react-dev-utils](https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/InlineChunkHtmlPlugin.js) created by [Facebook](https://github.com/facebook/).
1215

1316
## Install
14-
### NPM
17+
18+
### Webpack5
19+
20+
#### NPM
1521
```bash
1622
npm i html-inline-script-webpack-plugin -D
1723
```
18-
### Yarn
24+
#### Yarn
1925
```bash
2026
yarn add html-inline-script-webpack-plugin -D
2127
```
2228

29+
### Webpack4
30+
31+
#### NPM
32+
```bash
33+
npm i html-inline-script-webpack-plugin@^1 -D
34+
```
35+
#### Yarn
36+
```bash
37+
yarn add html-inline-script-webpack-plugin@^1 -D
38+
```
39+
2340
## Usage
2441
By default, the plugin will convert all the external script files to inline script block.
2542
```js
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title></head><body><p>This is minimal code to demonstrate webpack usage</p><script>!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){console.log("Hello world")}]);</script><script>!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([,function(e,t){console.log("Page 2")}]);</script></body></html>
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title><script defer="defer">console.log("Hello world");</script><script defer="defer">console.log("Page 2");</script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title></head><body><p>This is minimal code to demonstrate webpack usage</p><script>!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){console.log("Hello world")}]);</script><script>!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([,function(e,t){console.log("Page 2")}]);</script></body></html>
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title><script defer="defer">console.log("Hello world");</script><script defer="defer">console.log("Page 2");</script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title></head><body><p>This is minimal code to demonstrate webpack usage</p><script>!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){console.log("Hello world")}]);</script></body></html>
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title><script defer="defer">console.log("Hello world");</script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
"keywords": [
2323
"webpack",
2424
"webpack4",
25+
"webpack5",
2526
"webpack-plugin",
2627
"html-webpack-plugin",
2728
"inline",
2829
"internal",
2930
"embedded",
31+
"source",
3032
"inline-source",
3133
"script",
3234
"inline-script"
@@ -36,7 +38,6 @@
3638
"@commitlint/config-conventional": "^12.0.0",
3739
"@types/html-webpack-plugin": "^3.2.3",
3840
"@types/jest": "^26.0.20",
39-
"@types/webpack": "^4.41.26",
4041
"@types/webpack-sources": "^2.1.0",
4142
"@typescript-eslint/eslint-plugin": "^4.0.0",
4243
"@typescript-eslint/parser": "^4.15.2",
@@ -54,7 +55,7 @@
5455
"typescript": "^4.1.3"
5556
},
5657
"peerDependencies": {
57-
"html-webpack-plugin": "^4.0.0",
58-
"webpack": "^4.0.0"
58+
"html-webpack-plugin": "^5.0.0",
59+
"webpack": "^5.0.0"
5960
}
6061
}

src/HtmlInlineScriptPlugin.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { Compiler, Plugin } from 'webpack';
2-
import type { RawSource } from 'webpack-sources';
1+
import { Compilation } from 'webpack';
2+
import type { Compiler, WebpackPluginInstance } from 'webpack';
33
import htmlWebpackPlugin from 'html-webpack-plugin';
44
import type { HtmlTagObject } from 'html-webpack-plugin';
55
import { PLUGIN_PREFIX } from './constants';
66

7-
class HtmlInlineScriptPlugin implements Plugin {
7+
class HtmlInlineScriptPlugin implements WebpackPluginInstance {
88
tests: RegExp[];
99

1010
constructor(tests?: RegExp[]) {
@@ -19,7 +19,7 @@ class HtmlInlineScriptPlugin implements Plugin {
1919

2020
processScriptTag(
2121
publicPath: string,
22-
assets: { [key: string]: RawSource },
22+
assets: Compilation['assets'],
2323
tag: HtmlTagObject
2424
): HtmlTagObject {
2525
if (tag.tagName !== 'script' || !tag.attributes?.src) {
@@ -43,14 +43,15 @@ class HtmlInlineScriptPlugin implements Plugin {
4343

4444
return {
4545
tagName: 'script',
46-
innerHTML: asset.source(),
46+
innerHTML: asset.source() as string,
4747
voidTag: false,
48-
attributes: attributesWithoutSrc
48+
attributes: attributesWithoutSrc,
49+
meta: { plugin: 'html-inline-script-webpack-plugin' }
4950
};
5051
}
5152

5253
apply(compiler: Compiler): void {
53-
let publicPath = compiler.options?.output?.publicPath || '';
54+
let publicPath = compiler.options?.output?.publicPath as string || '';
5455

5556
if (publicPath && !publicPath.endsWith('/')) {
5657
publicPath += '/';
@@ -65,13 +66,16 @@ class HtmlInlineScriptPlugin implements Plugin {
6566
);
6667
return data;
6768
});
68-
});
6969

70-
compiler.hooks.emit.tap(`${PLUGIN_PREFIX}_emit`, (compilation) => {
71-
Object.keys(compilation.assets).forEach((assetName) => {
72-
if (this.isFileNeedsToBeInlined(assetName)) {
73-
delete compilation.assets[assetName];
74-
}
70+
compilation.hooks.processAssets.tap({
71+
name: `${PLUGIN_PREFIX}_PROCESS_ASSETS_STAGE_SUMMARIZE`,
72+
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
73+
}, (assets) => {
74+
Object.keys(assets).forEach((assetName) => {
75+
if (this.isFileNeedsToBeInlined(assetName)) {
76+
delete assets[assetName];
77+
}
78+
});
7579
});
7680
});
7781
}

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import { version as webpackVersion } from 'webpack';
12
import htmlWebpackPlugin from 'html-webpack-plugin';
23
import HtmlInlineScriptPlugin from './HtmlInlineScriptPlugin';
34

4-
const isHtmlWebpackPluginV4 = 'getHooks' in htmlWebpackPlugin;
5+
const isHtmlWebpackPluginV3 = !('getHooks' in htmlWebpackPlugin);
6+
const isHtmlWebpackPluginV4 = webpackVersion.startsWith('4.');
7+
const isUnsupportedVersion = isHtmlWebpackPluginV3 || isHtmlWebpackPluginV4;
58

6-
if (!isHtmlWebpackPluginV4) {
9+
if (isUnsupportedVersion) {
710
// eslint-disable-next-line no-console
811
console.error(
912
'\x1b[35m%s \x1b[31m%s\x1b[0m',
1013
'[html-inline-script-webpack-plugin]',
11-
'Please upgrade your webpack to version 4 to use this plugin.'
14+
'Version 2.x only supports webpack 5. If you are using webpack 4, please downgrade this plugin to version 1.x instead.'
1215
);
1316

1417
throw new Error('VERSION_INCOMPATIBLE');

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@
850850
"@types/source-list-map" "*"
851851
source-map "^0.7.3"
852852

853-
"@types/webpack@*", "@types/webpack@^4.41.26":
853+
"@types/webpack@*":
854854
version "4.41.26"
855855
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.26.tgz#27a30d7d531e16489f9c7607c747be6bc1a459ef"
856856
integrity sha512-7ZyTfxjCRwexh+EJFwRUM+CDB2XvgHl4vfuqf1ZKrgGvcS5BrNvPQqJh3tsZ0P6h6Aa1qClVHaJZszLPzpqHeA==

0 commit comments

Comments
 (0)