diff --git a/examples/basic-store/src/pages/About/index.tsx b/examples/basic-store/src/pages/About/index.tsx index 363d06bb3e..ead9604f27 100644 --- a/examples/basic-store/src/pages/About/index.tsx +++ b/examples/basic-store/src/pages/About/index.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from 'react'; -import { Link, store as appStore } from 'ice'; +import { Link, store as appStore } from 'ice'; import { store as pageStore } from 'ice/About'; const About = (props) => { diff --git a/examples/basic-store/src/pages/Detail/index.tsx b/examples/basic-store/src/pages/Detail/index.tsx new file mode 100644 index 0000000000..594697e3e8 --- /dev/null +++ b/examples/basic-store/src/pages/Detail/index.tsx @@ -0,0 +1,23 @@ +import React from 'react'; +import { store } from 'ice/Detail'; + +class Detail extends React.Component { + constructor(props) { + super(props); + console.log(this.props); + } + + public render() { + const { default: detailStore } = this.props as any; + const [state, dispatcher] = detailStore; + return ( + <> +
Detail Page
+
Title: {state.title}
+ + + ); + } +} + +export default store.withModel('default')(Detail); diff --git a/examples/basic-store/src/pages/Detail/model.ts b/examples/basic-store/src/pages/Detail/model.ts new file mode 100644 index 0000000000..f3b3e0b69b --- /dev/null +++ b/examples/basic-store/src/pages/Detail/model.ts @@ -0,0 +1,26 @@ +const delay = (time) => new Promise((resolve) => setTimeout(() => resolve(), time)); + + +export default { + state: { + title: 'detail' + }, + + reducers: { + update(prevState, payload) { + return { + ...prevState, + ...payload, + }; + }, + }, + + effects: (dispatch) => ({ + async updateDetailTitle(title: string) { + await delay(1000); + dispatch.default.update({ + title + }); + }, + }), +}; diff --git a/examples/basic-store/src/routes.ts b/examples/basic-store/src/routes.ts index cd22620339..99f602765a 100644 --- a/examples/basic-store/src/routes.ts +++ b/examples/basic-store/src/routes.ts @@ -4,6 +4,7 @@ import Layout from '@/layouts/index'; import Home from '@/pages/Home'; import About from '@/pages/About'; +import Detail from '@/pages/Detail'; import { NotFound } from '@/pages/NotFound'; import Index from '@/pages/Index'; @@ -25,6 +26,10 @@ export default [ path: '/about', component: About }, + { + path: '/detail', + component: Detail + }, { path: '/index', component: Index diff --git a/packages/icejs/package.json b/packages/icejs/package.json index 536c939df7..6df18cae58 100644 --- a/packages/icejs/package.json +++ b/packages/icejs/package.json @@ -1,6 +1,6 @@ { "name": "ice.js", - "version": "1.9.5", + "version": "1.9.6", "description": "command line interface and builtin plugin for icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", @@ -33,7 +33,7 @@ "build-plugin-ice-request": "1.7.2", "build-plugin-ice-router": "1.7.3", "build-plugin-ice-ssr": "1.7.3", - "build-plugin-ice-store": "1.7.2", + "build-plugin-ice-store": "1.7.3", "build-plugin-react-app": "1.7.6", "build-plugin-miniapp": "0.1.5", "create-cli-utils": "0.1.3", diff --git a/packages/plugin-store/package.json b/packages/plugin-store/package.json index 7cdcbcf39c..8f9a0972a5 100644 --- a/packages/plugin-store/package.json +++ b/packages/plugin-store/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-store", - "version": "1.7.2", + "version": "1.7.3", "description": "builtin `icestore` in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-store/src/generator.ts b/packages/plugin-store/src/generator.ts index 4686a1a346..296ea9924d 100644 --- a/packages/plugin-store/src/generator.ts +++ b/packages/plugin-store/src/generator.ts @@ -146,7 +146,7 @@ export default class Generator { const pageComponentName = `Page${pageName}`; const pageComponentRenderData = { isRax: this.isRax, - pageComponentImport: `import ${pageComponentName} from '${pageComponentSourcePath}'` , + pageComponentImport: `import ${pageComponentName} from '${pageComponentSourcePath}'`, pageComponentExport: pageComponentName, hasPageStore: false, pageStoreImport: existedStoreFile ? `import store from '${pageStoreFile}'` : 'import store from \'./store\'' @@ -156,12 +156,26 @@ export default class Generator { pageComponentRenderData.hasPageStore = true; } - this.renderFile(pageComponentTemplatePath, pageComponentTargetPath , pageComponentRenderData); + this.renderFile(pageComponentTemplatePath, pageComponentTargetPath, pageComponentRenderData); + } + + private renderPageIndex(params) { + const { pageName, pageModelsDir, pageModelFile } = params; + const pageIndexTemplatePath = path.join(__dirname, './template/pageIndex.ts.ejs'); + const pageComponentTargetPath = path.join(this.targetPath, 'pages', pageName, 'index.ts'); + + const existStore = fse.pathExistsSync(pageModelsDir) || fse.pathExistsSync(pageModelFile); + const pageComponentRenderData = { + pageImports: existStore ? 'import store from \'./store\'' : '', + pageExports: existStore ? ' store ' : '' + }; + + this.renderFile(pageIndexTemplatePath, pageComponentTargetPath, pageComponentRenderData); } private renderFile(templatePath: string, targetPath: string, extraData = {}) { const templateContent = fse.readFileSync(templatePath, 'utf-8'); - let content = ejs.render(templateContent, {...extraData}); + let content = ejs.render(templateContent, { ...extraData }); try { content = prettier.format(content, { parser: 'typescript', @@ -199,6 +213,9 @@ export default class Generator { // generate .ice/pages/${pageName}/${pageName}.tsx this.renderPageComponent(params); + + // generate .ice/pages/${pageName}/index.ts + this.renderPageIndex(params); }); } } diff --git a/packages/plugin-store/src/template/pageComponent.tsx.ejs b/packages/plugin-store/src/template/pageComponent.tsx.ejs index 9fcdb5109e..3934933c04 100644 --- a/packages/plugin-store/src/template/pageComponent.tsx.ejs +++ b/packages/plugin-store/src/template/pageComponent.tsx.ejs @@ -4,12 +4,13 @@ <% } else { %> import * as React from 'react'; <% } %> -<%- pageComponentImport %> <% if(hasPageStore) { %> <%- pageStoreImport %> <% } %> +<%- pageComponentImport %> + const PageComponentName = <%= pageComponentExport %>; <% if(hasPageStore) { %> diff --git a/packages/plugin-store/src/template/pageIndex.ts.ejs b/packages/plugin-store/src/template/pageIndex.ts.ejs new file mode 100644 index 0000000000..ef5555c407 --- /dev/null +++ b/packages/plugin-store/src/template/pageIndex.ts.ejs @@ -0,0 +1,5 @@ +<%- pageImports %> + +export { +<%= pageExports %> +} \ No newline at end of file diff --git a/packages/rax-app/package.json b/packages/rax-app/package.json index 804b8cd48e..45c2fe4da9 100644 --- a/packages/rax-app/package.json +++ b/packages/rax-app/package.json @@ -1,6 +1,6 @@ { "name": "rax-app", - "version": "3.0.0-7", + "version": "3.0.0-8", "description": "command line interface and builtin plugin for rax app", "author": "ice-admin@alibaba-inc.com", "homepage": "https://github.com/alibaba/ice#readme", @@ -16,7 +16,7 @@ "dependencies": { "@alib/build-scripts": "^0.1.24", "build-plugin-app-core": "0.1.15", - "build-plugin-ice-store": "1.7.2", + "build-plugin-ice-store": "1.7.3", "build-plugin-rax-app": "5.2.6", "chokidar": "^3.3.1", "commander": "^5.0.0",