import type { Compiler , LibraryOptions } from 'webpack' ; import { library , sources } from 'webpack' ; import ExportPropertyLibraryPlugin from 'webpack/lib/library/ExportPropertyLibraryPlugin' ; export interface LazyLibraryOptions { prefix ? : string ; } export class LazyLibraryInitialization extends library . AbstractLibraryPlugin < { name : string ; } > { private readonly prefix : string ; constructor ( { prefix } ) { super ( { pluginName : 'LazyLibraryInitialization ' , type : 'lazy' , } ) ; this . prefix = prefix ?? '' ; } 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["${ this . prefix }__" + document.currentScript.src] = (function() {
  var result = {
    exports: null,
    initialized: false,
    initialize: function (require) {
      result.exports =` , source , `;
      result.initialized = true;
      result.initialize = function (module, ) {
        return result.exports;
      };

      return result.exports;
    }
  };

  return result;
})();
` ) ; } }