feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -60,17 +60,14 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
`AMD library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
} else {
if (name && typeof name !== "string") {
throw new Error(
`AMD library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
} else if (name && typeof name !== "string") {
throw new Error(
`AMD library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
return {
name: /** @type {string} */ (name),
amdContainer: /** @type {string} */ (amdContainer)
};
const _name = /** @type {string} */ (name);
const _amdContainer = /** @type {string} */ (amdContainer);
return { name: _name, amdContainer: _amdContainer };
}
/**
@@ -146,13 +143,12 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
source,
`${fnEnd});`
);
} else {
return new ConcatSource(
`${amdContainerPrefix}define(${fnStart}`,
source,
`${fnEnd});`
);
}
return new ConcatSource(
`${amdContainerPrefix}define(${fnStart}`,
source,
`${fnEnd});`
);
}
/**

View File

@@ -36,9 +36,8 @@ const IDENTIFIER_REGEX =
* @param {string} name name to be validated
* @returns {boolean} true, when valid
*/
const isNameValid = name => {
return !KEYWORD_REGEX.test(name) && IDENTIFIER_REGEX.test(name);
};
const isNameValid = name =>
!KEYWORD_REGEX.test(name) && IDENTIFIER_REGEX.test(name);
/**
* @param {string[]} accessor variable plus properties
@@ -136,15 +135,14 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
`Library name must be a string or string array. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
} else {
if (name && typeof name !== "string" && !Array.isArray(name)) {
throw new Error(
`Library name must be a string, string array or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
} else if (name && typeof name !== "string" && !Array.isArray(name)) {
throw new Error(
`Library name must be a string, string array or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
const _name = /** @type {string | string[]} */ (name);
return {
name: /** @type {string | string[]} */ (name),
name: _name,
export: library.export
};
}

View File

@@ -54,8 +54,9 @@ class EnableLibraryPlugin {
"EnableLibraryPlugin need to be used to enable this type of library. " +
'This usually happens through the "output.enabledLibraryTypes" option. ' +
'If you are using a function as entry which sets "library", you need to add all potential library types to "output.enabledLibraryTypes". ' +
"These types are enabled: " +
Array.from(getEnabledTypes(compiler)).join(", ")
`These types are enabled: ${Array.from(
getEnabledTypes(compiler)
).join(", ")}`
);
}
}
@@ -84,7 +85,7 @@ class EnableLibraryPlugin {
};
switch (type) {
case "var": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -95,7 +96,7 @@ class EnableLibraryPlugin {
break;
}
case "assign-properties": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -107,7 +108,7 @@ class EnableLibraryPlugin {
break;
}
case "assign": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -118,7 +119,7 @@ class EnableLibraryPlugin {
break;
}
case "this": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -129,7 +130,7 @@ class EnableLibraryPlugin {
break;
}
case "window": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -140,7 +141,7 @@ class EnableLibraryPlugin {
break;
}
case "self": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -151,7 +152,7 @@ class EnableLibraryPlugin {
break;
}
case "global": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -162,7 +163,7 @@ class EnableLibraryPlugin {
break;
}
case "commonjs": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -173,7 +174,7 @@ class EnableLibraryPlugin {
break;
}
case "commonjs-static": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
@@ -185,7 +186,7 @@ class EnableLibraryPlugin {
}
case "commonjs2":
case "commonjs-module": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,

View File

@@ -55,7 +55,7 @@ class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
*/
parseOptions(library) {
return {
export: library.export
export: /** @type {string | string[]} */ (library.export)
};
}

View File

@@ -54,8 +54,9 @@ class JsonpLibraryPlugin extends AbstractLibraryPlugin {
`Jsonp library name must be a simple string. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
const _name = /** @type {string} */ (name);
return {
name: /** @type {string} */ (name)
name: _name
};
}

View File

@@ -16,6 +16,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
/** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
/** @typedef {import("../util/Hash")} Hash */
/** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
@@ -46,9 +47,7 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
compiler.hooks.compilation.tap("ModernModuleLibraryPlugin", compilation => {
const { exportsDefinitions } =
ConcatenatedModule.getCompilationHooks(compilation);
exportsDefinitions.tap("ModernModuleLibraryPlugin", () => {
return true;
});
exportsDefinitions.tap("ModernModuleLibraryPlugin", () => true);
});
}
@@ -73,8 +72,9 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
`Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
const _name = /** @type {string} */ (name);
return {
name: /** @type {string} */ (name)
name: _name
};
}
@@ -93,7 +93,9 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
) {
const result = new ConcatSource(source);
const exportsInfo = moduleGraph.getExportsInfo(module);
const definitions = module.buildMeta.exportsFinalName;
const definitions =
/** @type {BuildMeta} */
(module.buildMeta).exportsFinalName;
const exports = [];
for (const exportInfo of exportsInfo.orderedExports) {
@@ -106,7 +108,7 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
for (const reexportInfo of exp.orderedExports) {
if (
!reexportInfo.provided &&
reexportInfo.name === reexport.export[0]
reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
) {
shouldContinue = true;
}

View File

@@ -58,8 +58,9 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
`Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
const _name = /** @type {string} */ (name);
return {
name: /** @type {string} */ (name)
name: _name
};
}

View File

@@ -58,8 +58,9 @@ class SystemLibraryPlugin extends AbstractLibraryPlugin {
`System.js library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
);
}
const _name = /** @type {string} */ (name);
return {
name: /** @type {string} */ (name)
name: _name
};
}

View File

@@ -30,9 +30,8 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
* @param {string[]} accessor the accessor to convert to path
* @returns {string} the path
*/
const accessorToObjectAccess = accessor => {
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
};
const accessorToObjectAccess = accessor =>
accessor.map(a => `[${JSON.stringify(a)}]`).join("");
/**
* @param {string|undefined} base the path prefix
@@ -65,10 +64,10 @@ const accessorAccess = (base, accessor, joinWith = ", ") => {
/**
* @typedef {object} UmdLibraryPluginParsed
* @property {string | string[]} name
* @property {string | string[] | undefined} name
* @property {LibraryCustomUmdObject} names
* @property {string | LibraryCustomUmdCommentObject} auxiliaryComment
* @property {boolean} namedDefine
* @property {string | LibraryCustomUmdCommentObject | undefined} auxiliaryComment
* @property {boolean | undefined} namedDefine
*/
/**
@@ -93,7 +92,7 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
* @returns {T | false} preprocess as needed by overriding
*/
parseOptions(library) {
/** @type {LibraryName} */
/** @type {LibraryName | undefined} */
let name;
/** @type {LibraryCustomUmdObject} */
let names;
@@ -157,18 +156,17 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
* @param {string} str the string to replace
* @returns {string} the replaced keys
*/
const replaceKeys = str => {
return compilation.getPath(str, {
const replaceKeys = str =>
compilation.getPath(str, {
chunk
});
};
/**
* @param {ExternalModule[]} modules external modules
* @returns {string} result
*/
const externalsDepsArray = modules => {
return `[${replaceKeys(
const externalsDepsArray = modules =>
`[${replaceKeys(
modules
.map(m =>
JSON.stringify(
@@ -180,14 +178,13 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
)
.join(", ")
)}]`;
};
/**
* @param {ExternalModule[]} modules external modules
* @returns {string} result
*/
const externalsRootArray = modules => {
return replaceKeys(
const externalsRootArray = modules =>
replaceKeys(
modules
.map(m => {
let request = m.request;
@@ -195,18 +192,17 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
request =
/** @type {RequestRecord} */
(request).root;
return `root${accessorToObjectAccess([].concat(request))}`;
return `root${accessorToObjectAccess(/** @type {string[]} */ ([]).concat(request))}`;
})
.join(", ")
);
};
/**
* @param {string} type the type
* @returns {string} external require array
*/
const externalsRequireArray = type => {
return replaceKeys(
const externalsRequireArray = type =>
replaceKeys(
externals
.map(m => {
let expr;
@@ -218,16 +214,14 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
}
if (request === undefined) {
throw new Error(
"Missing external configuration for type:" + type
`Missing external configuration for type:${type}`
);
}
if (Array.isArray(request)) {
expr = `require(${JSON.stringify(
request[0]
)})${accessorToObjectAccess(request.slice(1))}`;
} else {
expr = `require(${JSON.stringify(request)})`;
}
expr = Array.isArray(request)
? `require(${JSON.stringify(
request[0]
)})${accessorToObjectAccess(request.slice(1))}`
: `require(${JSON.stringify(request)})`;
if (m.isOptional(moduleGraph)) {
expr = `(function webpackLoadOptionalExternalModule() { try { return ${expr}; } catch(e) {} }())`;
}
@@ -235,14 +229,13 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
})
.join(", ")
);
};
/**
* @param {ExternalModule[]} modules external modules
* @returns {string} arguments
*/
const externalsArguments = modules => {
return modules
const externalsArguments = modules =>
modules
.map(
m =>
`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
@@ -250,26 +243,27 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
)}__`
)
.join(", ");
};
/**
* @param {string| string[]} library library name
* @returns {string} stringified library name
*/
const libraryName = library => {
return JSON.stringify(
replaceKeys(/** @type {string[]} */ ([]).concat(library).pop())
const libraryName = library =>
JSON.stringify(
replaceKeys(
/** @type {string} */
(/** @type {string[]} */ ([]).concat(library).pop())
)
);
};
let amdFactory;
if (optionalExternals.length > 0) {
const wrapperArguments = externalsArguments(requiredExternals);
const factoryArguments =
requiredExternals.length > 0
? externalsArguments(requiredExternals) +
", " +
externalsRootArray(optionalExternals)
? `${externalsArguments(requiredExternals)}, ${externalsRootArray(
optionalExternals
)}`
: externalsRootArray(optionalExternals);
amdFactory =
`function webpackLoadOptionalExternalModuleAmd(${wrapperArguments}) {\n` +
@@ -288,72 +282,60 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
const getAuxiliaryComment = type => {
if (auxiliaryComment) {
if (typeof auxiliaryComment === "string")
return "\t//" + auxiliaryComment + "\n";
if (auxiliaryComment[type])
return "\t//" + auxiliaryComment[type] + "\n";
return `\t//${auxiliaryComment}\n`;
if (auxiliaryComment[type]) return `\t//${auxiliaryComment[type]}\n`;
}
return "";
};
return new ConcatSource(
new OriginalSource(
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
getAuxiliaryComment("commonjs2") +
" if(typeof exports === 'object' && typeof module === 'object')\n" +
" module.exports = factory(" +
externalsRequireArray("commonjs2") +
");\n" +
getAuxiliaryComment("amd") +
" else if(typeof define === 'function' && define.amd)\n" +
(requiredExternals.length > 0
? names.amd && namedDefine === true
? " define(" +
libraryName(names.amd) +
", " +
externalsDepsArray(requiredExternals) +
", " +
amdFactory +
");\n"
: " define(" +
externalsDepsArray(requiredExternals) +
", " +
amdFactory +
");\n"
: names.amd && namedDefine === true
? " define(" +
libraryName(names.amd) +
", [], " +
amdFactory +
");\n"
: " define([], " + amdFactory + ");\n") +
(names.root || names.commonjs
? getAuxiliaryComment("commonjs") +
" else if(typeof exports === 'object')\n" +
" exports[" +
libraryName(names.commonjs || names.root) +
"] = factory(" +
externalsRequireArray("commonjs") +
");\n" +
getAuxiliaryComment("root") +
" else\n" +
" " +
replaceKeys(
accessorAccess("root", names.root || names.commonjs)
) +
" = factory(" +
externalsRootArray(externals) +
");\n"
: " else {\n" +
(externals.length > 0
? " var a = typeof exports === 'object' ? factory(" +
externalsRequireArray("commonjs") +
") : factory(" +
externalsRootArray(externals) +
");\n"
: " var a = factory();\n") +
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
" }\n") +
`})(${runtimeTemplate.outputOptions.globalObject}, ${
`(function webpackUniversalModuleDefinition(root, factory) {\n${getAuxiliaryComment(
"commonjs2"
)} if(typeof exports === 'object' && typeof module === 'object')\n` +
` module.exports = factory(${externalsRequireArray(
"commonjs2"
)});\n${getAuxiliaryComment(
"amd"
)} else if(typeof define === 'function' && define.amd)\n${
requiredExternals.length > 0
? names.amd && namedDefine === true
? ` define(${libraryName(names.amd)}, ${externalsDepsArray(
requiredExternals
)}, ${amdFactory});\n`
: ` define(${externalsDepsArray(requiredExternals)}, ${
amdFactory
});\n`
: names.amd && namedDefine === true
? ` define(${libraryName(names.amd)}, [], ${amdFactory});\n`
: ` define([], ${amdFactory});\n`
}${
names.root || names.commonjs
? `${getAuxiliaryComment(
"commonjs"
)} else if(typeof exports === 'object')\n` +
` exports[${libraryName(
/** @type {string | string[]} */
(names.commonjs || names.root)
)}] = factory(${externalsRequireArray(
"commonjs"
)});\n${getAuxiliaryComment("root")} else\n` +
` ${replaceKeys(
accessorAccess(
"root",
/** @type {string | string[]} */
(names.root || names.commonjs)
)
)} = factory(${externalsRootArray(externals)});\n`
: ` else {\n${
externals.length > 0
? ` var a = typeof exports === 'object' ? factory(${externalsRequireArray(
"commonjs"
)}) : factory(${externalsRootArray(externals)});\n`
: " var a = factory();\n"
} for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n` +
" }\n"
}})(${runtimeTemplate.outputOptions.globalObject}, ${
runtimeTemplate.supportsArrowFunction()
? `(${externalsArguments(externals)}) =>`
: `function(${externalsArguments(externals)})`