import { RuntimeModule , RuntimeGlobals } from 'webpack' ; import type webpack from 'webpack' ; import type { Compiler } from 'webpack' ; const PLUGIN_NAME = 'RuntimePathPlugin' ; interface Options { publicPath : string ; } /**
 * RuntimePathPlugin необходим для правильной генерации ссылок на ассеты (картинки и т.п. которые грузятся через file-loader или url-loader)
 */ export default class RuntimePathPlugin implements webpack . WebpackPluginInstance { protected options : Options ; constructor ( options ) { this . options = options ; } apply ( compiler ) { const publicPathEval = this . options . publicPath ; const compilerPublicPath = compiler . options . output . publicPath ?? '' ; const defaultPublicPath = compilerPublicPath === 'auto' ? compilerPublicPath : `"${ compilerPublicPath }"` ; compiler . hooks . thisCompilation . tap ( PLUGIN_NAME , ( compilation ) => { compilation . hooks . runtimeRequirementInTree . for ( RuntimeGlobals . publicPath ) . tap ( PLUGIN_NAME , ( chunk ) => { const isEntry = compilation . entries . has ( chunk . name . toString ( ) ) ; compilation . addRuntimeModule ( chunk , new ( class extends RuntimeModule { generate ( ) { return `${ RuntimeGlobals . publicPath } = ${ isEntry ? publicPathEval : defaultPublicPath };` ; } } as any ) ( 'runtime-path-module' ) ) ; return true ; } ) ; } ) ; } }