import type { Compiler , LibraryOptions } from 'webpack' ; import { library , sources } from 'webpack' ; import ExportPropertyLibraryPlugin from 'webpack/lib/library/ExportPropertyLibraryPlugin' ; export default class LazyModuleInitialization extends library . AbstractLibraryPlugin < { name : string ; } > { constructor ( ) { super ( { pluginName : 'LazyModuleInitialization ' , type : 'lazy' , } ) ; } apply ( compiler ) { library . EnableLibraryPlugin . setEnabled ( compiler , 'lazy' ) ; new ExportPropertyLibraryPlugin ( { type : 'lazy' , nsObjectUsed : true , } ) . apply ( compiler ) ; super . apply ( compiler ) ; } parseOptions ( { name } ) { if ( ! name || typeof name !== 'string' ) { throw new Error ( 'Library name must be a non-empty string' ) ; } return { name , } ; } render ( source , context , { options : { name } } ) { return new sources . ConcatSource ( 'window.__externals = window.__externals || {};\n' , `Object.defineProperty(window.__externals, "${ name }", { get: function() {\n` , `var module = ` , source , ';\n' , `Object.defineProperty(window.__externals, "${ name }", { value: module });\n` , 'return module; }\n' , ', configurable: true })' ) ; } }