Skip to content

Commit 5b5654c

Browse files
feat: added types
1 parent 613fdf7 commit 5b5654c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2235
-1488
lines changed

package-lock.json

+1,270-1,326
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@
1111
"type": "opencollective",
1212
"url": "https://opencollective.com/webpack"
1313
},
14-
"main": "dist/cjs.js",
14+
"main": "dist/index.js",
15+
"types": "types/index.d.ts",
1516
"engines": {
1617
"node": ">= 12.13.0"
1718
},
1819
"scripts": {
1920
"start": "npm run build -- -w",
2021
"prebuild": "npm run clean",
21-
"build": "cross-env NODE_ENV=production babel src -d dist --ignore \"src/**/*.test.js\" --copy-files",
22+
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
23+
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
24+
"build": "npm-run-all -p \"build:**\"",
2225
"postbuild": "es-check es5 dist/hmr/hotModuleReplacement.js",
2326
"clean": "del-cli dist",
2427
"commitlint": "commitlint --from=master",
2528
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
2629
"lint:js": "eslint --cache .",
30+
"lint:types": "tsc --pretty --noEmit",
2731
"lint": "npm-run-all -l -p \"lint:**\"",
2832
"prepare": "husky install && npm run build",
2933
"release": "standard-version",
@@ -37,7 +41,8 @@
3741
"test": "cross-env NODE_ENV=test npm run test:coverage"
3842
},
3943
"files": [
40-
"dist"
44+
"dist",
45+
"types"
4146
],
4247
"peerDependencies": {
4348
"webpack": "^5.0.0"
@@ -74,6 +79,7 @@
7479
"sass": "^1.45.2",
7580
"sass-loader": "^12.1.0",
7681
"standard-version": "^9.3.0",
82+
"typescript": "^4.5.4",
7783
"webpack": "^5.58.1",
7884
"webpack-cli": "^4.7.2",
7985
"webpack-dev-server": "^4.7.2"

src/cjs.js

-1
This file was deleted.

src/hmr/hotModuleReplacement.js

+54-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
func-names
66
*/
77

8+
/** @typedef {any} TODO */
9+
810
const normalizeUrl = require("./normalize-url");
911

1012
const srcByModuleId = Object.create(null);
@@ -13,10 +15,16 @@ const noDocument = typeof document === "undefined";
1315

1416
const { forEach } = Array.prototype;
1517

18+
/**
19+
* @param {function} fn
20+
* @param {number} time
21+
* @returns {(function(): void)|*}
22+
*/
1623
function debounce(fn, time) {
1724
let timeout = 0;
1825

1926
return function () {
27+
// @ts-ignore
2028
const self = this;
2129
// eslint-disable-next-line prefer-rest-params
2230
const args = arguments;
@@ -26,18 +34,24 @@ function debounce(fn, time) {
2634
};
2735

2836
clearTimeout(timeout);
37+
38+
// @ts-ignore
2939
timeout = setTimeout(functionCall, time);
3040
};
3141
}
3242

3343
function noop() {}
3444

45+
/**
46+
* @param {TODO} moduleId
47+
* @returns {TODO}
48+
*/
3549
function getCurrentScriptUrl(moduleId) {
3650
let src = srcByModuleId[moduleId];
3751

3852
if (!src) {
3953
if (document.currentScript) {
40-
({ src } = document.currentScript);
54+
({ src } = /** @type {HTMLScriptElement} */ (document.currentScript));
4155
} else {
4256
const scripts = document.getElementsByTagName("script");
4357
const lastScriptTag = scripts[scripts.length - 1];
@@ -50,6 +64,10 @@ function getCurrentScriptUrl(moduleId) {
5064
srcByModuleId[moduleId] = src;
5165
}
5266

67+
/**
68+
* @param {string} fileMap
69+
* @returns {null | string[]}
70+
*/
5371
return function (fileMap) {
5472
if (!src) {
5573
return null;
@@ -76,6 +94,10 @@ function getCurrentScriptUrl(moduleId) {
7694
};
7795
}
7896

97+
/**
98+
* @param {TODO} el
99+
* @param {string} [url]
100+
*/
79101
function updateCss(el, url) {
80102
if (!url) {
81103
if (!el.href) {
@@ -86,7 +108,7 @@ function updateCss(el, url) {
86108
url = el.href.split("?")[0];
87109
}
88110

89-
if (!isUrlRequest(url)) {
111+
if (!isUrlRequest(/** @type {string} */ (url))) {
90112
return;
91113
}
92114

@@ -134,22 +156,36 @@ function updateCss(el, url) {
134156
}
135157
}
136158

159+
/**
160+
* @param {string} href
161+
* @param {TODO} src
162+
* @returns {TODO}
163+
*/
137164
function getReloadUrl(href, src) {
138165
let ret;
139166

140167
// eslint-disable-next-line no-param-reassign
141-
href = normalizeUrl(href, { stripWWW: false });
142-
143-
// eslint-disable-next-line array-callback-return
144-
src.some((url) => {
145-
if (href.indexOf(src) > -1) {
146-
ret = url;
168+
href = normalizeUrl(href);
169+
170+
src.some(
171+
/**
172+
* @param {string} url
173+
*/
174+
// eslint-disable-next-line array-callback-return
175+
(url) => {
176+
if (href.indexOf(src) > -1) {
177+
ret = url;
178+
}
147179
}
148-
});
180+
);
149181

150182
return ret;
151183
}
152184

185+
/**
186+
* @param {string} [src]
187+
* @returns {boolean}
188+
*/
153189
function reloadStyle(src) {
154190
if (!src) {
155191
return false;
@@ -195,6 +231,10 @@ function reloadAll() {
195231
});
196232
}
197233

234+
/**
235+
* @param {string} url
236+
* @returns {boolean}
237+
*/
198238
function isUrlRequest(url) {
199239
// An URL is not an request if
200240

@@ -206,6 +246,11 @@ function isUrlRequest(url) {
206246
return true;
207247
}
208248

249+
/**
250+
* @param {TODO} moduleId
251+
* @param {TODO} options
252+
* @returns {TODO}
253+
*/
209254
module.exports = function (moduleId, options) {
210255
if (noDocument) {
211256
console.log("no window.document found, will not HMR CSS");

src/hmr/normalize-url.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable */
22

3+
/**
4+
* @param {string[]} pathComponents
5+
* @returns {string}
6+
*/
37
function normalizeUrl(pathComponents) {
48
return pathComponents
59
.reduce(function (accumulator, item) {
@@ -14,10 +18,14 @@ function normalizeUrl(pathComponents) {
1418
}
1519

1620
return accumulator;
17-
}, [])
21+
}, /** @type {string[]} */ ([]))
1822
.join("/");
1923
}
2024

25+
/**
26+
* @param {string} urlString
27+
* @returns {string}
28+
*/
2129
module.exports = function (urlString) {
2230
urlString = urlString.trim();
2331

0 commit comments

Comments
 (0)