Skip to content

Commit d8f9d53

Browse files
committed
Merge branch 'develop'
2 parents 4299513 + 139b2d6 commit d8f9d53

34 files changed

+854
-435
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,3 @@ updates:
1818
reviewers:
1919
- "icelam"
2020
open-pull-requests-limit: 10
21-
22-
# checking for v1 branch which contains v1 code of this plugin
23-
- package-ecosystem: "npm"
24-
directory: "/"
25-
schedule:
26-
interval: "monthly"
27-
target-branch: "v1"
28-
labels:
29-
- "dependencies"
30-
commit-message:
31-
prefix: "chore"
32-
prefix-development: "chore"
33-
include: "scope"
34-
ignore:
35-
- dependency-name: "webpack"
36-
- dependency-name: "html-webpack-plugin"
37-
reviewers:
38-
- "icelam"
39-
open-pull-requests-limit: 10

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12.16.1
1+
12.22.0

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"endOfLine": "lf",
3+
"trailingComma": "es5",
4+
"tabWidth": 2,
5+
"semi": true,
6+
"singleQuote": true
7+
}

README.md

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,114 @@ Inspired by [react-dev-utils](https://github.com/facebook/create-react-app/blob/
1818
### Webpack5
1919

2020
#### NPM
21+
2122
```bash
2223
npm i html-inline-script-webpack-plugin -D
2324
```
25+
2426
#### Yarn
27+
2528
```bash
2629
yarn add html-inline-script-webpack-plugin -D
2730
```
2831

2932
### Webpack4
3033

3134
#### NPM
35+
3236
```bash
3337
npm i html-inline-script-webpack-plugin@^1 -D
3438
```
39+
3540
#### Yarn
41+
3642
```bash
3743
yarn add html-inline-script-webpack-plugin@^1 -D
3844
```
3945

4046
## Usage
47+
4148
By default, the plugin will convert all the external script files to inline script block.
49+
50+
```js
51+
const HtmlWebpackPlugin = require('html-webpack-plugin');
52+
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
53+
54+
module.exports = {
55+
plugins: [new HtmlWebpackPlugin(), new HtmlInlineScriptPlugin()],
56+
};
57+
```
58+
59+
## Options
60+
61+
Below are lists of options supported by this plugin:
62+
63+
| Name | Description | Type |
64+
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
65+
| scriptMatchPattern | List of script files that should be processed and inject as inline script. This will be filtered using the output file name. | RegExp[] |
66+
| htmlMatchPattern | List of HTML template files that should be processed by this plugin. Useful when you have multiple `html-webpack-plugin` initialized. This will be filtered using the [`options?.filename`](https://github.com/jantimon/html-webpack-plugin#options) provided by `html-webpack-plugin`. | RegExp[] |
67+
68+
Here are some examples illustrating how to use these options:
69+
70+
##### Process only script files that have file name start with `runtime~` and `app~`
71+
4272
```js
4373
const HtmlWebpackPlugin = require('html-webpack-plugin');
4474
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
4575

4676
module.exports = {
4777
plugins: [
4878
new HtmlWebpackPlugin(),
49-
new HtmlInlineScriptPlugin(),
50-
]
51-
}
79+
new HtmlInlineScriptPlugin({
80+
scriptMatchPattern: [/runtime~.+[.]js$/, /app~.+[.]js$/],
81+
}),
82+
],
83+
};
5284
```
5385

54-
To limit the scope of the plugin, specify lists of files you wish to convert in regular expressions:
86+
##### Process any script files but only have them inlined in `index.html`
87+
5588
```js
5689
const HtmlWebpackPlugin = require('html-webpack-plugin');
5790
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
5891

5992
module.exports = {
6093
plugins: [
61-
new HtmlWebpackPlugin(),
62-
new HtmlInlineScriptPlugin([
63-
/runtime~.+[.]js$/,
64-
/app~.+[.]js$/
65-
]),
66-
]
67-
}
94+
new HtmlWebpackPlugin({
95+
filename: 'index.html',
96+
template: 'static/index.webos.html',
97+
}),
98+
new HtmlWebpackPlugin({
99+
filename: 'page2.html',
100+
template: 'page2.html',
101+
}),
102+
new HtmlInlineScriptPlugin({
103+
htmlMatchPattern: [/index.html$/],
104+
}),
105+
],
106+
};
107+
```
108+
109+
##### Process script files that have file name start with `runtime~` and `app~` and inject only to `index.html`
110+
111+
```js
112+
const HtmlWebpackPlugin = require('html-webpack-plugin');
113+
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
114+
115+
module.exports = {
116+
plugins: [
117+
new HtmlWebpackPlugin({
118+
filename: 'index.html',
119+
template: 'static/index.webos.html',
120+
}),
121+
new HtmlWebpackPlugin({
122+
filename: 'page2.html',
123+
template: 'page2.html',
124+
}),
125+
new HtmlInlineScriptPlugin({
126+
scriptMatchPattern: [/runtime~.+[.]js$/, /app~.+[.]js$/],
127+
htmlMatchPattern: [/index.html$/],
128+
}),
129+
],
130+
};
68131
```

__tests__/HtmlInlineScriptPlugin.test.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import fs from 'fs';
22
import path from 'path';
33
import webpack from 'webpack';
4+
5+
import Self from '../dist';
6+
47
import simpleConfig from './cases/simple/webpack.config';
58
import multipleInstanceConfig from './cases/multiple-instance/webpack.config';
69
import jsWithImportConfig from './cases/js-with-import/webpack.config';
710
import webWorkerConfig from './cases/web-worker/webpack.config';
811
import inlineWebWorkerConfig from './cases/inline-web-worker/webpack.config';
12+
import ignoreScriptsConfig from './cases/ignore-scripts/webpack.config';
13+
import ignoreHtmlsConfig from './cases/ignore-htmls/webpack.config';
14+
import ignoreScriptsAndHtmlsConfig from './cases/ignore-scripts-and-htmls/webpack.config';
915

1016
describe('HtmlInlineScriptPlugin', () => {
1117
it('should build simple webpack config without error', async () => {
@@ -195,4 +201,135 @@ describe('HtmlInlineScriptPlugin', () => {
195201

196202
await webpackPromise;
197203
});
204+
205+
it('should respect plugin options on script matching pattern', async () => {
206+
const webpackPromise = new Promise((resolve) => {
207+
const compiler = webpack(ignoreScriptsConfig);
208+
209+
compiler.run((error, stats) => {
210+
expect(error).toBeNull();
211+
212+
const statsErrors = stats?.compilation.errors;
213+
expect(statsErrors?.length).toBe(0);
214+
215+
const result1 = fs.readFileSync(
216+
path.join(__dirname, 'cases/ignore-scripts/dist/index.html'),
217+
'utf8',
218+
);
219+
220+
const expected1 = fs.readFileSync(
221+
path.join(__dirname, 'cases/ignore-scripts/expected/index.html'),
222+
'utf8',
223+
);
224+
225+
expect(result1).toBe(expected1);
226+
227+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts/expected/'));
228+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts/dist/'));
229+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
230+
231+
resolve(true);
232+
});
233+
});
234+
235+
await webpackPromise;
236+
});
237+
238+
it('should respect plugin options on html template matching pattern', async () => {
239+
const webpackPromise = new Promise((resolve) => {
240+
const compiler = webpack(ignoreHtmlsConfig);
241+
242+
compiler.run((error, stats) => {
243+
expect(error).toBeNull();
244+
245+
const statsErrors = stats?.compilation.errors;
246+
expect(statsErrors?.length).toBe(0);
247+
248+
const result1 = fs.readFileSync(
249+
path.join(__dirname, 'cases/ignore-htmls/dist/index.html'),
250+
'utf8',
251+
);
252+
253+
const expected1 = fs.readFileSync(
254+
path.join(__dirname, 'cases/ignore-htmls/expected/index.html'),
255+
'utf8',
256+
);
257+
258+
expect(result1).toBe(expected1);
259+
260+
const result2 = fs.readFileSync(
261+
path.join(__dirname, 'cases/ignore-htmls/dist/page2.html'),
262+
'utf8',
263+
);
264+
265+
const expected2 = fs.readFileSync(
266+
path.join(__dirname, 'cases/ignore-htmls/expected/page2.html'),
267+
'utf8',
268+
);
269+
270+
expect(result2).toBe(expected2);
271+
272+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-htmls/expected/'));
273+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-htmls/dist/'));
274+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
275+
276+
resolve(true);
277+
});
278+
});
279+
280+
await webpackPromise;
281+
});
282+
283+
it('should respect plugin options on both script matching pattern and html template matching pattern', async () => {
284+
const webpackPromise = new Promise((resolve) => {
285+
const compiler = webpack(ignoreScriptsAndHtmlsConfig);
286+
287+
compiler.run((error, stats) => {
288+
expect(error).toBeNull();
289+
290+
const statsErrors = stats?.compilation.errors;
291+
expect(statsErrors?.length).toBe(0);
292+
293+
const result1 = fs.readFileSync(
294+
path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/index.html'),
295+
'utf8',
296+
);
297+
298+
const expected1 = fs.readFileSync(
299+
path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/index.html'),
300+
'utf8',
301+
);
302+
303+
expect(result1).toBe(expected1);
304+
305+
const result2 = fs.readFileSync(
306+
path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/page2.html'),
307+
'utf8',
308+
);
309+
310+
const expected2 = fs.readFileSync(
311+
path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/page2.html'),
312+
'utf8',
313+
);
314+
315+
expect(result2).toBe(expected2);
316+
317+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/'));
318+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/'));
319+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
320+
321+
resolve(true);
322+
});
323+
});
324+
325+
await webpackPromise;
326+
});
327+
328+
it('should build throw error if options passed to plugin is old options format', async () => {
329+
const initializedPlugin = () => {
330+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-new
331+
new Self([/runtime~.+[.]js$/, /app~.+[.]js$/] as any);
332+
};
333+
expect(initializedPlugin).toThrow();
334+
});
198335
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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><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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello world");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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><script defer="defer" src="index.js"></script><script defer="defer" src="page2.js"></script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Page 2");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+
<meta name="language" content="English" />
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
8+
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no" />
9+
<title>webpack test</title>
10+
</head>
11+
<body>
12+
<p>This is minimal code to demonstrate webpack usage</p>
13+
</body>
14+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log('Hello world');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+
<meta name="language" content="English" />
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
8+
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no" />
9+
<title>webpack test</title>
10+
</head>
11+
<body>
12+
<p>This is minimal code to demonstrate webpack usage</p>
13+
</body>
14+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log('Page 2');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from 'path';
2+
import type { Configuration } from 'webpack';
3+
import HtmlWebpackPlugin from 'html-webpack-plugin';
4+
import Self from '../../../dist';
5+
6+
const config: Configuration = {
7+
mode: 'production',
8+
entry: {
9+
index: path.join(__dirname, './fixtures/index.js'),
10+
page2: path.join(__dirname, './fixtures/page2.js')
11+
},
12+
output: {
13+
path: path.join(__dirname, './dist'),
14+
filename: '[name].js'
15+
},
16+
plugins: [
17+
new HtmlWebpackPlugin({
18+
template: path.resolve(__dirname, './fixtures/index.html'),
19+
filename: 'index.html'
20+
}),
21+
new HtmlWebpackPlugin({
22+
template: path.resolve(__dirname, './fixtures/page2.html'),
23+
filename: 'page2.html'
24+
}),
25+
new Self({
26+
htmlMatchPattern: [/index.html$/]
27+
})
28+
]
29+
};
30+
31+
export default config;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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><script defer="defer">console.log("Hello world");</script><script defer="defer" src="page2.js"></script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello world");

0 commit comments

Comments
 (0)