-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,95 @@ | ||
import Garfish, { interfaces } from '@garfish/core'; | ||
import { assert, warn } from '@garfish/utils'; | ||
import { App } from './app'; | ||
// import { interfaces } from '@garfish/core'; | ||
// import { assert, warn } from '@garfish/utils'; | ||
// import { App } from './app'; | ||
|
||
declare module '@garfish/core' { | ||
export default interface Garfish { | ||
setExternal: ( | ||
nameOrExtObj: string | Record<string, any>, | ||
value?: any, | ||
) => void; | ||
externals: Record<string, any>; | ||
} | ||
// declare module '@garfish/core' { | ||
// export default interface Garfish { | ||
// setExternal: ( | ||
// nameOrExtObj: string | Record<string, any>, | ||
// value?: any, | ||
// ) => void; | ||
// externals: Record<string, any>; | ||
// } | ||
|
||
export namespace interfaces { | ||
export interface App { | ||
name: string; | ||
appInfo: AppInfo; | ||
entryResManager: interfaces.HtmlResource; | ||
cjsModules: Record<string, any>; | ||
customExports: Record<string, any>; // If you don't want to use the CJS export, can use this | ||
mounted: boolean; | ||
appContainer: HTMLElement; | ||
provider: Provider; | ||
htmlNode: HTMLElement | ShadowRoot; | ||
isHtmlMode: boolean; | ||
strictIsolation: boolean; | ||
mount(): Promise<boolean>; | ||
unmount(): boolean; | ||
getExecScriptEnv(noEntry: boolean): Record<string, any>; | ||
execScript( | ||
code: string, | ||
env: Record<string, any>, | ||
url?: string, | ||
options?: { | ||
async?: boolean; | ||
noEntry?: boolean; | ||
}, | ||
): void; | ||
} | ||
} | ||
} | ||
// export namespace interfaces { | ||
// export interface App { | ||
// name: string; | ||
// appInfo: AppInfo; | ||
// entryResManager: interfaces.HtmlResource; | ||
// cjsModules: Record<string, any>; | ||
// customExports: Record<string, any>; // If you don't want to use the CJS export, can use this | ||
// mounted: boolean; | ||
// appContainer: HTMLElement; | ||
// provider: Provider; | ||
// htmlNode: HTMLElement | ShadowRoot; | ||
// isHtmlMode: boolean; | ||
// strictIsolation: boolean; | ||
// mount(): Promise<boolean>; | ||
// unmount(): boolean; | ||
// getExecScriptEnv(noEntry: boolean): Record<string, any>; | ||
// execScript( | ||
// code: string, | ||
// env: Record<string, any>, | ||
// url?: string, | ||
// options?: { | ||
// async?: boolean; | ||
// noEntry?: boolean; | ||
// }, | ||
// ): void; | ||
// } | ||
// } | ||
// } | ||
|
||
export type CustomerLoader = ( | ||
provider: interfaces.Provider, | ||
appInfo: interfaces.AppInfo, | ||
path: string, | ||
) => Promise<interfaces.LoaderResult | void> | interfaces.LoaderResult | void; | ||
// export type CustomerLoader = ( | ||
// provider: interfaces.Provider, | ||
// appInfo: interfaces.AppInfo, | ||
// path: string, | ||
// ) => Promise<interfaces.LoaderResult | void> | interfaces.LoaderResult | void; | ||
|
||
interface Options { | ||
customLoader?: CustomerLoader; | ||
} | ||
// interface Options { | ||
// customLoader?: CustomerLoader; | ||
// } | ||
|
||
export default function cjsApp( | ||
options: Options = { customLoader: () => null }, | ||
) { | ||
return function (Garfish: Garfish): interfaces.Plugin { | ||
Garfish.setExternal = setExternal; | ||
Garfish.externals = {}; | ||
// export default function cjsApp( | ||
// options: Options = { customLoader: () => null }, | ||
// ) { | ||
// return function (Garfish: interfaces.Garfish): interfaces.Plugin { | ||
// Garfish.setExternal = setExternal; | ||
// Garfish.externals = {}; | ||
|
||
function setExternal( | ||
nameOrExtObj: string | Record<string, any>, | ||
value?: any, | ||
) { | ||
assert(nameOrExtObj, 'Invalid parameter.'); | ||
if (typeof nameOrExtObj === 'object') { | ||
for (const key in nameOrExtObj) { | ||
if (Garfish.externals[key]) { | ||
__DEV__ && warn(`The "${key}" will be overwritten in external.`); | ||
} | ||
Garfish.externals[key] = nameOrExtObj[key]; | ||
} | ||
} else { | ||
Garfish.externals[nameOrExtObj] = value; | ||
} | ||
} | ||
// function setExternal( | ||
// nameOrExtObj: string | Record<string, any>, | ||
// value?: any, | ||
// ) { | ||
// assert(nameOrExtObj, 'Invalid parameter.'); | ||
// if (typeof nameOrExtObj === 'object') { | ||
// for (const key in nameOrExtObj) { | ||
// if (Garfish.externals[key]) { | ||
// __DEV__ && warn(`The "${key}" will be overwritten in external.`); | ||
// } | ||
// Garfish.externals[key] = nameOrExtObj[key]; | ||
// } | ||
// } else { | ||
// Garfish.externals[nameOrExtObj] = value; | ||
// } | ||
// } | ||
|
||
return { | ||
name: 'cjs-app', | ||
version: __VERSION__, | ||
initializeApp(context, appInfo, resource, ResourceModules, isHtmlModule) { | ||
const instance = new App( | ||
context, | ||
appInfo, | ||
resource, | ||
ResourceModules, | ||
isHtmlModule, | ||
options.customLoader, | ||
); | ||
instance.cjsModules.require = (name) => | ||
Garfish.externals && Garfish.externals[name]; | ||
return Promise.resolve(instance); | ||
}, | ||
}; | ||
}; | ||
} | ||
// return { | ||
// name: 'cjs-app', | ||
// version: __VERSION__, | ||
// initializeApp(context, appInfo, resource, ResourceModules, isHtmlModule) { | ||
// const instance = new App( | ||
// context, | ||
// appInfo, | ||
// resource, | ||
// ResourceModules, | ||
// isHtmlModule, | ||
// options.customLoader, | ||
// ); | ||
// instance.cjsModules.require = (name) => | ||
// Garfish.externals && Garfish.externals[name]; | ||
// return Promise.resolve(instance); | ||
// }, | ||
// }; | ||
// }; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters