Skip to content

Types #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 14, 2022
Merged

Types #895

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,596 changes: 1,270 additions & 1,326 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"main": "dist/cjs.js",
"main": "dist/index.js",
"types": "types/index.d.ts",
"engines": {
"node": ">= 12.13.0"
},
"scripts": {
"start": "npm run build -- -w",
"prebuild": "npm run clean",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore \"src/**/*.test.js\" --copy-files",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"build": "npm-run-all -p \"build:**\"",
"postbuild": "es-check es5 dist/hmr/hotModuleReplacement.js",
"clean": "del-cli dist",
"commitlint": "commitlint --from=master",
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
"lint:js": "eslint --cache .",
"lint:types": "tsc --pretty --noEmit",
"lint": "npm-run-all -l -p \"lint:**\"",
"prepare": "husky install && npm run build",
"release": "standard-version",
Expand All @@ -37,7 +41,8 @@
"test": "cross-env NODE_ENV=test npm run test:coverage"
},
"files": [
"dist"
"dist",
"types"
],
"peerDependencies": {
"webpack": "^5.0.0"
Expand Down Expand Up @@ -74,6 +79,7 @@
"sass": "^1.45.2",
"sass-loader": "^12.1.0",
"standard-version": "^9.3.0",
"typescript": "^4.5.4",
"webpack": "^5.58.1",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^4.7.2"
Expand Down
1 change: 0 additions & 1 deletion src/cjs.js

This file was deleted.

63 changes: 54 additions & 9 deletions src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
func-names
*/

/** @typedef {any} TODO */

const normalizeUrl = require("./normalize-url");

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

const { forEach } = Array.prototype;

/**
* @param {function} fn
* @param {number} time
* @returns {(function(): void)|*}
*/
function debounce(fn, time) {
let timeout = 0;

return function () {
// @ts-ignore
const self = this;
// eslint-disable-next-line prefer-rest-params
const args = arguments;
Expand All @@ -26,18 +34,24 @@ function debounce(fn, time) {
};

clearTimeout(timeout);

// @ts-ignore
timeout = setTimeout(functionCall, time);
};
}

function noop() {}

/**
* @param {TODO} moduleId
* @returns {TODO}
*/
function getCurrentScriptUrl(moduleId) {
let src = srcByModuleId[moduleId];

if (!src) {
if (document.currentScript) {
({ src } = document.currentScript);
({ src } = /** @type {HTMLScriptElement} */ (document.currentScript));
} else {
const scripts = document.getElementsByTagName("script");
const lastScriptTag = scripts[scripts.length - 1];
Expand All @@ -50,6 +64,10 @@ function getCurrentScriptUrl(moduleId) {
srcByModuleId[moduleId] = src;
}

/**
* @param {string} fileMap
* @returns {null | string[]}
*/
return function (fileMap) {
if (!src) {
return null;
Expand All @@ -76,6 +94,10 @@ function getCurrentScriptUrl(moduleId) {
};
}

/**
* @param {TODO} el
* @param {string} [url]
*/
function updateCss(el, url) {
if (!url) {
if (!el.href) {
Expand All @@ -86,7 +108,7 @@ function updateCss(el, url) {
url = el.href.split("?")[0];
}

if (!isUrlRequest(url)) {
if (!isUrlRequest(/** @type {string} */ (url))) {
return;
}

Expand Down Expand Up @@ -134,22 +156,36 @@ function updateCss(el, url) {
}
}

/**
* @param {string} href
* @param {TODO} src
* @returns {TODO}
*/
function getReloadUrl(href, src) {
let ret;

// eslint-disable-next-line no-param-reassign
href = normalizeUrl(href, { stripWWW: false });

// eslint-disable-next-line array-callback-return
src.some((url) => {
if (href.indexOf(src) > -1) {
ret = url;
href = normalizeUrl(href);

src.some(
/**
* @param {string} url
*/
// eslint-disable-next-line array-callback-return
(url) => {
if (href.indexOf(src) > -1) {
ret = url;
}
}
});
);

return ret;
}

/**
* @param {string} [src]
* @returns {boolean}
*/
function reloadStyle(src) {
if (!src) {
return false;
Expand Down Expand Up @@ -195,6 +231,10 @@ function reloadAll() {
});
}

/**
* @param {string} url
* @returns {boolean}
*/
function isUrlRequest(url) {
// An URL is not an request if

Expand All @@ -206,6 +246,11 @@ function isUrlRequest(url) {
return true;
}

/**
* @param {TODO} moduleId
* @param {TODO} options
* @returns {TODO}
*/
module.exports = function (moduleId, options) {
if (noDocument) {
console.log("no window.document found, will not HMR CSS");
Expand Down
10 changes: 9 additions & 1 deletion src/hmr/normalize-url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable */

/**
* @param {string[]} pathComponents
* @returns {string}
*/
function normalizeUrl(pathComponents) {
return pathComponents
.reduce(function (accumulator, item) {
Expand All @@ -14,10 +18,14 @@ function normalizeUrl(pathComponents) {
}

return accumulator;
}, [])
}, /** @type {string[]} */ ([]))
.join("/");
}

/**
* @param {string} urlString
* @returns {string}
*/
module.exports = function (urlString) {
urlString = urlString.trim();

Expand Down
Loading