Skip to content

Commit

Permalink
fix: adjust env to externals
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Jul 6, 2021
1 parent b118287 commit a6d8711
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dev/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GarfishInstance.run({
// disablePreloadApp: true,
sandbox: {
open: true,
snapshot: false,
snapshot: true,
modules: [],
},
});
Expand Down
2 changes: 1 addition & 1 deletion dev/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import App from './App';
import { setModuleConfig } from '@garfish/remote-module';

setModuleConfig({
env: { React },
externals: { React },
});
// setModuleAlias({
// testModule: 'http://localhost:3000/remoteComponent.js',
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/remote-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@garfish/remote-module';

setModuleConfig({
env: { React }, // Environment variables required by remoteModules
externals: { React }, // Environment variables required by remoteModules
alias: { Component: 'https://xx.js' },
});

Expand Down Expand Up @@ -59,7 +59,7 @@ Other usage
// Or
loadModule({
cache: true, // This will cache the module instance
env: { React },
externals: { React },
url: 'https://xx.js',
error: (err, info, alias) => {
console.error(err);
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/remote-module/src/actuator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export class Actuator {
private manager: ModuleManager;
public env: Record<string, any>;

constructor(manager: ModuleManager, env?: Record<string, any>) {
constructor(manager: ModuleManager, externals?: Record<string, any>) {
this.manager = manager;
this.env = {
exports: {},
module: null,
require: (key) => (env || {})[key] || moduleConfig.env[key],
require: (key) => (externals || {})[key] || moduleConfig.externals[key],
};
this.env.module = this.env;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/remote-module/src/apis/loadModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function loadModule(
options: ModuleInfo | string,
): Promise<Record<string, any> | null> {
const info = purifyOptions(options);
const { env, cache, version, url: originalUrl, error, adapter } = info;
const { cache, version, externals, url: originalUrl, error, adapter } = info;
const [url, segments] = processAlias(originalUrl);

assert(url, 'Missing url for loading remote module');
Expand All @@ -38,7 +38,7 @@ export function loadModule(
result = getValueInObject(module, segments);
} else {
const data = await loader.loadModule(url);
const actuator = new Actuator(data.resourceManager, env);
const actuator = new Actuator(data.resourceManager, externals);
let exports = actuator.execScript().exports;
if (typeof adapter === 'function') {
exports = adapter(exports);
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/remote-module/src/apis/loadModuleSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function loadModuleSync(
options: ModuleInfo | string,
): Record<string, any> {
const info = purifyOptions(options);
const { env, cache, version, url: originalUrl, error, adapter } = info;
const { cache, version, externals, url: originalUrl, error, adapter } = info;
const [url, segments] = processAlias(originalUrl);

assert(url, 'Missing url for loading remote module');
Expand All @@ -51,7 +51,7 @@ export function loadModuleSync(
);

try {
const actuator = new Actuator(manager, env);
const actuator = new Actuator(manager, externals);
let exports = actuator.execScript().exports;
if (typeof adapter === 'function') {
exports = adapter(exports);
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/remote-module/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ModuleInfo {
url: string;
cache?: boolean;
version?: string;
env?: Record<string, any>;
externals?: Record<string, any>;
error?: (err: Error, info: ModuleInfo, alias: string) => any;
adapter?: (cjsModule: Record<string, any>) => Record<string, any>;
}
Expand All @@ -24,7 +24,7 @@ export const moduleConfig: ModuleConfig = {
cache: true, // Default use cache
error: null,
adapter: null,
env: {
externals: {
loadModule, // Only `loadModule` is provided for use by remote modules
},
};
Expand All @@ -42,7 +42,7 @@ try {
currentApp = app;
}
if (isObject(externals)) {
moduleConfig.env = { ...externals };
moduleConfig.externals = { ...externals };
}
if (Array.isArray(remoteModulesCode)) {
resourcesStore = resourcesStore.concat(remoteModulesCode);
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/remote-module/src/garfishPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function GarfishRemoteModulePlugin() {
version: __VERSION__,
bootstrap() {
setModuleConfig({
env: Garfish.externals,
externals: Garfish.externals,
});
},
};
Expand Down

0 comments on commit a6d8711

Please sign in to comment.