Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
feat: support register provider with mpa (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbin92 authored Apr 27, 2020
1 parent 78b2798 commit f094c7d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/basic-mpa/src/pages/Dashboard/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createApp, IAppConfig } from 'ice';
import Dashboard from './index';
import routes from './routes';

const appConfig: IAppConfig = {
router: {
routes: [{ path: '/', component: Dashboard }],
routes
},
};

Expand Down
3 changes: 3 additions & 0 deletions examples/basic-mpa/src/pages/Dashboard/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Index from './index';

export default [{ path: '/', component: Index }];
2 changes: 1 addition & 1 deletion packages/plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (api) => {

const iceTempPath = path.join(rootDir, '.ice');
setValue('ICE_TEMP', iceTempPath);
const tsEntryFiles = globby.sync(['src/app.@(ts?(x))'], { cwd: rootDir });
const tsEntryFiles = globby.sync(['src/app.@(ts?(x))', 'src/pages/*/app.@(ts?(x))'], { cwd: rootDir });
const projectType = tsEntryFiles.length ? 'ts' : 'js';
setValue('PROJECT_TYPE', projectType);

Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-store/src/babelPluginReplacePath.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as path from 'path';

// match:
// eg: src/pages/home | src/pages/home/index(.tsx|.jsx) | src/pages/index(.tsx|jsx)
const pathRegExp = /src\/pages\/\w+((.tsx|.jsx?)$|(\/index(.tsx|.jsx?))?$)/;
// eg: src/pages/home | src/pages/home/index | src/pages/home/index(.tsx|.jsx) | src/pages/index(.tsx|jsx)
const pathRegExp = /src\/pages\/\w+((.tsx|.jsx?)$|(\/index(.tsx|.jsx?)?)?$)/;

module.exports = ({ types: t }, { routesPath, alias, applyMethod }) => {
return {
visitor: {
ImportDeclaration(nodePath, state) {
const isRoutesFile = (routesPath === state.filename);
const isRoutesFile = routesPath.includes(state.filename);
if (isRoutesFile) {
const { source, specifiers } = nodePath.node;
// issue: https://github.com/ice-lab/icejs/issues/271
Expand All @@ -22,7 +22,7 @@ module.exports = ({ types: t }, { routesPath, alias, applyMethod }) => {
// default alias: import Home from '@/pages/Home';
// custom alias: import Home from '$pages/Home';
// relative path: import Home from '../pages/Home'
const newValue = formatPagePath({ routesPath, value, alias, applyMethod });
const newValue = formatPagePath({ routesPath: state.filename, value, alias, applyMethod });
// replace to: import Home from 'ice/pages/Home'
if (newValue) {
replaceWith(t, nodePath, newValue);
Expand All @@ -33,7 +33,7 @@ module.exports = ({ types: t }, { routesPath, alias, applyMethod }) => {
},

CallExpression(nodePath, state) {
const isRoutesFile = (routesPath === state.filename);
const isRoutesFile = routesPath.includes(state.filename);
if (isRoutesFile) {
if (t.isImport(nodePath.node.callee)) {
const args = nodePath.node.arguments;
Expand All @@ -46,7 +46,7 @@ module.exports = ({ types: t }, { routesPath, alias, applyMethod }) => {
// default alias: const Home = lazy(() => import('@/pages/Home'));
// custom alias: const Home = lazy(() => import('$pages/home));
// relative path: const Home = lazy(() => import('../pages/Home'));
const newValue = formatPagePath({ routesPath, value, alias, applyMethod });
const newValue = formatPagePath({ routesPath: state.filename, value, alias, applyMethod });
// replace to: const Home =lazy (() => import('ice/Home/Home'));
if (newValue) {
args[i].value = newValue;
Expand Down Expand Up @@ -120,7 +120,6 @@ function formatPagePath({ routesPath, value, alias, applyMethod }: IGetConfigRou
const [, , pageName] = matchedPagePath.split('/');
newValue = pageName ? `ice/${pageName}/${pageName}` : '';
}

return newValue;
}
}
Expand Down
18 changes: 16 additions & 2 deletions packages/plugin-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,33 @@ export default async (api) => {
// add babel plugins for ice lazy
const { configPath } = userConfig.router || {};
const { mpa: isMpa } = userConfig;
const { routesPath } = applyMethod('getRoutes', {
let { routesPath } = applyMethod('getRoutes', {
rootDir,
tempDir: targetPath,
configPath,
projectType,
isMpa
});

if (isMpa) {
const routesFile = `routes.${projectType}`;
const pagesPath = path.join(rootDir, 'src', 'pages');
const pages = applyMethod('getPages', rootDir);
const pagesRoutePath = pages.map(pageName => {
return path.join(pagesPath, pageName, routesFile);
});
routesPath = pagesRoutePath;
}
modifyUserConfig('babelPlugins',
[
...(userConfig.babelPlugins as [] || []),
[
require.resolve('./babelPluginReplacePath'),
{ routesPath, alias: userConfig.alias, applyMethod }
{
routesPath,
alias: userConfig.alias,
applyMethod
}
]
]
);
Expand Down

0 comments on commit f094c7d

Please sign in to comment.