Skip to content

Commit a3bdfd1

Browse files
author
legitatx
committed
⬆️ Update to Webpack 5
1 parent 1979f1b commit a3bdfd1

File tree

11 files changed

+1116
-1167
lines changed

11 files changed

+1116
-1167
lines changed

package.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,54 +50,54 @@
5050
"hot-reload"
5151
],
5252
"dependencies": {
53-
"@types/webpack": "^4.41.26",
53+
"@types/webpack": "^5.28.0",
5454
"@types/webpack-sources": "^2.1.0",
5555
"colors": "^1.4.0",
5656
"lodash": "^4.17.21",
5757
"minimist": "^1.2.5",
5858
"useragent": "^2.3.0",
59-
"webextension-polyfill": "^0.7.0",
60-
"webpack-sources": "^2.2.0",
61-
"ws": "^7.4.3"
59+
"webextension-polyfill": "^0.8.0",
60+
"webpack-sources": "^2.3.0",
61+
"ws": "^7.5.0"
6262
},
6363
"devDependencies": {
64-
"@babel/core": "^7.13.8",
65-
"@babel/plugin-proposal-class-properties": "^7.13.0",
66-
"@babel/plugin-proposal-logical-assignment-operators": "^7.13.8",
67-
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
64+
"@babel/core": "^7.14.6",
65+
"@babel/plugin-proposal-class-properties": "^7.14.5",
66+
"@babel/plugin-proposal-logical-assignment-operators": "^7.14.5",
67+
"@babel/plugin-proposal-object-rest-spread": "^7.14.7",
6868
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
69-
"@babel/plugin-transform-regenerator": "^7.12.13",
69+
"@babel/plugin-transform-regenerator": "^7.14.5",
7070
"@babel/polyfill": "^7.6.0",
71-
"@babel/preset-env": "^7.13.9",
72-
"@types/chai": "^4.2.15",
71+
"@babel/preset-env": "^7.14.7",
72+
"@types/chai": "^4.2.19",
7373
"@types/colors": "^1.2.1",
74-
"@types/lodash": "^4.14.168",
74+
"@types/lodash": "^4.14.170",
7575
"@types/minimist": "^1.2.1",
76-
"@types/mocha": "^8.2.1",
77-
"@types/sinon": "^9.0.10",
76+
"@types/mocha": "^8.2.2",
77+
"@types/sinon": "^10.0.2",
7878
"@types/useragent": "^2.3.0",
79-
"@types/ws": "^7.4.0",
80-
"autoprefixer": "^10.2.4",
79+
"@types/ws": "^7.4.5",
80+
"autoprefixer": "^10.2.6",
8181
"babel-loader": "^8.2.2",
82-
"chai": "^4.3.3",
83-
"copy-webpack-plugin": "^8.0.0",
84-
"css-loader": "^5.1.1",
85-
"husky": "^5.1.3",
82+
"chai": "^4.3.4",
83+
"copy-webpack-plugin": "^9.0.0",
84+
"css-loader": "^5.2.6",
85+
"husky": "^6.0.0",
8686
"json-loader": "^0.5.7",
87-
"lint-staged": "^10.5.4",
88-
"mini-css-extract-plugin": "^1.3.9",
89-
"mocha": "^8.3.0",
90-
"prettier": "^2.2.1",
87+
"lint-staged": "^11.0.0",
88+
"mini-css-extract-plugin": "^1.6.0",
89+
"mocha": "^9.0.1",
90+
"prettier": "^2.3.1",
9191
"raw-loader": "^4.0.2",
92-
"sinon": "^9.2.4",
92+
"sinon": "^11.1.1",
9393
"source-map-support": "^0.5.19",
94-
"style-loader": "^2.0.0",
95-
"ts-loader": "^8.0.17",
94+
"style-loader": "^3.0.0",
95+
"ts-loader": "^9.2.3",
9696
"tslint": "^5.20.1",
9797
"tslint-config-prettier": "^1.18.0",
98-
"typescript": "^4.2.3",
99-
"webpack": "^5.24.3",
100-
"webpack-bundle-analyzer": "^4.4.0",
101-
"webpack-cli": "^4.5.0"
98+
"typescript": "^4.3.4",
99+
"webpack": "5.40.0",
100+
"webpack-bundle-analyzer": "^4.4.2",
101+
"webpack-cli": "4.7.2"
102102
}
103103
}

src/ExtensionReloader.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { merge } from "lodash";
2-
import { Compiler, Entry, Output, version } from "webpack";
2+
import { Compiler, Compilation, Chunk, Entry, version } from "webpack";
33
import { changesTriggerer } from "./hot-reload";
44
import { onlyOnDevelopmentMsg } from "./messages/warnings";
55
import { middlewareInjector } from "./middleware";
@@ -35,14 +35,16 @@ export default class ExtensionReloaderImpl extends AbstractPluginReloader
3535
}
3636

3737
public _whatChanged(
38-
chunks: IWebpackChunk[],
38+
chunks: Compilation["chunks"],
3939
{ background, contentScript, extensionPage }: IEntriesOption,
4040
) {
41-
const changedChunks = chunks.filter(({ name, hash }) => {
42-
const oldVersion = this._chunkVersions[name];
43-
this._chunkVersions[name] = hash;
44-
return hash !== oldVersion;
45-
});
41+
const changedChunks = [] as Chunk[];
42+
43+
for (const chunk of chunks) {
44+
const oldVersion = this._chunkVersions[chunk.name];
45+
this._chunkVersions[chunk.name] = chunk.hash;
46+
if (chunk.hash !== oldVersion) changedChunks.push(chunk);
47+
}
4648

4749
const contentOrBgChanged = changedChunks.some(({ name }) => {
4850
let contentChanged = false;
@@ -57,6 +59,7 @@ export default class ExtensionReloaderImpl extends AbstractPluginReloader
5759
return contentChanged || bgChanged;
5860
});
5961

62+
//
6063
const onlyPageChanged =
6164
!contentOrBgChanged &&
6265
changedChunks.some(({ name }) => {
@@ -67,6 +70,7 @@ export default class ExtensionReloaderImpl extends AbstractPluginReloader
6770
} else {
6871
pageChanged = name === extensionPage;
6972
}
73+
//
7074

7175
return pageChanged;
7276
});
@@ -83,7 +87,7 @@ export default class ExtensionReloaderImpl extends AbstractPluginReloader
8387
const parsedEntries: IEntriesOption = manifest
8488
? extractEntries(
8589
compiler.options.entry as Entry,
86-
compiler.options.output as Output,
90+
compiler.options.output as Compiler["options"]["output"],
8791
manifest,
8892
)
8993
: entries;
@@ -98,16 +102,14 @@ export default class ExtensionReloaderImpl extends AbstractPluginReloader
98102
};
99103
});
100104

101-
this._eventAPI.afterEmit((comp, done) => {
105+
this._eventAPI.afterEmit(comp => {
102106
const { contentOrBgChanged, onlyPageChanged } = this._whatChanged(
103107
comp.chunks,
104108
parsedEntries,
105109
);
106110

107111
if (contentOrBgChanged || onlyPageChanged) {
108-
this._triggerer(onlyPageChanged)
109-
.then(done)
110-
.catch(done);
112+
this._triggerer(onlyPageChanged);
111113
}
112114
});
113115
}

src/hot-reload/SignEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class SignEmitter {
1717
private _safeSignChange: (
1818
reloadPage: boolean,
1919
onlyPageChanged: boolean,
20-
onSuccess: () => void,
20+
onSuccess: (val?: any) => void,
2121
onError: (err: Error) => void,
2222
) => void;
2323
private _server: Server;

src/middleware/middleware-injector.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Compilation } from "webpack";
12
import { ConcatSource, Source } from "webpack-sources";
23
import { SourceFactory } from "../../typings";
34
import middleWareSourceBuilder from "./middleware-source-builder";
@@ -17,10 +18,11 @@ const middlewareInjector: MiddlewareInjector = (
1718
name === extensionPage ||
1819
(extensionPage && extensionPage.includes(name));
1920

20-
return (assets, chunks) =>
21-
chunks.reduce((prev, { name, files }) => {
21+
return (assets, chunks: Compilation["chunks"]) =>
22+
Array.from(chunks).reduce((prev, { name, files }) => {
2223
if (matchBgOrContentOrPage(name)) {
23-
files.forEach(entryPoint => {
24+
files.forEach((entryPoint) => {
25+
console.log(`Entry point: ${entryPoint}`);
2426
if (/\.js$/.test(entryPoint)) {
2527
const finalSrc = sourceFactory(source, assets[entryPoint]);
2628
prev[entryPoint] = finalSrc;

src/middleware/wer-middleware.raw.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
/* -------------------------------------------------- */
88
(function(window) {
99

10-
const injectionContext = {browser: null};
10+
const injectionContext = window || this || {browser: null};
11+
1112
(function() {
1213
`<%= polyfillSource %>`;
1314
}).bind(injectionContext)();
1415

15-
const { browser }: any = injectionContext;
16+
const { browser }: any = injectionContext || {};
1617
const signals: any = JSON.parse('<%= signals %>');
1718
const config: any = JSON.parse('<%= config %>');
1819

@@ -27,7 +28,7 @@
2728
} = signals;
2829
const { RECONNECT_INTERVAL, SOCKET_ERR_CODE_REF } = config;
2930

30-
const { extension, runtime, tabs } = browser;
31+
const { extension, runtime, tabs } = browser || {};
3132
const manifest = runtime.getManifest();
3233

3334
// =============================== Helper functions ======================================= //
@@ -38,9 +39,10 @@
3839

3940
// ========================== Called only on content scripts ============================== //
4041
function contentScriptWorker() {
42+
console.log('contentScriptWorker')
4143
runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg));
4244

43-
runtime.onMessage.addListener(({ type, payload }: IAction) => {
45+
runtime.onMessage.addListener(({ type, payload }: { type: string; payload: any }) => {
4446
switch (type) {
4547
case SIGN_RELOAD:
4648
logger("Detected Changes. Reloading ...");
@@ -56,7 +58,7 @@
5658

5759
// ======================== Called only on background scripts ============================= //
5860
function backgroundWorker(socket: WebSocket) {
59-
runtime.onMessage.addListener((action: IAction, sender) => {
61+
runtime.onMessage.addListener((action: { type: string; payload: any }, sender) => {
6062
if (action.type === SIGN_CONNECT) {
6163
return Promise.resolve(formatter("Connected to Extension Hot Reloader"));
6264
}
@@ -114,7 +116,7 @@
114116
function extensionPageWorker() {
115117
runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg));
116118

117-
runtime.onMessage.addListener(({ type, payload }: IAction) => {
119+
runtime.onMessage.addListener(({ type, payload }: { type: string; payload: any }) => {
118120
switch (type) {
119121
case SIGN_CHANGE:
120122
logger("Detected Changes. Reloading ...");

src/utils/manifest.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { readFileSync } from "fs";
22
import { flatMapDeep } from "lodash";
3-
import { Entry, Output } from "webpack";
3+
import { Compiler, Entry } from "webpack";
44
import {
55
bgScriptEntryErrorMsg,
66
bgScriptManifestRequiredMsg,
77
} from "../messages/errors";
88

99
export function extractEntries(
1010
webpackEntry: Entry,
11-
webpackOutput: Output = {},
11+
webpackOutput: Compiler["options"]["output"] = {},
1212
manifestPath: string,
1313
): IEntriesOption {
1414
const manifestJson = JSON.parse(
@@ -28,9 +28,9 @@ export function extractEntries(
2828
const bgScriptFileNames = background.scripts;
2929
const toRemove = (filename as string).replace("[name]", "");
3030

31-
const bgWebpackEntry = Object.keys(webpackEntry).find(entryName =>
31+
const bgWebpackEntry = Object.keys(webpackEntry).find((entryName) =>
3232
bgScriptFileNames.some(
33-
bgManifest => bgManifest.replace(toRemove, "") === entryName,
33+
(bgManifest) => bgManifest.replace(toRemove, "") === entryName,
3434
),
3535
);
3636

@@ -39,11 +39,11 @@ export function extractEntries(
3939
}
4040

4141
const contentEntries: unknown = content_scripts
42-
? flatMapDeep(Object.keys(webpackEntry), entryName =>
42+
? flatMapDeep(Object.keys(webpackEntry), (entryName) =>
4343
content_scripts.map(({ js }) =>
4444
js
45-
.map(contentItem => contentItem.replace(toRemove, ""))
46-
.filter(contentItem => contentItem === entryName),
45+
.map((contentItem) => contentItem.replace(toRemove, ""))
46+
.filter((contentItem) => contentItem === entryName),
4747
),
4848
)
4949
: null;

src/webpack/AbstractExtensionReloader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Compiler, Plugin } from "webpack";
1+
import { Compiler } from "webpack";
22
import CompilerEventsFacade from "./CompilerEventsFacade";
33

4-
export default abstract class AbstractExtensionReloader implements Plugin {
4+
export default abstract class AbstractExtensionReloader {
55
public context: any;
66
protected _injector: InjectMiddleware;
77
protected _triggerer: Triggerer;

src/webpack/CompilerEventsFacade.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
import { Compiler } from "webpack";
1+
import { Compiler, Compilation } from "webpack";
22

33
export default class CompilerEventsFacade {
44
public static extensionName = "webpack-extension-reloader";
55
private _compiler: Compiler;
66
private _legacyTapable: boolean;
77

8-
constructor(compiler) {
8+
constructor(compiler: Compiler) {
99
this._compiler = compiler;
1010
this._legacyTapable = !compiler.hooks;
1111
}
1212

13-
public afterOptimizeChunkAssets(call) {
13+
public afterOptimizeChunkAssets(
14+
call: (compilation: Compilation, chunks: Compilation["chunks"]) => void,
15+
) {
1416
return this._legacyTapable
15-
? this._compiler.plugin("compilation", comp =>
16-
comp.plugin("after-optimize-chunk-assets", chunks =>
17+
? (this._compiler as any).plugin("compilation", (comp) =>
18+
comp.plugin("after-optimize-chunk-assets", (chunks) =>
1719
call(comp, chunks),
1820
),
1921
)
2022
: this._compiler.hooks.compilation.tap(
2123
CompilerEventsFacade.extensionName,
22-
comp =>
24+
(comp) =>
2325
comp.hooks.afterOptimizeChunkAssets.tap(
2426
CompilerEventsFacade.extensionName,
25-
chunks => call(comp, chunks),
27+
(chunks) => call(comp, chunks),
2628
),
2729
);
2830
}
2931

30-
public afterEmit(call) {
32+
public afterEmit(call: (compilation: Compilation) => void) {
3133
return this._legacyTapable
32-
? this._compiler.plugin("after-emit", call)
34+
? (this._compiler as any).plugin("after-emit", call)
3335
: this._compiler.hooks.afterEmit.tap(
3436
CompilerEventsFacade.extensionName,
3537
call,

typings/declarations.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare interface IMiddlewareTemplateParams {
1212

1313
declare type InjectMiddleware = (
1414
assets: Record<string, any>,
15-
chunks: IWebpackChunk[],
15+
chunks: Set<any>,
1616
) => Record<string, any>;
1717

1818
declare type MiddlewareInjector = (

0 commit comments

Comments
 (0)