@farmfe/runtime
Advanced tools
Comparing version 0.9.3 to 0.10.0
# @farmfe/runtime | ||
## 0.10.0 | ||
### Minor Changes | ||
- 8f8366de: Release Farm 1.0-beta | ||
## 0.9.3 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "@farmfe/runtime", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "Runtime of Farm", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -13,2 +13,2 @@ import { ModuleSystem } from './module-system'; | ||
export { ModuleSystem, FarmRuntimePlugin }; | ||
export { ModuleSystem, FarmRuntimePlugin as Plugin }; |
@@ -72,3 +72,3 @@ import { Module } from './module'; | ||
this.dynamicModuleResourcesMap = {}; | ||
this.resourceLoader = new ResourceLoader(this.publicPaths); | ||
this.resourceLoader = new ResourceLoader(this, this.publicPaths); | ||
this.pluginContainer = new FarmRuntimePluginContainer([]); | ||
@@ -75,0 +75,0 @@ this.targetEnv = targetEnv; |
import { Module } from './module'; | ||
import type { ModuleSystem } from './module-system'; | ||
import type { Resource } from './resource-loader'; | ||
export interface ResourceLoadResult { | ||
success: boolean; | ||
retryWithDefaultResourceLoader?: boolean; | ||
err?: Error; | ||
} | ||
export interface FarmRuntimePlugin { | ||
@@ -17,2 +24,9 @@ // plugin name | ||
moduleNotFound?: (moduleId: string) => void | Promise<void>; | ||
// called when loading resources, custom your resource loading in this hook. | ||
// return { success: true } to indicate that this resources have been loaded successfully. | ||
// return { success: false, retryWithDefaultResourceLoader: true } to indicate that this resources have not been loaded successfully and should be retried with the default resource loader. | ||
loadResource?: ( | ||
resource: Resource, | ||
targetEnv: 'browser' | 'node' | ||
) => Promise<ResourceLoadResult>; | ||
} | ||
@@ -19,0 +33,0 @@ |
// using native ability to load resources if target env is node. | ||
import type { ModuleSystem } from './module-system'; | ||
import type { ResourceLoadResult } from './plugin'; | ||
export interface Resource { | ||
@@ -24,3 +27,3 @@ path: string; | ||
constructor(publicPaths: string[]) { | ||
constructor(private moduleSystem: ModuleSystem, publicPaths: string[]) { | ||
this.publicPaths = publicPaths; | ||
@@ -32,6 +35,27 @@ } | ||
if (!isBrowser) { | ||
if (resource.type === 'script') { | ||
return this._loadScript(`./${resource.path}`); | ||
} else if (resource.type === 'link') { | ||
return this._loadLink(`./${resource.path}`); | ||
const result = this.moduleSystem.pluginContainer.hookBail( | ||
'loadResource', | ||
resource | ||
); | ||
if (result) { | ||
return result.then((res: ResourceLoadResult) => { | ||
if (!res.success && res.retryWithDefaultResourceLoader) { | ||
if (resource.type === 'script') { | ||
return this._loadScript(`./${resource.path}`); | ||
} else if (resource.type === 'link') { | ||
return this._loadLink(`./${resource.path}`); | ||
} | ||
} else if (!res.success) { | ||
throw new Error( | ||
`[Farm] Failed to load resource: "${resource.path}, type: ${resource.type}". Original Error: ${res.err}` | ||
); | ||
} | ||
}); | ||
} else { | ||
if (resource.type === 'script') { | ||
return this._loadScript(`./${resource.path}`); | ||
} else if (resource.type === 'link') { | ||
return this._loadLink(`./${resource.path}`); | ||
} | ||
} | ||
@@ -51,2 +75,33 @@ } | ||
const result = this.moduleSystem.pluginContainer.hookBail( | ||
'loadResource', | ||
resource | ||
); | ||
if (result) { | ||
return result.then((res: ResourceLoadResult) => { | ||
if (res.success) { | ||
this.setLoadedResource(resource.path); | ||
} else if (res.retryWithDefaultResourceLoader) { | ||
return this._load(url, resource, index); | ||
} else { | ||
throw new Error( | ||
`[Farm] Failed to load resource: "${resource.path}, type: ${resource.type}". Original Error: ${res.err}` | ||
); | ||
} | ||
}); | ||
} else { | ||
return this._load(url, resource, index); | ||
} | ||
} | ||
setLoadedResource(path: string) { | ||
this._loadedResources[path] = true; | ||
} | ||
isResourceLoaded(path: string) { | ||
return this._loadedResources[path]; | ||
} | ||
private _load(url: string, resource: Resource, index: number): Promise<void> { | ||
let promise = Promise.resolve(); | ||
@@ -73,3 +128,3 @@ | ||
if (index < this.publicPaths.length) { | ||
return this.load(resource, index); | ||
return this._load(url, resource, index); | ||
} else { | ||
@@ -84,6 +139,2 @@ throw new Error( | ||
setLoadedResource(path: string) { | ||
this._loadedResources[path] = true; | ||
} | ||
private _loadScript(path: string): Promise<void> { | ||
@@ -90,0 +141,0 @@ if (!isBrowser) { |
24710
590