From 44b4f96e2be24003ac83689796e7668498992d1a Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 5 Mar 2024 17:40:46 +0300 Subject: [PATCH] refactor: code --- lib/APIPlugin.js | 12 ++++++++---- lib/LibManifestPlugin.js | 15 ++++++++------- lib/javascript/JavascriptParserHelpers.js | 2 +- lib/util/fs.js | 10 +++++----- package.json | 2 +- .../types/filesystems/webpack.config.js | 6 ++++++ types.d.ts | 10 ++++++++-- yarn.lock | 8 ++++---- 8 files changed, 41 insertions(+), 24 deletions(-) diff --git a/lib/APIPlugin.js b/lib/APIPlugin.js index cc13392b8..3d1384ba6 100644 --- a/lib/APIPlugin.js +++ b/lib/APIPlugin.js @@ -25,6 +25,7 @@ const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule"); /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */ +/** @typedef {import("./Module").BuildInfo} BuildInfo */ /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */ /** @typedef {import("./javascript/JavascriptParser").Range} Range */ @@ -187,7 +188,7 @@ class APIPlugin { hooks.renderModuleContent.tap( PLUGIN_NAME, (source, module, renderContext) => { - if (module.buildInfo.needCreateRequire) { + if (/** @type {BuildInfo} */ (module.buildInfo).needCreateRequire) { const chunkInitFragments = [ new InitFragment( 'import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";\n', @@ -214,7 +215,8 @@ class APIPlugin { const dep = toConstantDependency(parser, info.expr, info.req); if (key === "__non_webpack_require__" && this.options.module) { - parser.state.module.buildInfo.needCreateRequire = true; + /** @type {BuildInfo} */ + (parser.state.module.buildInfo).needCreateRequire = true; } return dep(expression); @@ -267,7 +269,8 @@ class APIPlugin { parser.hooks.expression .for("__webpack_module__.id") .tap(PLUGIN_NAME, expr => { - parser.state.module.buildInfo.moduleConcatenationBailout = + /** @type {BuildInfo} */ + (parser.state.module.buildInfo).moduleConcatenationBailout = "__webpack_module__.id"; const dep = new ConstDependency( parser.state.module.moduleArgument + ".id", @@ -282,7 +285,8 @@ class APIPlugin { parser.hooks.expression .for("__webpack_module__") .tap(PLUGIN_NAME, expr => { - parser.state.module.buildInfo.moduleConcatenationBailout = + /** @type {BuildInfo} */ + (parser.state.module.buildInfo).moduleConcatenationBailout = "__webpack_module__"; const dep = new ConstDependency( parser.state.module.moduleArgument, diff --git a/lib/LibManifestPlugin.js b/lib/LibManifestPlugin.js index e3d5c3ec3..527231aa4 100644 --- a/lib/LibManifestPlugin.js +++ b/lib/LibManifestPlugin.js @@ -12,6 +12,7 @@ const { compareModulesById } = require("./util/comparators"); const { dirname, mkdirp } = require("./util/fs"); /** @typedef {import("./Compiler")} Compiler */ +/** @typedef {import("./Compiler").IntermediateFileSystem} IntermediateFileSystem */ /** @typedef {import("./Module").BuildMeta} BuildMeta */ /** @@ -113,16 +114,16 @@ class LibManifestPlugin { ? JSON.stringify(manifest, null, 2) : JSON.stringify(manifest); const buffer = Buffer.from(manifestContent, "utf8"); + const intermediateFileSystem = + /** @type {IntermediateFileSystem} */ ( + compiler.intermediateFileSystem + ); mkdirp( - compiler.intermediateFileSystem, - dirname(compiler.intermediateFileSystem, targetPath), + intermediateFileSystem, + dirname(intermediateFileSystem, targetPath), err => { if (err) return callback(err); - compiler.intermediateFileSystem.writeFile( - targetPath, - buffer, - callback - ); + intermediateFileSystem.writeFile(targetPath, buffer, callback); } ); }, diff --git a/lib/javascript/JavascriptParserHelpers.js b/lib/javascript/JavascriptParserHelpers.js index 4d5298876..be85e9e14 100644 --- a/lib/javascript/JavascriptParserHelpers.js +++ b/lib/javascript/JavascriptParserHelpers.js @@ -18,7 +18,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** * @param {JavascriptParser} parser the parser * @param {string} value the const value - * @param {string[]=} runtimeRequirements runtime requirements + * @param {(string[] | null)=} runtimeRequirements runtime requirements * @returns {function(Expression): true} plugin function */ exports.toConstantDependency = (parser, value, runtimeRequirements) => { diff --git a/lib/util/fs.js b/lib/util/fs.js index 8e63ab7da..bc7ef2db7 100644 --- a/lib/util/fs.js +++ b/lib/util/fs.js @@ -74,7 +74,7 @@ const path = require("path"); /** @typedef {function(NodeJS.ErrnoException | null, IBigIntStats): void} BigIntStatsCallback */ /** @typedef {function(NodeJS.ErrnoException | null, IStats | IBigIntStats): void} StatsOrBigIntStatsCallback */ /** @typedef {function(NodeJS.ErrnoException | null, number): void} NumberCallback */ -/** @typedef {function(NodeJS.ErrnoException | Error | null, any): void} ReadJsonCallback */ +/** @typedef {function(NodeJS.ErrnoException | Error | null, any=): void} ReadJsonCallback */ /** * @typedef {Object} WatcherInfo @@ -128,7 +128,7 @@ const path = require("path"); * (path: PathOrFileDescriptor, options: ({ encoding?: null | undefined, flag?: string | undefined } & import("events").Abortable) | undefined | null, callback: BufferCallback): void; * (path: PathOrFileDescriptor, options: ({ encoding: BufferEncoding, flag?: string | undefined } & import("events").Abortable) | BufferEncoding, callback: StringCallback): void; * (path: PathOrFileDescriptor, options: (ObjectEncodingOptions & { flag?: string | undefined } & import("events").Abortable) | BufferEncoding | undefined | null, callback: StringOrBufferCallback): void; - * (path: PathOrFileDescriptor, BufferCallback): void; + * (path: PathOrFileDescriptor, callback: BufferCallback): void; * }} ReadFile */ @@ -164,7 +164,7 @@ const path = require("path"); * (path: PathLike, options: EncodingOption, callback: StringCallback): void; * (path: PathLike, options: BufferEncodingOption, callback: BufferCallback): void; * (path: PathLike, options: EncodingOption, callback: StringOrBufferCallback): void; - * (path: PathLike, StringCallback): void; + * (path: PathLike, callback: StringCallback): void; * }} Readlink */ @@ -564,7 +564,7 @@ const mkdirpSync = (fs, p) => { fs.mkdirSync(p); } catch (err) { if (err) { - if (err.code === "ENOENT") { + if (/** @type {NodeJS.ErrnoException} */ (err).code === "ENOENT") { const dir = dirname(fs, p); if (dir === p) { throw err; @@ -572,7 +572,7 @@ const mkdirpSync = (fs, p) => { mkdirpSync(fs, dir); fs.mkdirSync(p); return; - } else if (err.code === "EEXIST") { + } else if (/** @type {NodeJS.ErrnoException} */ (err).code === "EEXIST") { return; } throw err; diff --git a/package.json b/package.json index dfd679d12..61fbe93ad 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "lint-staged": "^13.2.1", "lodash": "^4.17.19", "lodash-es": "^4.17.15", - "memfs": "^3.5.0", + "memfs": "^3.6.0", "mini-css-extract-plugin": "^1.6.1", "mini-svg-data-uri": "^1.2.3", "nyc": "^15.1.0", diff --git a/test/configCases/types/filesystems/webpack.config.js b/test/configCases/types/filesystems/webpack.config.js index 2180a04e5..36c632723 100644 --- a/test/configCases/types/filesystems/webpack.config.js +++ b/test/configCases/types/filesystems/webpack.config.js @@ -5,8 +5,14 @@ const fs = require("fs"); module.exports = { plugins: [ compiler => { + // eslint-disable-next-line no-warning-comments + // @ts-ignore compiler.outputFileSystem = memfs.fs; + // eslint-disable-next-line no-warning-comments + // @ts-ignore compiler.inputFileSystem = memfs.fs; + // eslint-disable-next-line no-warning-comments + // @ts-ignore compiler.intermediateFileSystem = memfs.fs; compiler.outputFileSystem = fs; diff --git a/types.d.ts b/types.d.ts index e7589f119..e9563d294 100644 --- a/types.d.ts +++ b/types.d.ts @@ -10617,7 +10617,10 @@ declare interface ReadFile { arg1: string | Buffer ) => void ): void; - (path: PathOrFileDescriptor, BufferCallback?: any): void; + ( + path: PathOrFileDescriptor, + callback: (arg0: null | NodeJS.ErrnoException, arg1: Buffer) => void + ): void; } declare class ReadFileCompileWasmPlugin { constructor(options?: ReadFileCompileWasmPluginOptions); @@ -10855,7 +10858,10 @@ declare interface Readlink { arg1: string | Buffer ) => void ): void; - (path: PathLike, StringCallback?: any): void; + ( + path: PathLike, + callback: (arg0: null | NodeJS.ErrnoException, arg1: string) => void + ): void; } declare interface ReadlinkSync { (path: PathLike, options?: EncodingOption): string; diff --git a/yarn.lock b/yarn.lock index 21b232f20..ebfcbf82e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4533,10 +4533,10 @@ map-obj@^4.3.0: resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.3.0.tgz#9304f906e93faae70880da102a9f1df0ea8bb05a" integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== -memfs@^3.4.1, memfs@^3.5.0: - version "3.5.3" - resolved "https://registry.yarnpkg.com/memfs/-/memfs-3.5.3.tgz#d9b40fe4f8d5788c5f895bda804cd0d9eeee9f3b" - integrity sha512-UERzLsxzllchadvbPs5aolHh65ISpKpM+ccLbOJ8/vvpBKmAWf+la7dXFy7Mr0ySHbdHrFv5kGFCUHHe6GFEmw== +memfs@^3.4.1, memfs@^3.6.0: + version "3.6.0" + resolved "https://registry.yarnpkg.com/memfs/-/memfs-3.6.0.tgz#d7a2110f86f79dd950a8b6df6d57bc984aa185f6" + integrity sha512-EGowvkkgbMcIChjMTMkESFDbZeSh8xZ7kNSF0hAiAN4Jh6jgHCRS0Ga/+C8y6Au+oqpezRHCfPsmJ2+DwAgiwQ== dependencies: fs-monkey "^1.0.4"