Skip to content

Commit 7c7bc30

Browse files
committed
Merge branch 'develop'
2 parents 28564b0 + e506e33 commit 7c7bc30

File tree

21 files changed

+3426
-407
lines changed

21 files changed

+3426
-407
lines changed

.eslintrc

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,40 @@
2323
"rules": {
2424
"no-param-reassign": "off",
2525
"comma-dangle": ["error", "never"],
26-
"import/no-extraneous-dependencies": [
27-
"error",
28-
{
29-
"optionalDependencies": false
30-
}
31-
],
32-
"import/extensions": [
33-
"error",
34-
{
35-
"js": "never",
36-
"ts": "never",
37-
}
38-
]
26+
"import/no-extraneous-dependencies": ["error", {
27+
"optionalDependencies": false
28+
}],
29+
"import/extensions": ["error", {
30+
"js": "never",
31+
"ts": "never",
32+
}]
3933
},
4034
"overrides": [
4135
{
42-
"files": ["scripts/*.js", "test/**/*.js"],
36+
"plugins": ["jest"],
37+
"files": ["__tests__/**/*.test.js", "__tests__/**/*.test.ts"],
38+
"settings": {
39+
"import/resolver": {
40+
"node": {
41+
"extensions": [".js", ".json", ".ts"]
42+
}
43+
}
44+
},
45+
"extends": [
46+
"eslint:recommended",
47+
"airbnb-base",
48+
"plugin:jest/recommended",
49+
"plugin:@typescript-eslint/eslint-recommended",
50+
"plugin:@typescript-eslint/recommended",
51+
],
4352
"env": { "node": true },
4453
"rules": {
4554
"@typescript-eslint/no-var-requires": ["off"],
46-
"no-console": ["off"]
55+
"no-console": ["off"],
56+
"import/extensions": ["error", {
57+
"js": "never",
58+
"ts": "never",
59+
}]
4760
}
4861
}
4962
]

.github/workflows/ci.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
3535
with:
3636
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
37-
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
37+
key: ${{ runner.os }}-node${{ matrix.node-version }}-yarn-${{ hashFiles('**/yarn.lock') }}
3838
restore-keys: |
3939
${{ runner.os }}-yarn-
4040
- name: Install webpack-cli
@@ -43,12 +43,9 @@ jobs:
4343
- name: Install dependencies
4444
run: |
4545
yarn install --frozen-lockfile
46-
- name: List webpack version
47-
run: |
48-
yarn list --depth=0
4946
- name: Build package
5047
run: |
5148
yarn build
5249
- name: Test
5350
run: |
54-
webpack --config test/cases/v4/simple/webpack.config.js
51+
yarn test:ci

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ yarn-debug.log*
55
yarn-error.log*
66

77
/dist
8-
/test/**/*/dist
8+
/__tests__/**/*/dist

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ yarn-error.log
1212
yarn.lock
1313
.github
1414
lgtm.yml
15-
/test
15+
/__tests__
1616
.husky
1717
/scripts
18+
jest.config.js
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import webpack from 'webpack';
4+
import simpleConfig from './cases/simple/webpack.config';
5+
import multipleInstanceConfig from './cases/multiple-instance/webpack.config';
6+
7+
describe('HtmlInlineScriptPlugin', () => {
8+
it('should build simple webpack config without error', async () => {
9+
const webpackPromise = new Promise((resolve) => {
10+
const compiler = webpack(simpleConfig);
11+
12+
compiler.run((error, stats) => {
13+
expect(error).toBeNull();
14+
15+
const statsErrors = stats?.compilation.errors;
16+
expect(statsErrors?.length).toBe(0);
17+
18+
const result = fs.readFileSync(
19+
path.join(__dirname, 'cases/simple/dist/index.html'),
20+
'utf8',
21+
);
22+
23+
const expected = fs.readFileSync(
24+
path.join(__dirname, 'cases/simple/expected/index.html'),
25+
'utf8',
26+
);
27+
expect(result).toBe(expected);
28+
resolve(true);
29+
});
30+
});
31+
32+
await webpackPromise;
33+
});
34+
35+
it('should build webpack config having multiple HTML webpack plugin instance without error', async () => {
36+
const webpackPromise = new Promise((resolve) => {
37+
const compiler = webpack(multipleInstanceConfig);
38+
39+
compiler.run((error, stats) => {
40+
expect(error).toBeNull();
41+
42+
const statsErrors = stats?.compilation.errors;
43+
expect(statsErrors?.length).toBe(0);
44+
45+
const result1 = fs.readFileSync(
46+
path.join(__dirname, 'cases/multiple-instance/dist/index.html'),
47+
'utf8',
48+
);
49+
50+
const expected1 = fs.readFileSync(
51+
path.join(__dirname, 'cases/multiple-instance/expected/index.html'),
52+
'utf8',
53+
);
54+
55+
expect(result1).toBe(expected1);
56+
57+
const result2 = fs.readFileSync(
58+
path.join(__dirname, 'cases/multiple-instance/dist/page2.html'),
59+
'utf8',
60+
);
61+
62+
const expected2 = fs.readFileSync(
63+
path.join(__dirname, 'cases/multiple-instance/expected/page2.html'),
64+
'utf8',
65+
);
66+
67+
expect(result2).toBe(expected2);
68+
resolve(true);
69+
});
70+
});
71+
72+
await webpackPromise;
73+
});
74+
});
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></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>
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></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>
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
]
27+
};
28+
29+
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></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>
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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: path.join(__dirname, './fixtures/index.js'),
9+
output: {
10+
path: path.join(__dirname, './dist'),
11+
filename: '[name].js'
12+
},
13+
plugins: [
14+
new HtmlWebpackPlugin({
15+
template: path.resolve(__dirname, './fixtures/index.html')
16+
}),
17+
new Self()
18+
]
19+
};
20+
21+
export default config;

jest.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
collectCoverageFrom: ['src/**/*.[tj]s'],
3+
testMatch: ['<rootDir>__tests__/**/*.test.[jt]s'],
4+
moduleFileExtensions: [
5+
'ts',
6+
'js',
7+
'json'
8+
],
9+
transform: {
10+
'^.+\\.ts$': 'ts-jest'
11+
},
12+
globals: {
13+
'ts-jest': {
14+
tsconfig: '<rootDir>/tsconfig.json'
15+
}
16+
},
17+
testEnvironment: 'node'
18+
};

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"prepare": "husky install && install-peers",
1010
"first-release": "npx standard-version --commit-all --tag-prefix v --first-release",
1111
"release": "npx standard-version --commit-all --tag-prefix v",
12-
"extract-latest-change-log": "node scripts/extractLatestChangeLog.js"
12+
"extract-latest-change-log": "node scripts/extractLatestChangeLog.js",
13+
"test": "jest --verbose --watchAll --config ./jest.config.js",
14+
"test:ci": "yarn test --ci --watchAll=false --runInBand --detectOpenHandles"
1315
},
1416
"author": "Ice Lam",
1517
"repository": {
@@ -33,18 +35,22 @@
3335
"@commitlint/cli": "^12.0.0",
3436
"@commitlint/config-conventional": "^12.0.0",
3537
"@types/html-webpack-plugin": "^3.2.3",
38+
"@types/jest": "^26.0.20",
3639
"@types/webpack": "^4.41.26",
37-
"@types/webpack-sources": "^1.4.0",
40+
"@types/webpack-sources": "^2.1.0",
3841
"@typescript-eslint/eslint-plugin": "^4.0.0",
3942
"@typescript-eslint/parser": "^4.15.2",
4043
"eslint": "^7.4.0",
4144
"eslint-config-airbnb-base": "^14.2.0",
4245
"eslint-plugin-import": "^2.22.0",
46+
"eslint-plugin-jest": "^24.1.5",
4347
"husky": "^5.1.2",
4448
"install-peers-cli": "^2.2.0",
49+
"jest": "^26.6.3",
4550
"lint-staged": "^10.2.11",
4651
"pinst": "^2.1.6",
4752
"prettier": "^2.0.5",
53+
"ts-jest": "^26.5.3",
4854
"typescript": "^4.1.3"
4955
},
5056
"peerDependencies": {

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import HtmlInlineScriptPlugin from './HtmlInlineScriptPlugin';
33

44
const isHtmlWebpackPluginV4 = 'getHooks' in htmlWebpackPlugin;
55

6-
module.exports = isHtmlWebpackPluginV4
7-
? HtmlInlineScriptPlugin
8-
: null;
6+
if (!isHtmlWebpackPluginV4) {
7+
// eslint-disable-next-line no-console
8+
console.error(
9+
'\x1b[35m%s \x1b[31m%s\x1b[0m',
10+
'[html-inline-script-webpack-plugin]',
11+
'Please upgrade your webpack to version 4 to use this plugin.'
12+
);
13+
14+
throw new Error('VERSION_INCOMPATIBLE');
15+
}
16+
17+
export = HtmlInlineScriptPlugin;

test/cases/v4/simple/webpack.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)