Skip to content

Commit

Permalink
feat: added types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 21, 2021
1 parent f4fb15f commit 8f02c3f
Show file tree
Hide file tree
Showing 25 changed files with 6,500 additions and 655 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -83,6 +83,10 @@ jobs:
if: matrix.os == 'windows-latest'
run: npm i -g npm

- name: Use latest NPM on windows
if: matrix.webpack-version == 4
run: sed -i'.original' 's/"build:types"/"_unused"/g' package.json

- name: Install dependencies
run: npm ci

Expand Down
94 changes: 87 additions & 7 deletions bin/process-arguments.js
Expand Up @@ -5,8 +5,50 @@ const path = require("path");
// Based on https://github.com/webpack/webpack/blob/master/lib/cli.js
// Please do not modify it

/** @typedef {"unknown-argument" | "unexpected-non-array-in-path" | "unexpected-non-object-in-path" | "multiple-values-unexpected" | "invalid-value"} ProblemType */

/**
* @typedef {Object} Problem
* @property {ProblemType} type
* @property {string} path
* @property {string} argument
* @property {any=} value
* @property {number=} index
* @property {string=} expected
*/

/**
* @typedef {Object} LocalProblem
* @property {ProblemType} type
* @property {string} path
* @property {string=} expected
*/

/**
* @typedef {Object} ArgumentConfig
* @property {string} description
* @property {string} path
* @property {boolean} multiple
* @property {"enum"|"string"|"path"|"number"|"boolean"|"RegExp"|"reset"} type
* @property {any[]=} values
*/

/**
* @typedef {Object} Argument
* @property {string} description
* @property {"string"|"number"|"boolean"} simpleType
* @property {boolean} multiple
* @property {ArgumentConfig[]} configs
*/

const cliAddedItems = new WeakMap();

/**
* @param {any} config configuration
* @param {string} schemaPath path in the config
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
* @returns {{ problem?: LocalProblem, object?: any, property?: string | number, value?: any }} problem or object with property and value
*/
const getObjectAndProperty = (config, schemaPath, index = 0) => {
if (!schemaPath) {
return { value: config };
Expand Down Expand Up @@ -81,10 +123,10 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
i++;
}

const value = current[property];
const value = current[/** @type {string} */ (property)];

if (property.endsWith("[]")) {
const name = property.slice(0, -2);
if (/** @type {string} */ (property).endsWith("[]")) {
const name = /** @type {string} */ (property).slice(0, -2);
// eslint-disable-next-line no-shadow
const value = current[name];

Expand Down Expand Up @@ -140,6 +182,11 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
return { object: current, property, value };
};

/**
* @param {ArgumentConfig} argConfig processing instructions
* @param {any} value the value
* @returns {any | undefined} parsed value
*/
const parseValueForArgumentConfig = (argConfig, value) => {
// eslint-disable-next-line default-case
switch (argConfig.type) {
Expand Down Expand Up @@ -194,11 +241,11 @@ const parseValueForArgumentConfig = (argConfig, value) => {

break;
case "enum":
if (argConfig.values.includes(value)) {
if (/** @type {any[]} */ (argConfig.values).includes(value)) {
return value;
}

for (const item of argConfig.values) {
for (const item of /** @type {any[]} */ (argConfig.values)) {
if (`${item}` === value) return item;
}

Expand All @@ -212,6 +259,10 @@ const parseValueForArgumentConfig = (argConfig, value) => {
}
};

/**
* @param {ArgumentConfig} argConfig processing instructions
* @returns {string | undefined} expected message
*/
const getExpectedValue = (argConfig) => {
switch (argConfig.type) {
default:
Expand All @@ -221,12 +272,21 @@ const getExpectedValue = (argConfig) => {
case "RegExp":
return "regular expression (example: /ab?c*/)";
case "enum":
return argConfig.values.map((v) => `${v}`).join(" | ");
return /** @type {any[]} */ (argConfig.values)
.map((v) => `${v}`)
.join(" | ");
case "reset":
return "true (will reset the previous value to an empty array)";
}
};

/**
* @param {any} config configuration
* @param {string} schemaPath path in the config
* @param {any} value parsed value
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
* @returns {LocalProblem | null} problem or null for success
*/
const setValue = (config, schemaPath, value, index) => {
const { problem, object, property } = getObjectAndProperty(
config,
Expand All @@ -238,11 +298,18 @@ const setValue = (config, schemaPath, value, index) => {
return problem;
}

object[property] = value;
object[/** @type {string} */ (property)] = value;

return null;
};

/**
* @param {ArgumentConfig} argConfig processing instructions
* @param {any} config configuration
* @param {any} value the value
* @param {number | undefined} index the index if multiple values provided
* @returns {LocalProblem | null} a problem if any
*/
const processArgumentConfig = (argConfig, config, value, index) => {
// eslint-disable-next-line no-undefined
if (index !== undefined && !argConfig.multiple) {
Expand Down Expand Up @@ -272,7 +339,16 @@ const processArgumentConfig = (argConfig, config, value, index) => {
return null;
};

/**
* @param {Record<string, Argument>} args object of arguments
* @param {any} config configuration
* @param {Record<string, string | number | boolean | RegExp | (string | number | boolean | RegExp)[]>} values object with values
* @returns {Problem[] | null} problems or null for success
*/
const processArguments = (args, config, values) => {
/**
* @type {Problem[]}
*/
const problems = [];

for (const key of Object.keys(values)) {
Expand All @@ -289,6 +365,10 @@ const processArguments = (args, config, values) => {
continue;
}

/**
* @param {any} value
* @param {number | undefined} i
*/
const processValue = (value, i) => {
const currentProblems = [];

Expand Down
3 changes: 3 additions & 0 deletions bin/webpack-dev-server.js
Expand Up @@ -108,6 +108,9 @@ if (!cli.installed) {

console.error(notify);

/**
* @type {string}
*/
let packageManager;

if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
Expand Down
22 changes: 19 additions & 3 deletions client-src/clients/SockJSClient.js
Expand Up @@ -2,25 +2,41 @@ import SockJS from "../modules/sockjs-client/index.js";
import { log } from "../utils/log.js";

export default class SockJSClient {
/**
* @param {string} url
*/
constructor(url) {
// SockJS requires `http` and `https` protocols
this.sock = new SockJS(
url.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:")
);
this.sock.onerror = (error) => {
log.error(error);
};
this.sock.onerror =
/**
* @param {Error} error
*/
(error) => {
log.error(error);
};
}

/**
* @param {(...args: any[]) => void} f
*/
onOpen(f) {
this.sock.onopen = f;
}

/**
* @param {(...args: any[]) => void} f
*/
onClose(f) {
this.sock.onclose = f;
}

// call f with the message string as the first argument
/**
* @param {(...args: any[]) => void} f
*/
onMessage(f) {
this.sock.onmessage = (e) => {
f(e.data);
Expand Down
12 changes: 12 additions & 0 deletions client-src/clients/WebSocketClient.js
@@ -1,22 +1,34 @@
import { log } from "../utils/log.js";

export default class WebSocketClient {
/**
* @param {string} url
*/
constructor(url) {
this.client = new WebSocket(url);
this.client.onerror = (error) => {
log.error(error);
};
}

/**
* @param {(...args: any[]) => void} f
*/
onOpen(f) {
this.client.onopen = f;
}

/**
* @param {(...args: any[]) => void} f
*/
onClose(f) {
this.client.onclose = f;
}

// call f with the message string as the first argument
/**
* @param {(...args: any[]) => void} f
*/
onMessage(f) {
this.client.onmessage = (e) => {
f(e.data);
Expand Down
16 changes: 16 additions & 0 deletions client-src/index.js
Expand Up @@ -45,6 +45,9 @@ if (typeof parsedResourceQuery.reconnect !== "undefined") {
options.reconnect = Number(parsedResourceQuery.reconnect);
}

/**
* @param {string} level
*/
function setAllLogLevel(level) {
// This is needed because the HMR logger operate separately from dev server logger
webpackHotLog.setLogLevel(
Expand Down Expand Up @@ -90,6 +93,9 @@ const onSocketMessage = {

sendMessage("Invalid");
},
/**
* @param {string} hash
*/
hash(hash) {
status.previousHash = status.currentHash;
status.currentHash = hash;
Expand Down Expand Up @@ -160,6 +166,10 @@ const onSocketMessage = {

self.location.reload();
},
/**
* @param {Error[]} warnings
* @param {any} params
*/
warnings(warnings, params) {
log.warn("Warnings while compiling.");

Expand Down Expand Up @@ -190,6 +200,9 @@ const onSocketMessage = {

reloadApp(options, status);
},
/**
* @param {Error[]} errors
*/
errors(errors) {
log.error("Errors while compiling. Reload prevented.");

Expand All @@ -214,6 +227,9 @@ const onSocketMessage = {
show("error", errors);
}
},
/**
* @param {Error} error
*/
error(error) {
log.error(error);
},
Expand Down
5 changes: 5 additions & 0 deletions client-src/utils/log.js
Expand Up @@ -5,6 +5,11 @@ const name = "webpack-dev-server";
// to be set by the CLI or API
const defaultLevel = "info";

// options new options, merge with old options
/**
* @param {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose"} level
* @returns {void}
*/
function setLogLevel(level) {
logger.configureDefaultLogger({ level });
}
Expand Down
4 changes: 4 additions & 0 deletions client-src/utils/sendMessage.js
@@ -1,6 +1,10 @@
/* global __resourceQuery WorkerGlobalScope */

// Send messages to the outside, so plugins can consume it.
/**
* @param {string} type
* @param {any} data
*/
function sendMsg(type, data) {
if (
typeof self !== "undefined" &&
Expand Down
2 changes: 2 additions & 0 deletions client-src/webpack.config.js
Expand Up @@ -4,6 +4,7 @@ const path = require("path");
const webpack = require("webpack");
const { merge } = require("webpack-merge");

// @ts-ignore
const library = webpack.webpack
? {
library: {
Expand All @@ -27,6 +28,7 @@ const baseForModules = {
optimization: {
minimize: false,
},
// @ts-ignore
target: webpack.webpack ? ["web", "es5"] : "web",
module: {
rules: [
Expand Down

0 comments on commit 8f02c3f

Please sign in to comment.