UNPKG

create-farm-plugin

Version:

use create-farm-plugin to create farm plugin

1 lines 8.05 kB
import __farmNodeModule from 'node:module';global.nodeRequire = __farmNodeModule.createRequire(import.meta.url);global['28d997dabae4ce50ab48ae4ea4490780'] = {__FARM_TARGET_ENV__: 'node'};const e="undefined"!=typeof globalThis?globalThis:window;class t{constructor(e,t){this.resource_pot="",this.id=e,this.exports={},this.meta={env:{}},this.require=t;}o(e,t,r){Object.defineProperty(e,t,{enumerable:!0,get:r});}d(e,t,r){this.o(e,t,function(){return r;});}_m(e){let t="__esModule";e[t]||Object.defineProperty(e,t,{value:!0});}_e(e,t){return Object.keys(t).forEach(function(r){"default"===r||Object.prototype.hasOwnProperty.call(e,r)||Object.defineProperty(e,r,{value:t[r],enumerable:!0,configurable:!0});}),t;}i(e){return e&&e.__esModule?e:{default:e};}_g(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(this._g=function(e){return e?r:t;})(e);}w(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var r=this._g(t);if(r&&r.has(e))return r.get(e);var i={__proto__:null},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(i,s,a):i[s]=e[s];}return i.default=e,r&&r.set(e,i),i;}_(e,t,r,i){this.d(e,t,r[i||t]);}p(e,t){for(let r of Object.keys(t)){let i=e[r];i&&!Object.prototype.hasOwnProperty.call(t,i)&&this.d(t,i,t[r]);}}f(e){return void 0!==e.default?e.default:e;}}class r{constructor(e){this.plugins=[],this.plugins=e;}hookSerial(e,...t){for(let r of this.plugins){let i=r[e];i&&i.apply(r,t);}}hookBail(e,...t){for(let r of this.plugins){let i=r[e];if(i){let e=i.apply(r,t);if(e)return e;}}}}const i=global["28d997dabae4ce50ab48ae4ea4490780"],o="undefined"!=typeof window?window:"undefined"!=typeof global?global:{},s=i.__FARM_TARGET_ENV__||"node",a="browser"===s&&o.document;class l{constructor(e,t){this.moduleSystem=e,this._loadedResources={},this._loadingResources={},this.publicPaths=t;}load(e,t=0){if(!a){let t=this.moduleSystem.pluginContainer.hookBail("loadResource",e);if(t)return t.then(t=>{if(!t.success&&t.retryWithDefaultResourceLoader){if(0===e.type)return this._loadScript(`./${e.path}`);if(1===e.type)return this._loadLink(`./${e.path}`);}else if(!t.success)throw Error(`[Farm] Failed to load resource: "${e.path}, type: ${e.type}". Original Error: ${t.err}`);});if(0===e.type)return this._loadScript(`./${e.path}`);if(1===e.type)return this._loadLink(`./${e.path}`);}let r=this.publicPaths[t],i=`${r.endsWith("/")?r.slice(0,-1):r}/${e.path}`;if(this._loadedResources[e.path])return Promise.resolve();if(this._loadingResources[e.path])return this._loadingResources[e.path];let o=this.moduleSystem.pluginContainer.hookBail("loadResource",e);return o?o.then(r=>{if(r.success)this.setLoadedResource(e.path);else if(r.retryWithDefaultResourceLoader)return this._load(i,e,t);else throw Error(`[Farm] Failed to load resource: "${e.path}, type: ${e.type}". Original Error: ${r.err}`);}):this._load(i,e,t);}setLoadedResource(e,t=!0){this._loadedResources[e]=t;}isResourceLoaded(e){return this._loadedResources[e];}_load(e,t,r){let i=Promise.resolve();return 0===t.type?i=this._loadScript(e):1===t.type&&(i=this._loadLink(e)),this._loadingResources[t.path]=i,i.then(()=>{this._loadedResources[t.path]=!0,this._loadingResources[t.path]=null;}).catch(i=>{if(console.warn(`[Farm] Failed to load resource "${e}" using publicPath: ${this.publicPaths[r]}`),++r<this.publicPaths.length)return this._load(e,t,r);throw this._loadingResources[t.path]=null,Error(`[Farm] Failed to load resource: "${t.path}, type: ${t.type}". ${i}`);}),i;}_loadScript(e){return import(e);}_loadLink(e){return Promise.resolve();}}class n{constructor(){this.dynamicResources=[],this.modules={},this.cache={},this.publicPaths=[],this.dynamicModuleResourcesMap={},this.resourceLoader=new l(this,this.publicPaths),this.pluginContainer=new r([]),this.targetEnv=s,this.externalModules={},this.reRegisterModules=!1;}require(e,r=!1){if(this.cache[e]&&!this.pluginContainer.hookBail("readModuleCache",this.cache[e])){let t=this.cache[e];return t.initializer||t.exports;}let i=this.modules[e];if(!i){if(this.externalModules[e]){let t=this.externalModules[e];return r&&t.default||t;}return("node"===this.targetEnv||!a)&&nodeRequire?nodeRequire(e):(this.pluginContainer.hookSerial("moduleNotFound",e),console.debug(`[Farm] Module "${e}" is not registered`),{});}let s=new t(e,this.require.bind(this));s.resource_pot=i.__farm_resource_pot__,this.pluginContainer.hookSerial("moduleCreated",s),this.cache[e]=s,o.require||(o.require=this.require.bind(this));let l=i(s,s.exports,this.require.bind(this),this.farmDynamicRequire.bind(this));return l&&l instanceof Promise?(s.initializer=l.then(()=>(this.pluginContainer.hookSerial("moduleInitialized",s),s.initializer=void 0,s.exports)),s.initializer):(this.pluginContainer.hookSerial("moduleInitialized",s),s.exports);}farmDynamicRequire(e){if(this.modules[e]){let t=this.require(e);return t.__farm_async?t.default:Promise.resolve(t);}return this.loadDynamicResources(e);}loadDynamicResourcesOnly(e,t=!1){let r=this.dynamicModuleResourcesMap[e].map(e=>this.dynamicResources[e]);if(!r||0===r.length)throw Error(`Dynamic imported module "${e}" does not belong to any resource`);return t&&this.clearCache(e),Promise.all(r.map(e=>{if(t){let t=this.resourceLoader.isResourceLoaded(e.path);if(this.resourceLoader.setLoadedResource(e.path,!1),t)return this.resourceLoader.load({...e,path:`${e.path}?t=${Date.now()}`});}return this.resourceLoader.load(e);}));}loadDynamicResources(e,t=!1){let r=this.dynamicModuleResourcesMap[e].map(e=>this.dynamicResources[e]);return this.loadDynamicResourcesOnly(e,t).then(()=>{if(r.every(e=>0!==e.type))return;if(!this.modules[e])throw Error(`Dynamic imported module "${e}" is not registered.`);let t=this.require(e);return t.__farm_async?t.default:t;}).catch(t=>{throw console.error(`[Farm] Error loading dynamic module "${e}"`,t),t;});}register(e,t){if(this.modules[e]&&!this.reRegisterModules){console.warn(`Module "${e}" has registered! It should not be registered twice`);return;}this.modules[e]=t;}update(e,t){this.modules[e]=t,this.clearCache(e);}delete(e){return!!this.modules[e]&&(this.clearCache(e),delete this.modules[e],!0);}getModuleUrl(e){let t=this.publicPaths[0]??"";return a?`${window.location.protocol}//${window.location.host}${t.endsWith("/")?t.slice(0,-1):t}/${this.modules[e].__farm_resource_pot__}`:this.modules[e].__farm_resource_pot__;}getCache(e){return this.cache[e];}clearCache(e){return!!this.cache[e]&&(delete this.cache[e],!0);}setInitialLoadedResources(e){for(let t of e)this.resourceLoader.setLoadedResource(t);}setDynamicModuleResourcesMap(e,t){this.dynamicResources=e,this.dynamicModuleResourcesMap=t;}setPublicPaths(e){this.publicPaths=e,this.resourceLoader.publicPaths=this.publicPaths;}setPlugins(e){this.pluginContainer.plugins=e;}addPlugin(e){this.pluginContainer.plugins.every(t=>t.name!==e.name)&&this.pluginContainer.plugins.push(e);}removePlugin(e){this.pluginContainer.plugins=this.pluginContainer.plugins.filter(t=>t.name!==e);}setExternalModules(e){Object.assign(this.externalModules,e||{});}bootstrap(){this.pluginContainer.hookSerial("bootstrap",this);}}i.__farm_module_system__=(function(){let e=new n;return function(){return e;};})()(),global["28d997dabae4ce50ab48ae4ea4490780"].__farm_module_system__.setPlugins([{name:"farm-runtime-import-meta",_moduleSystem:{},bootstrap(e){this._moduleSystem=e;},moduleCreated(t){let r;let i=this._moduleSystem.publicPaths?.[0]||"",o="node"===this._moduleSystem.targetEnv,{location:s}=e;try{r=(s?new URL(i,`${s.protocol}//${s.host}`):new URL(t.resource_pot)).pathname;}catch(e){r="/";}t.meta.env={...{NODE_ENV:"production",mode:"production"}??{},dev:"development"===process.env.NODE_ENV,prod:"production"===process.env.NODE_ENV,BASE_URL:r,SSR:o};let a=s?`${s.protocol}//${s.host}${i.replace(/\/$/,"")}/${t.id}?t=${Date.now()}`:t.resource_pot;t.meta.url=a;}}]);