refactor, rename, extend, fix, add tests

This commit is contained in:
Tobias Koppers 2021-11-03 12:28:46 +01:00
parent e095f51c46
commit 03001118e9
9 changed files with 247 additions and 166 deletions

View File

@ -1,72 +0,0 @@
type Accept = (
module: string | string[],
callback?: () => void,
errorHandler?: (
err: Error,
ids: { moduleId: string | number; dependencyId: string | number }
) => void
) => void;
type SelfAccept = (
errorHandler: (
err: Error,
ids: { moduleId: string | number; module: any }
) => void
) => void;
interface ApplyInfo {
type:
| "self-declined"
| "declined"
| "unaccepted"
| "accepted"
| "disposed"
| "accept-errored"
| "self-accept-errored"
| "self-accept-error-handler-errored";
moduleId: number; // The module in question.
dependencyId: number; // For errors: the module id owning the accept handler.
chain: number[]; // For declined/accepted/unaccepted: the chain from where the update was propagated.
parentId: number; // For declined: the module id of the declining parent
outdatedModules: number[]; // For accepted: the modules that are outdated and will be disposed
outdatedDependencies: {
[id: number]: number[];
};
error: Error; // For errors: the thrown error
originalError: Error; // For self-accept-error-handler-errored:
// the error thrown by the module before the error handler tried to handle it.
}
interface ApplyOptions {
ignoreUnaccepted?: boolean;
ignoreDeclined?: boolean;
ignoreErrored?: boolean;
onDeclined?(callback: (info: ApplyInfo) => void): void;
onUnaccepted?(callback: (info: ApplyInfo) => void): void;
onAccepted?(callback: (info: ApplyInfo) => void): void;
onDisposed?(callback: (info: ApplyInfo) => void): void;
onErrored?(callback: (info: ApplyInfo) => void): void;
}
enum HotUpdateStatus {
idle = "idle",
check = "check",
prepare = "prepare",
ready = "ready",
dispose = "dispose",
apply = "apply",
abort = "abort",
fail = "fail"
}
export interface Hot {
accept: Accept & SelfAccept;
status(): HotUpdateStatus;
decline(module?: string | string[]): void;
dispose(callback: (data: any) => void);
invalidate(): void;
addStatusHandler(callback: (status: HotUpdateStatus) => void);
removeStatusHandler(callback: () => void): void;
data: any;
check(autoApply: boolean | any): Promise<any[]>;
apply(options?: ApplyOptions): Promise<any[]>;
}

View File

@ -1,65 +0,0 @@
export {};
interface ExportInfo {
used: boolean;
provideInfo: boolean | null | undefined;
useInfo: boolean | null | undefined;
}
interface ExportsInfo {
[k: string]: ExportInfo & ExportsInfo;
}
interface Context {
resolve(dependency: string): string | number;
keys(): Array<string>;
id: string | number;
(dependency: string): any;
}
declare global {
interface ImportMeta {
url: string;
webpack: number;
webpackHot: import("./hot").Hot;
}
var __resourceQuery: string;
var __webpack_public_path__: string;
var __webpack_nonce__: string;
var __webpack_chunkname__: string;
var __webpack_base_uri__: string;
var __webpack_runtime_id__: string;
var __webpack_hash__: string;
var __webpack_modules__: object;
var __webpack_require__: (id: string | number) => any;
var __webpack_chunk_load__: (id: string | number) => Promise<any>;
var __non_webpack_require__: (id: string) => any;
var __webpack_is_included__: (module: string) => boolean;
var __webpack_exports_info__: ExportsInfo;
var __webpack_share_scopes__: any;
var __webpack_init_sharing__: (scope: string) => Promise<void>;
namespace NodeJS {
interface Module {
hot: import("./hot").Hot;
}
interface Require {
ensure(
dependencies: string[],
callback: (require: (module: string) => void) => void,
errorCallback?: (error: Error) => void,
chunkName?: string
): void;
context(
ctx: string,
includeSubdirs?: boolean,
filter?: RegExp,
mode?: "sync" | "eager" | "weak" | "lazy" | "lazy-once"
): Context;
include(dependency: string): void;
resolveWeak(dependency: string): void;
}
}
}

200
module.d.ts vendored Normal file
View File

@ -0,0 +1,200 @@
declare namespace webpack {
type HotEvent =
| {
type: "disposed";
/** The module in question. */
moduleId: number;
}
| {
type: "self-declined" | "unaccepted";
/** The module in question. */
moduleId: number;
/** the chain from where the update was propagated. */
chain: number[];
}
| {
type: "declined";
/** The module in question. */
moduleId: number;
/** the chain from where the update was propagated. */
chain: number[];
/** the module id of the declining parent */
parentId: number;
}
| {
type: "accepted";
/** The module in question. */
moduleId: number;
/** the chain from where the update was propagated. */
chain: number[];
/** the modules that are outdated and will be disposed */
outdatedModules: number[];
/** the accepted dependencies that are outdated */
outdatedDependencies: {
[id: number]: number[];
};
}
| {
type: "accept-error-handler-errored";
/** The module in question. */
moduleId: number;
/** the module id owning the accept handler. */
dependencyId: number;
/** the thrown error */
error: Error;
/** the error thrown by the module before the error handler tried to handle it. */
originalError: Error;
}
| {
type: "self-accept-error-handler-errored";
/** The module in question. */
moduleId: number;
/** the thrown error */
error: Error;
/** the error thrown by the module before the error handler tried to handle it. */
originalError: Error;
}
| {
type: "accept-errored";
/** The module in question. */
moduleId: number;
/** the module id owning the accept handler. */
dependencyId: number;
/** the thrown error */
error: Error;
}
| {
type: "self-accept-errored";
/** The module in question. */
moduleId: number;
/** the thrown error */
error: Error;
};
interface ApplyOptions {
ignoreUnaccepted?: boolean;
ignoreDeclined?: boolean;
ignoreErrored?: boolean;
onDeclined?(callback: (info: HotEvent) => void): void;
onUnaccepted?(callback: (info: HotEvent) => void): void;
onAccepted?(callback: (info: HotEvent) => void): void;
onDisposed?(callback: (info: HotEvent) => void): void;
onErrored?(callback: (info: HotEvent) => void): void;
}
const enum HotUpdateStatus {
idle = "idle",
check = "check",
prepare = "prepare",
ready = "ready",
dispose = "dispose",
apply = "apply",
abort = "abort",
fail = "fail"
}
interface Hot {
accept: {
(
modules: string | string[],
callback?: (outdatedDependencies: string[]) => void,
errorHandler?: (
err: Error,
context: { moduleId: string | number; dependencyId: string | number }
) => void
): void;
(
errorHandler: (
err: Error,
ids: { moduleId: string | number; module: NodeJS.Module }
) => void
): void;
};
status(): HotUpdateStatus;
decline(module?: string | string[]): void;
dispose(callback: (data: object) => void): void;
addDisposeHandler(callback: (data: object) => void): void;
removeDisposeHandler(callback: (data: object) => void): void;
invalidate(): void;
addStatusHandler(callback: (status: HotUpdateStatus) => void): void;
removeStatusHandler(callback: (status: HotUpdateStatus) => void): void;
data: object;
check(
autoApply?: boolean | ApplyOptions
): Promise<(string | number)[] | null>;
apply(options?: ApplyOptions): Promise<(string | number)[] | null>;
}
interface ExportInfo {
used: boolean;
provideInfo: boolean | null | undefined;
useInfo: boolean | null | undefined;
}
interface ExportsInfo {
[k: string]: ExportInfo & ExportsInfo;
}
interface Context {
resolve(dependency: string): string | number;
keys(): Array<string>;
id: string | number;
(dependency: string): unknown;
}
}
interface ImportMeta {
url: string;
webpack: number;
webpackHot: webpack.Hot;
}
declare const __resourceQuery: string;
declare var __webpack_public_path__: string;
declare var __webpack_nonce__: string;
declare const __webpack_chunkname__: string;
declare var __webpack_base_uri__: string;
declare var __webpack_runtime_id__: string;
declare const __webpack_hash__: string;
declare const __webpack_modules__: Record<string | number, NodeJS.Module>;
declare const __webpack_require__: (id: string | number) => unknown;
declare var __webpack_chunk_load__: (chunkId: string | number) => Promise<void>;
declare var __webpack_get_script_filename__: (
chunkId: string | number
) => string;
declare var __webpack_is_included__: (request: string) => boolean;
declare var __webpack_exports_info__: webpack.ExportsInfo;
declare const __webpack_share_scopes__: Record<
string,
Record<
string,
{ loaded?: 1; get: () => Promise<unknown>; from: string; eager: boolean }
>
>;
declare var __webpack_init_sharing__: (scope: string) => Promise<void>;
declare var __non_webpack_require__: (id: any) => unknown;
declare const __system_context__: object;
declare namespace NodeJS {
interface Module {
hot: webpack.Hot;
}
interface Require {
ensure(
dependencies: string[],
callback: (require: (module: string) => void) => void,
errorCallback?: (error: Error) => void,
chunkName?: string
): void;
context(
request: string,
includeSubdirectories?: boolean,
filter?: RegExp,
mode?: "sync" | "eager" | "weak" | "lazy" | "lazy-once"
): webpack.Context;
include(dependency: string): void;
resolveWeak(dependency: string): void;
onError?: (error: Error) => void;
}
}

View File

@ -131,8 +131,8 @@
"bin/",
"hot/",
"schemas/",
"client-types/",
"SECURITY.md",
"module.d.ts",
"types.d.ts"
],
"scripts": {
@ -154,17 +154,18 @@
"type-report": "rimraf coverage && yarn cover:types && yarn cover:report && open-cli coverage/lcov-report/index.html",
"pretest": "yarn lint",
"prelint": "yarn setup",
"lint": "yarn code-lint && yarn special-lint && yarn type-lint && yarn typings-lint && yarn yarn-lint && yarn pretty-lint && yarn spellcheck",
"lint": "yarn code-lint && yarn special-lint && yarn type-lint && yarn typings-test && yarn module-typings-test && yarn yarn-lint && yarn pretty-lint && yarn spellcheck",
"code-lint": "eslint . --ext '.js' --cache",
"type-lint": "tsc",
"typings-lint": "tsc -p tsconfig.test.json && tsc -p tsconfig.test.esm.json",
"typings-test": "tsc -p tsconfig.types.test.json",
"module-typings-test": "tsc -p tsconfig.module.test.json",
"spellcheck": "cspell \"{.github,benchmark,bin,examples,hot,lib,schemas,setup,tooling}/**/*.{md,yml,yaml,js,json}\" \"*.md\"",
"special-lint": "node node_modules/tooling/lockfile-lint && node node_modules/tooling/schemas-lint && node node_modules/tooling/inherit-types && node node_modules/tooling/format-schemas && node tooling/generate-runtime-code.js && node tooling/generate-wasm-code.js && node node_modules/tooling/format-file-header && node node_modules/tooling/compile-to-definitions && node node_modules/tooling/precompile-schemas && node node_modules/tooling/generate-types --no-template-literals",
"special-lint-fix": "node node_modules/tooling/inherit-types --write && node node_modules/tooling/format-schemas --write && node tooling/generate-runtime-code.js --write && node tooling/generate-wasm-code.js --write && node node_modules/tooling/format-file-header --write && node node_modules/tooling/compile-to-definitions --write && node node_modules/tooling/precompile-schemas --write && node node_modules/tooling/generate-types --no-template-literals --write",
"fix": "yarn code-lint --fix && yarn special-lint-fix && yarn pretty-lint-fix",
"prepare": "husky install",
"pretty-lint-base": "prettier \"*.{ts,json,yml,yaml,md}\" \"{setup,lib,bin,hot,benchmark,tooling,schemas}/**/*.json\" \"examples/*.md\"",
"pretty-lint-base-all": "yarn pretty-lint-base \"*.js\" \"{setup,lib,bin,hot,benchmark,tooling,schemas}/**/*.js\" \"client-types/*.ts\" \"test/*.js\" \"test/helpers/*.js\" \"test/{configCases,watchCases,statsCases,hotCases,benchmarkCases}/**/webpack.config.js\" \"examples/**/webpack.config.js\"",
"pretty-lint-base-all": "yarn pretty-lint-base \"*.js\" \"{setup,lib,bin,hot,benchmark,tooling,schemas}/**/*.js\" \"module.d.ts\" \"test/*.js\" \"test/helpers/*.js\" \"test/{configCases,watchCases,statsCases,hotCases,benchmarkCases}/**/webpack.config.js\" \"examples/**/webpack.config.js\"",
"pretty-lint-fix": "yarn pretty-lint-base-all --loglevel warn --write",
"pretty-lint": "yarn pretty-lint-base --check",
"yarn-lint": "yarn-deduplicate --fail --list -s highest yarn.lock",

View File

@ -1,13 +1,24 @@
require("./a");
require.include("./a");
require.resolveWeak("./a");
require.ensure(["./a"], (require) => {
require.ensure(["./a"], require => {
require("./b");
});
require.ensure(["./a"], (require) => {
require("./b");
}, err => {}, "name1");
require.ensure(
["./a"],
require => {
require("./b");
},
err => {},
"name1"
);
const context = require.context("ctx");
context.keys();
context.id;
context.resolve("./a");
require.context("ctx", true, /.*\.js/, 'sync');
require.context("ctx", true, /.*\.js/, "sync");
//@ts-expect-error
require(123);
//@ts-expect-error
require.include(123);

View File

@ -1,8 +1,18 @@
__webpack_hash__;
//@ts-expect-error
window.__webpack_hash__;
__webpack_require__(1);
//@ts-expect-error
global.__webpack_require__(1);
__webpack_require__("1");
//@ts-expect-error
global.__webpack_require__("1");
module.hot;
if (module.hot) {
}
__webpack_exports_info__.exportA.used;
__webpack_exports_info__.exportA.b.c.provideInfo;

14
tsconfig.module.test.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext", "dom"],
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": true,
"types": ["node", "./module"],
"esModuleInterop": true
},
"include": ["test/typesCases/**/*"]
}

View File

@ -1,16 +0,0 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "es2020",
"lib": ["es2017", "dom"],
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": false,
"noImplicitThis": true,
"alwaysStrict": true,
"types": ["node", "./client-types"],
"esModuleInterop": true
},
"include": ["test/typesCases/meta/*"]
}

View File

@ -9,17 +9,15 @@
"strict": false,
"noImplicitThis": true,
"alwaysStrict": true,
"types": ["node", "jest", "./client-types"],
"types": ["node", "jest"],
"esModuleInterop": true
},
"exclude": ["test/typesCases/meta/*"],
"include": [
"test/**/webpack.config.js",
"test/cases/**/*loader*.js",
"test/watchCases/**/*loader*.js",
"test/configCases/**/*loader*.js",
"test/hotCases/**/*loader*.js",
"test/typesCases/**/*.ts",
"declarations.test.d.ts"
]
}