Skip to content

Commit

Permalink
fix: type error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw committed May 17, 2021
1 parent 0b7f8a8 commit 6df97db
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 88 deletions.
176 changes: 88 additions & 88 deletions packages/runtime/cjs-external/src/index.ts
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);
// },
// };
// };
// }
1 change: 1 addition & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function run() {
const buildAll = async (targets) => {
for (const target of targets) {
// watch mode can't await
if (target.indexOf('hooks') !== -1) return;
if (watch) {
build(target);
} else {
Expand Down

0 comments on commit 6df97db

Please sign in to comment.