refactor: code

This commit is contained in:
alexander.akait 2024-03-05 17:40:46 +03:00
parent b23130013c
commit 44b4f96e2b
8 changed files with 41 additions and 24 deletions

View File

@ -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,

View File

@ -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);
}
);
},

View File

@ -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) => {

View File

@ -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;

View File

@ -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",

View File

@ -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;

10
types.d.ts vendored
View File

@ -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;

View File

@ -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"