refactor: code

This commit is contained in:
alexander.akait 2024-10-31 18:18:08 +03:00
parent 5cbb321d6a
commit dd83eea98d
2 changed files with 178 additions and 153 deletions

View File

@ -57,9 +57,26 @@ const CssParser = require("./CssParser");
/** @typedef {import("../util/createHash").Algorithm} Algorithm */ /** @typedef {import("../util/createHash").Algorithm} Algorithm */
/** @typedef {import("../util/memoize")} Memoize */ /** @typedef {import("../util/memoize")} Memoize */
/**
* @typedef {object} RenderContext
* @property {Chunk} chunk the chunk
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {CodeGenerationResults} codeGenerationResults results of code generation
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {string} uniqueName the unique name
* @property {boolean} cssHeadDataCompression need compress
* @property {string} undoPath undo path to css file
* @property {CssModule[]} modules modules
*/
/** /**
* @typedef {object} ChunkRenderContext * @typedef {object} ChunkRenderContext
* @property {RuntimeTemplate} runtimeTemplate runtime template * @property {Chunk} chunk the chunk
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {CodeGenerationResults} codeGenerationResults results of code generation
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {string[]} metaData meta data for runtime
* @property {string} undoPath undo path to css file
*/ */
/** /**
@ -460,18 +477,20 @@ class CssModulesPlugin {
); );
result.push({ result.push({
render: () => render: () =>
this.renderChunk({ this.renderChunk(
chunk, {
chunkGraph, chunk,
codeGenerationResults, chunkGraph,
uniqueName: compilation.outputOptions.uniqueName, codeGenerationResults,
cssHeadDataCompression: uniqueName: compilation.outputOptions.uniqueName,
compilation.outputOptions.cssHeadDataCompression, cssHeadDataCompression:
undoPath, compilation.outputOptions.cssHeadDataCompression,
modules, undoPath,
runtimeTemplate, modules,
runtimeTemplate
},
hooks hooks
}), ),
filename, filename,
info, info,
identifier: `css${chunk.id}`, identifier: `css${chunk.id}`,
@ -703,27 +722,14 @@ class CssModulesPlugin {
} }
/** /**
* @param {object} options options * @param {CssModule} module css module
* @param {string[]} options.metaData meta data * @param {ChunkRenderContext} renderContext options object
* @param {string} options.undoPath undo path for public path auto * @param {CompilationHooks} hooks hooks
* @param {Chunk} options.chunk chunk
* @param {ChunkGraph} options.chunkGraph chunk graph
* @param {CodeGenerationResults} options.codeGenerationResults code generation results
* @param {CssModule} options.module css module
* @param {RuntimeTemplate} options.runtimeTemplate runtime template
* @param {CompilationHooks} options.hooks hooks
* @returns {Source} css module source * @returns {Source} css module source
*/ */
renderModule({ renderModule(module, renderContext, hooks) {
metaData, const { codeGenerationResults, chunk, undoPath, chunkGraph, metaData } =
undoPath, renderContext;
chunk,
chunkGraph,
codeGenerationResults,
module,
hooks,
runtimeTemplate
}) {
const codeGenResult = codeGenerationResults.get(module, chunk.runtime); const codeGenResult = codeGenerationResults.get(module, chunk.runtime);
const moduleSourceContent = const moduleSourceContent =
/** @type {Source} */ /** @type {Source} */
@ -837,53 +843,46 @@ class CssModulesPlugin {
}${esModule ? "&" : ""}${escapeCss(moduleId)}` }${esModule ? "&" : ""}${escapeCss(moduleId)}`
); );
return tryRunOrWebpackError( return tryRunOrWebpackError(
() => () => hooks.renderModulePackage.call(source, module, renderContext),
hooks.renderModulePackage.call(source, module, {
runtimeTemplate
}),
"CssModulesPlugin.getCompilationHooks().renderModulePackage" "CssModulesPlugin.getCompilationHooks().renderModulePackage"
); );
} }
/** /**
* @param {object} options options * @param {RenderContext} renderContext the render context
* @param {string | undefined} options.uniqueName unique name * @param {CompilationHooks} hooks hooks
* @param {boolean | undefined} options.cssHeadDataCompression compress css head data
* @param {string} options.undoPath undo path for public path auto
* @param {Chunk} options.chunk chunk
* @param {ChunkGraph} options.chunkGraph chunk graph
* @param {CodeGenerationResults} options.codeGenerationResults code generation results
* @param {CssModule[]} options.modules ordered css modules
* @param {RuntimeTemplate} options.runtimeTemplate runtime template
* @param {CompilationHooks} options.hooks hooks
* @returns {Source} generated source * @returns {Source} generated source
*/ */
renderChunk({ renderChunk(
uniqueName, {
cssHeadDataCompression, uniqueName,
undoPath, cssHeadDataCompression,
chunk, undoPath,
chunkGraph, chunk,
codeGenerationResults, chunkGraph,
modules, codeGenerationResults,
runtimeTemplate, modules,
runtimeTemplate
},
hooks hooks
}) { ) {
const source = new ConcatSource(); const source = new ConcatSource();
/** @type {string[]} */ /** @type {string[]} */
const metaData = []; const metaData = [];
for (const module of modules) { for (const module of modules) {
try { try {
const moduleSource = this.renderModule({ const moduleSource = this.renderModule(
metaData,
undoPath,
chunk,
chunkGraph,
codeGenerationResults,
module, module,
runtimeTemplate, {
metaData,
undoPath,
chunk,
chunkGraph,
codeGenerationResults,
runtimeTemplate
},
hooks hooks
}); );
source.add(moduleSource); source.add(moduleSource);
} catch (err) { } catch (err) {
/** @type {Error} */ /** @type {Error} */

206
types.d.ts vendored
View File

@ -141,11 +141,11 @@ declare class AbstractLibraryPlugin<T> {
): void; ): void;
embedInRuntimeBailout( embedInRuntimeBailout(
module: Module, module: Module,
renderContext: RenderContext, renderContext: RenderContextJavascriptModulesPlugin,
libraryContext: LibraryContext<T> libraryContext: LibraryContext<T>
): undefined | string; ): undefined | string;
strictRuntimeBailout( strictRuntimeBailout(
renderContext: RenderContext, renderContext: RenderContextJavascriptModulesPlugin,
libraryContext: LibraryContext<T> libraryContext: LibraryContext<T>
): undefined | string; ): undefined | string;
runtimeRequirements( runtimeRequirements(
@ -155,7 +155,7 @@ declare class AbstractLibraryPlugin<T> {
): void; ): void;
render( render(
source: Source, source: Source,
renderContext: RenderContext, renderContext: RenderContextJavascriptModulesPlugin,
libraryContext: LibraryContext<T> libraryContext: LibraryContext<T>
): Source; ): Source;
renderStartup( renderStartup(
@ -1474,9 +1474,34 @@ declare class ChunkPrefetchPreloadPlugin {
} }
declare interface ChunkRenderContextCssModulesPlugin { declare interface ChunkRenderContextCssModulesPlugin {
/** /**
* runtime template * the chunk
*/
chunk: Chunk;
/**
* the chunk graph
*/
chunkGraph: ChunkGraph;
/**
* results of code generation
*/
codeGenerationResults: CodeGenerationResults;
/**
* the runtime template
*/ */
runtimeTemplate: RuntimeTemplate; runtimeTemplate: RuntimeTemplate;
/**
* meta data for runtime
*/
metaData: string[];
/**
* undo path to css file
*/
undoPath: string;
} }
declare interface ChunkRenderContextJavascriptModulesPlugin { declare interface ChunkRenderContextJavascriptModulesPlugin {
/** /**
@ -1548,7 +1573,11 @@ declare abstract class ChunkTemplate {
options: options:
| string | string
| (TapOptions & { name: string } & IfSet<AdditionalOptions>), | (TapOptions & { name: string } & IfSet<AdditionalOptions>),
fn: (arg0: Source, arg1: ModuleTemplate, arg2: RenderContext) => Source fn: (
arg0: Source,
arg1: ModuleTemplate,
arg2: RenderContextJavascriptModulesPlugin
) => Source
) => void; ) => void;
}; };
render: { render: {
@ -1556,7 +1585,11 @@ declare abstract class ChunkTemplate {
options: options:
| string | string
| (TapOptions & { name: string } & IfSet<AdditionalOptions>), | (TapOptions & { name: string } & IfSet<AdditionalOptions>),
fn: (arg0: Source, arg1: ModuleTemplate, arg2: RenderContext) => Source fn: (
arg0: Source,
arg1: ModuleTemplate,
arg2: RenderContextJavascriptModulesPlugin
) => Source
) => void; ) => void;
}; };
renderWithEntry: { renderWithEntry: {
@ -2311,20 +2344,33 @@ declare interface CompilationHooksJavascriptModulesPlugin {
renderModulePackage: SyncWaterfallHook< renderModulePackage: SyncWaterfallHook<
[Source, Module, ChunkRenderContextJavascriptModulesPlugin] [Source, Module, ChunkRenderContextJavascriptModulesPlugin]
>; >;
renderChunk: SyncWaterfallHook<[Source, RenderContext]>; renderChunk: SyncWaterfallHook<
renderMain: SyncWaterfallHook<[Source, RenderContext]>; [Source, RenderContextJavascriptModulesPlugin]
renderContent: SyncWaterfallHook<[Source, RenderContext]>; >;
render: SyncWaterfallHook<[Source, RenderContext]>; renderMain: SyncWaterfallHook<[Source, RenderContextJavascriptModulesPlugin]>;
renderContent: SyncWaterfallHook<
[Source, RenderContextJavascriptModulesPlugin]
>;
render: SyncWaterfallHook<[Source, RenderContextJavascriptModulesPlugin]>;
renderStartup: SyncWaterfallHook<[Source, Module, StartupRenderContext]>; renderStartup: SyncWaterfallHook<[Source, Module, StartupRenderContext]>;
renderRequire: SyncWaterfallHook<[string, RenderBootstrapContext]>; renderRequire: SyncWaterfallHook<[string, RenderBootstrapContext]>;
inlineInRuntimeBailout: SyncBailHook< inlineInRuntimeBailout: SyncBailHook<
[Module, RenderBootstrapContext], [Module, RenderBootstrapContext],
string | void string | void
>; >;
embedInRuntimeBailout: SyncBailHook<[Module, RenderContext], string | void>; embedInRuntimeBailout: SyncBailHook<
strictRuntimeBailout: SyncBailHook<[RenderContext], string | void>; [Module, RenderContextJavascriptModulesPlugin],
string | void
>;
strictRuntimeBailout: SyncBailHook<
[RenderContextJavascriptModulesPlugin],
string | void
>;
chunkHash: SyncHook<[Chunk, Hash, ChunkHashContext]>; chunkHash: SyncHook<[Chunk, Hash, ChunkHashContext]>;
useSourceMap: SyncBailHook<[Chunk, RenderContext], boolean | void>; useSourceMap: SyncBailHook<
[Chunk, RenderContextJavascriptModulesPlugin],
boolean | void
>;
} }
declare interface CompilationHooksModuleFederationPlugin { declare interface CompilationHooksModuleFederationPlugin {
addContainerEntryDependency: SyncHook<Dependency>; addContainerEntryDependency: SyncHook<Dependency>;
@ -3244,78 +3290,15 @@ declare class CssModulesPlugin {
chunkGraph: ChunkGraph, chunkGraph: ChunkGraph,
compilation: Compilation compilation: Compilation
): Module[]; ): Module[];
renderModule(__0: { renderModule(
/** module: CssModule,
* meta data renderContext: ChunkRenderContextCssModulesPlugin,
*/ hooks: CompilationHooksCssModulesPlugin
metaData: string[]; ): Source;
/** renderChunk(
* undo path for public path auto __0: RenderContextCssModulesPlugin,
*/ hooks: CompilationHooksCssModulesPlugin
undoPath: string; ): Source;
/**
* chunk
*/
chunk: Chunk;
/**
* chunk graph
*/
chunkGraph: ChunkGraph;
/**
* code generation results
*/
codeGenerationResults: CodeGenerationResults;
/**
* css module
*/
module: CssModule;
/**
* runtime template
*/
runtimeTemplate: RuntimeTemplate;
/**
* hooks
*/
hooks: CompilationHooksCssModulesPlugin;
}): Source;
renderChunk(__0: {
/**
* unique name
*/
uniqueName?: string;
/**
* compress css head data
*/
cssHeadDataCompression?: boolean;
/**
* undo path for public path auto
*/
undoPath: string;
/**
* chunk
*/
chunk: Chunk;
/**
* chunk graph
*/
chunkGraph: ChunkGraph;
/**
* code generation results
*/
codeGenerationResults: CodeGenerationResults;
/**
* ordered css modules
*/
modules: CssModule[];
/**
* runtime template
*/
runtimeTemplate: RuntimeTemplate;
/**
* hooks
*/
hooks: CompilationHooksCssModulesPlugin;
}): Source;
static getCompilationHooks( static getCompilationHooks(
compilation: Compilation compilation: Compilation
): CompilationHooksCssModulesPlugin; ): CompilationHooksCssModulesPlugin;
@ -5812,7 +5795,7 @@ declare class JavascriptModulesPlugin {
factory: boolean factory: boolean
): null | Source; ): null | Source;
renderChunk( renderChunk(
renderContext: RenderContext, renderContext: RenderContextJavascriptModulesPlugin,
hooks: CompilationHooksJavascriptModulesPlugin hooks: CompilationHooksJavascriptModulesPlugin
): Source; ): Source;
renderMain( renderMain(
@ -12134,7 +12117,48 @@ declare interface RenderBootstrapContext {
*/ */
hash: string; hash: string;
} }
declare interface RenderContext { declare interface RenderContextCssModulesPlugin {
/**
* the chunk
*/
chunk: Chunk;
/**
* the chunk graph
*/
chunkGraph: ChunkGraph;
/**
* results of code generation
*/
codeGenerationResults: CodeGenerationResults;
/**
* the runtime template
*/
runtimeTemplate: RuntimeTemplate;
/**
* the unique name
*/
uniqueName: string;
/**
* need compress
*/
cssHeadDataCompression: boolean;
/**
* undo path to css file
*/
undoPath: string;
/**
* modules
*/
modules: CssModule[];
}
declare interface RenderContextJavascriptModulesPlugin {
/** /**
* the chunk * the chunk
*/ */
@ -14242,7 +14266,9 @@ declare abstract class StackedMap<K, V> {
get size(): number; get size(): number;
createChild(): StackedMap<K, V>; createChild(): StackedMap<K, V>;
} }
type StartupRenderContext = RenderContext & { inlined: boolean }; type StartupRenderContext = RenderContextJavascriptModulesPlugin & {
inlined: boolean;
};
declare interface StatFs { declare interface StatFs {
( (
path: PathLikeFs, path: PathLikeFs,
@ -15022,13 +15048,13 @@ declare class Template {
): null | Source; ): null | Source;
static renderRuntimeModules( static renderRuntimeModules(
runtimeModules: RuntimeModule[], runtimeModules: RuntimeModule[],
renderContext: RenderContext & { renderContext: RenderContextJavascriptModulesPlugin & {
codeGenerationResults?: CodeGenerationResults; codeGenerationResults?: CodeGenerationResults;
} }
): Source; ): Source;
static renderChunkRuntimeModules( static renderChunkRuntimeModules(
runtimeModules: RuntimeModule[], runtimeModules: RuntimeModule[],
renderContext: RenderContext renderContext: RenderContextJavascriptModulesPlugin
): Source; ): Source;
static NUMBER_OF_IDENTIFIER_START_CHARS: number; static NUMBER_OF_IDENTIFIER_START_CHARS: number;
static NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS: number; static NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS: number;