Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: path is not found #1345

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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