Skip to content

Commit 4e40c19

Browse files
committed
fix: delete only script files which has been processed by plugin
1 parent 0b45e37 commit 4e40c19

File tree

21 files changed

+339
-5
lines changed

21 files changed

+339
-5
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
README.md
2+
__tests__/cases/**/dist
3+
__tests__/cases/**/expected
4+
dist

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"plugins": [
44
"@typescript-eslint"
55
],
6+
"env": {
7+
"browser": true,
8+
"node": true
9+
},
610
"extends": [
711
"eslint:recommended",
812
"airbnb-base",

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-18.04
1717
strategy:
1818
matrix:
19-
node-version: [12.x, 14.x]
19+
node-version: [12.x, 14.x, 16.x]
2020
env:
2121
HUSKY: 0
2222
steps:

__tests__/HtmlInlineScriptPlugin.test.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import path from 'path';
33
import webpack from 'webpack';
44
import simpleConfig from './cases/simple/webpack.config';
55
import multipleInstanceConfig from './cases/multiple-instance/webpack.config';
6+
import jsWithImportConfig from './cases/js-with-import/webpack.config';
7+
import webWorkerConfig from './cases/web-worker/webpack.config';
8+
import inlineWebWorkerConfig from './cases/inline-web-worker/webpack.config';
69

710
describe('HtmlInlineScriptPlugin', () => {
811
it('should build simple webpack config without error', async () => {
@@ -25,6 +28,11 @@ describe('HtmlInlineScriptPlugin', () => {
2528
'utf8',
2629
);
2730
expect(result).toBe(expected);
31+
32+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/simple/expected/'));
33+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/simple/dist/'));
34+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
35+
2836
resolve(true);
2937
});
3038
});
@@ -65,6 +73,122 @@ describe('HtmlInlineScriptPlugin', () => {
6573
);
6674

6775
expect(result2).toBe(expected2);
76+
77+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/multiple-instance/expected/'));
78+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/multiple-instance/dist/'));
79+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
80+
81+
resolve(true);
82+
});
83+
});
84+
85+
await webpackPromise;
86+
});
87+
88+
it('should build webpack config having JS file with import without error', async () => {
89+
const webpackPromise = new Promise((resolve) => {
90+
const compiler = webpack(jsWithImportConfig);
91+
92+
compiler.run((error, stats) => {
93+
expect(error).toBeNull();
94+
95+
const statsErrors = stats?.compilation.errors;
96+
expect(statsErrors?.length).toBe(0);
97+
98+
const result1 = fs.readFileSync(
99+
path.join(__dirname, 'cases/js-with-import/dist/index.html'),
100+
'utf8',
101+
);
102+
103+
const expected1 = fs.readFileSync(
104+
path.join(__dirname, 'cases/js-with-import/expected/index.html'),
105+
'utf8',
106+
);
107+
108+
expect(result1).toBe(expected1);
109+
110+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/js-with-import/expected/'));
111+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/js-with-import/dist/'));
112+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
113+
114+
resolve(true);
115+
});
116+
});
117+
118+
await webpackPromise;
119+
});
120+
121+
it('should build webpack config having web worker without error', async () => {
122+
const webpackPromise = new Promise((resolve) => {
123+
const compiler = webpack(webWorkerConfig);
124+
125+
compiler.run((error, stats) => {
126+
expect(error).toBeNull();
127+
128+
const statsErrors = stats?.compilation.errors;
129+
expect(statsErrors?.length).toBe(0);
130+
131+
const result1 = fs.readFileSync(
132+
path.join(__dirname, 'cases/web-worker/dist/index.html'),
133+
'utf8',
134+
);
135+
136+
const expected1 = fs.readFileSync(
137+
path.join(__dirname, 'cases/web-worker/expected/index.html'),
138+
'utf8',
139+
);
140+
141+
expect(result1).toBe(expected1);
142+
143+
const result2 = fs.readFileSync(
144+
path.join(__dirname, 'cases/web-worker/dist/test.worker.js'),
145+
'utf8',
146+
);
147+
148+
const expected2 = fs.readFileSync(
149+
path.join(__dirname, 'cases/web-worker/expected/test.worker.js'),
150+
'utf8',
151+
);
152+
153+
expect(result2).toBe(expected2);
154+
155+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/web-worker/expected/'));
156+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/web-worker/dist/'));
157+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
158+
159+
resolve(true);
160+
});
161+
});
162+
163+
await webpackPromise;
164+
});
165+
166+
it('should build webpack config having inline web worker without error', async () => {
167+
const webpackPromise = new Promise((resolve) => {
168+
const compiler = webpack(inlineWebWorkerConfig);
169+
170+
compiler.run((error, stats) => {
171+
expect(error).toBeNull();
172+
173+
const statsErrors = stats?.compilation.errors;
174+
expect(statsErrors?.length).toBe(0);
175+
176+
const result1 = fs.readFileSync(
177+
path.join(__dirname, 'cases/inline-web-worker/dist/index.html'),
178+
'utf8',
179+
);
180+
181+
const expected1 = fs.readFileSync(
182+
path.join(__dirname, 'cases/inline-web-worker/expected/index.html'),
183+
'utf8',
184+
);
185+
186+
expect(result1).toBe(expected1);
187+
188+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/inline-web-worker/expected/'));
189+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/inline-web-worker/dist/'));
190+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
191+
68192
resolve(true);
69193
});
70194
});
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">(()=>{"use strict";var e={433:e=>{e.exports="onmessage = function (event) {\n const workerResult = { timestamp: Date.now(), ...event.data };\n\n workerResult.onmessage = true;\n\n postMessage(workerResult);\n};\n"}},t={};function n(s){var o=t[s];if(void 0!==o)return o.exports;var r=t[s]={exports:{}};return e[s](r,r.exports,n),r.exports}(()=>{var e=n(433);const t=new Blob([e]),s=window.URL.createObjectURL(t),o=new Worker(s);let r;o.onmessage=function(e){r||(r=document.createElement("div"),r.setAttribute("id","result"),document.body.append(r));const t=document.createElement("pre");t.innerHTML=JSON.stringify(e.data),r.append(t)},window.addEventListener("load",(()=>{document.getElementById("button").addEventListener("click",(()=>{o.postMessage({postMessage:!0})}))}))})()})();</script></head><body><p>This is minimal code to demonstrate webpack usage</p><button id="button">Run Action</button></body></html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<button id="button">Run Action</button>
14+
</body>
15+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This file will be loaded as raw text as configured via webpack and asssets-loader
2+
// eslint-disable-next-line import/extensions, import/no-unresolved
3+
import workerSource from './worker.js?raw';
4+
5+
const blob = new Blob([
6+
workerSource
7+
]);
8+
9+
// Obtain a blob URL reference to our worker 'file'.
10+
const blobURL = window.URL.createObjectURL(blob);
11+
12+
const worker = new Worker(blobURL);
13+
14+
let result;
15+
16+
worker.onmessage = function (event) {
17+
if (!result) {
18+
result = document.createElement('div');
19+
result.setAttribute('id', 'result');
20+
21+
document.body.append(result);
22+
}
23+
24+
const record = document.createElement('pre');
25+
record.innerHTML = JSON.stringify(event.data);
26+
27+
result.append(record);
28+
};
29+
30+
window.addEventListener('load', () => {
31+
const button = document.getElementById('button');
32+
33+
button.addEventListener('click', () => {
34+
worker.postMessage({ postMessage: true });
35+
});
36+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
onmessage = function (event) {
2+
const workerResult = { timestamp: Date.now(), ...event.data };
3+
4+
workerResult.onmessage = true;
5+
6+
postMessage(workerResult);
7+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
publicPath: './'
13+
},
14+
plugins: [
15+
new HtmlWebpackPlugin({
16+
template: path.resolve(__dirname, './fixtures/index.html')
17+
}),
18+
new Self()
19+
],
20+
module: {
21+
rules: [
22+
{
23+
resourceQuery: /raw/,
24+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
25+
// @ts-ignore - according to assets-loader, it is a proper usage to use 'asset/source'
26+
type: 'asset/source'
27+
}
28+
]
29+
}
30+
};
31+
32+
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">(()=>{var e={377:()=>{console.log("Hello world")}},r={};function o(t){var n=r[t];if(void 0!==n)return n.exports;var a=r[t]={exports:{}};return e[t](a,a.exports,o),a.exports}o.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return o.d(r,{a:r}),r},o.d=(e,r)=>{for(var t in r)o.o(r,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},o.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),(()=>{"use strict";o(377)})()})();</script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>

0 commit comments

Comments
 (0)