Skip to content

Commit

Permalink
fix: path is not found (#1345)
Browse files Browse the repository at this point in the history
* fix: path is not found

* fix: path is not found
  • Loading branch information
SoloJiang authored Sep 12, 2019
1 parent dd6a0ae commit 0d60266
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
17 changes: 15 additions & 2 deletions packages/jsx-compiler/src/modules/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const SAFE_CREATE_APP = '__create_app__';
const SAFE_CREATE_COMPONENT = '__create_component__';
const SAFE_CREATE_PAGE = '__create_page__';
const SAFE_CREATE_STYLE = '__create_style__';
const SAFE_ROUTER_MAP = '__router_map__';

const USE_EFFECT = 'useEffect';
const USE_STATE = 'useState';
Expand Down Expand Up @@ -260,6 +261,20 @@ function addDefine(ast, type, outputPath, targetFileDir, userDefineType, eventHa
traverse(ast, {
Program(path) {
const localIdentifier = t.identifier(safeCreateInstanceId);
// Component(__create_component__(__class_def__));
const args = [t.identifier(EXPORTED_DEF)];

// Insert routerMap import declaration
if (type === 'app') {
path.node.body.unshift(
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(SAFE_ROUTER_MAP))],
t.stringLiteral('./routerMap')
)
);
// App(__create_app__(__class_def__, __router_map__));
args.push(t.identifier(SAFE_ROUTER_MAP));
}

// import { createComponent as __create_component__ } from "/__helpers/component";
const specifiers = [t.importSpecifier(localIdentifier, t.identifier(importedIdentifier))];
Expand Down Expand Up @@ -292,8 +307,6 @@ function addDefine(ast, type, outputPath, targetFileDir, userDefineType, eventHa
)
);

// Component(__create_component__(__class_def__));
const args = [t.identifier(EXPORTED_DEF)];
// __create_component__(__class_def__, { events: ['_e*']})
if (eventHandlers.length > 0) {
args.push(
Expand Down
1 change: 0 additions & 1 deletion packages/jsx-compiler/src/modules/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function transformList(ast, renderItemFunctions, adapter) {
|| innerPath.node.name === forIndex.name
&& !(t.isMemberExpression(innerPath.parent) && innerPath.parent.property !== innerPath.node)
) {
console.log(t.isMemberExpression(innerPath.parent));
innerPath.node.__listItem = {
jsxplus: false,
item: forItem.name
Expand Down
5 changes: 5 additions & 0 deletions packages/jsx2mp-loader/src/app-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ module.exports = function appLoader(content) {
const transformedAppConfig = transformAppConfig(entryPath, config);
writeFileSync(join(outputPath, 'app.js'), transformed.code);
writeJSONSync(join(outputPath, 'app.json'), transformedAppConfig, { spaces: 2 });
const routerMap = {};
config.routes.map(route => {
routerMap[route.path] = route.component;
});
writeFileSync(join(outputPath, 'routerMap.js'), `export default ${JSON.stringify(routerMap, null, 2)};`);

const appCssPath = join(outputPath, 'app.acss');
const appCss = transformed.style ? defaultStyle + transformed.style : defaultStyle;
Expand Down
5 changes: 3 additions & 2 deletions packages/jsx2mp-runtime/src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ function noop() {}
/**
* Bridge App definition.
* @param definedApp
* @param routerMap
* @return instance
*/
export function createApp(definedApp) {
const appProps = { routerConfig: null };
export function createApp(definedApp, routerMap) {
const appProps = { routerConfig: routerMap };
const appConfig = {
_updateData: noop,
_updateMethods: noop,
Expand Down
4 changes: 2 additions & 2 deletions packages/jsx2mp-runtime/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export function withRouter(Klass) {
* Navigate to given path.
*/
export function push(path) {
return navigateTo({ url: path });
return navigateTo({ url: `/${router.config[path]}` });
}

/**
* Navigate replace.
*/
export function replace(path) {
return redirectTo({ url: path });
return redirectTo({ url: `/${router.config[path]}` });
}

/**
Expand Down

0 comments on commit 0d60266

Please # to comment.