Skip to content

Commit

Permalink
fix: parse app entry file & pass correct routerType to sandbox (#168)
Browse files Browse the repository at this point in the history
* fix: parse app entry file & pass correct routerType to sandbox

* fix: export entry module
  • Loading branch information
wwsun authored May 31, 2024
1 parent 253ceca commit 3f3981b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/helpers/ast/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,22 @@ export function traverseFile(ast: t.File) {
imports,
};
}

/**
* app.js 入口文件解析
* @param ast
*/
export function traverseEntryFile(ast: t.File) {
let appConfig: any;
traverse(ast, {
CallExpression(path) {
const { node } = path;
const calleeName = keyNode2value(node.callee) as string;
if (calleeName === 'runApp') {
appConfig = node2value(node.arguments[0]);
path.stop();
}
},
});
return appConfig;
}
18 changes: 18 additions & 0 deletions packages/core/src/models/entry-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { traverseEntryFile } from '../helpers';
import { IFileConfig } from '../types';
import { IWorkspace } from './interfaces';
import { TangoModule } from './module';

export class AppEntryModule extends TangoModule {
routerType: string;

constructor(workspace: IWorkspace, props: IFileConfig) {
super(workspace, props, false);
this.update(props.code, true, false);
}

_analysisAst() {
const config = traverseEntryFile(this.ast);
this.routerType = config?.router?.type;
}
}
1 change: 1 addition & 0 deletions packages/core/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './drag-source';
export * from './history';
export * from './file';
export * from './module';
export * from './entry-module';
export * from './route-module';
export * from './service-module';
export * from './store-module';
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TangoRouteModule } from './route-module';
import { TangoStoreModule } from './store-module';
import { TangoServiceModule } from './service-module';
import { IdGenerator } from '../helpers';
import { AppEntryModule } from './entry-module';

export interface IViewFile {
readonly workspace: IWorkspace;
Expand Down Expand Up @@ -152,6 +153,10 @@ export interface IWorkspace {
* 解析后的 tango.config.json 文件,如果要获取项目配置,推荐使用 projectConfig 获取
*/
tangoConfigJson: TangoJsonFile;
/**
* app.js 入口文件解析后的模块
*/
appEntryModule: AppEntryModule;
/**
* 解析后的路由模块
*/
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { TangoStoreEntryModule, TangoStoreModule } from './store-module';
import { TangoServiceModule } from './service-module';
import { TangoViewModule } from './view-module';
import { TangoComponentsEntryModule } from './component-module';
import { AppEntryModule } from './entry-module';

export interface IWorkspaceOptions {
/**
Expand Down Expand Up @@ -97,6 +98,11 @@ export class Workspace extends EventTarget implements IWorkspace {
*/
activeViewFile: string;

/**
* 应用入口模块
*/
appEntryModule: AppEntryModule;

/**
* 路由配置模块
*/
Expand Down Expand Up @@ -329,6 +335,9 @@ export class Workspace extends EventTarget implements IWorkspace {
* @param fileType 模块类型
*/
addFile(filename: string, code: string, fileType?: FileType) {
if (!fileType && filename === this.entry) {
fileType = FileType.AppEntryModule;
}
const moduleType = fileType || inferFileType(filename);
const props = {
filename,
Expand All @@ -338,6 +347,10 @@ export class Workspace extends EventTarget implements IWorkspace {

let module;
switch (moduleType) {
case FileType.AppEntryModule:
module = new AppEntryModule(this, props);
this.appEntryModule = module;
break;
case FileType.StoreEntryModule:
module = new TangoStoreEntryModule(this, props);
this.storeEntryModule = module;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type SimulatorMode = 'desktop' | 'tablet' | 'phone';
export enum FileType {
// js 文件
Module = 'module',
AppEntryModule = 'appEntryModule',
StoreEntryModule = 'storeEntryModule',
RouteModule = 'routeModule',
ServiceModule = 'serviceModule',
Expand Down
11 changes: 9 additions & 2 deletions packages/designer/src/sandbox/sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface CombinedSandboxRef {

const LANDING_PAGE_PATH = '/__background_landing_page__';

function fixRouterMode(routerType: string): CodeSandboxProps['routerMode'] {
return routerType === 'hash' ? 'hash' : undefined;
}

function useSandbox({
isPreview: isPreviewProp,
configFormatter,
Expand Down Expand Up @@ -105,23 +109,26 @@ function useSandbox({
// 根据当前 workspace 状态与组件传入的状态是否一致,控制是否需要切换到空白路由
const display = isActive ? 'block' : 'none';
const routePath = isActive ? startRoute || workspace.activeRoute : LANDING_PAGE_PATH;
const routerMode = fixRouterMode(workspace.appEntryModule?.routerType);

const sandboxProps = isPreview
? {
files,
eventHandlers: pick(dndHandlers, ['onTango']),
onMessage,
onLoad,
display,
startRoute: routePath,
onLoad,
routerMode,
}
: {
files,
eventHandlers: dndHandlers as any,
onMessage,
onLoad,
display,
startRoute: routePath,
onLoad,
routerMode,
};

return sandboxProps;
Expand Down

0 comments on commit 3f3981b

Please sign in to comment.