diff --git a/examples/basic-store/src/routes.ts b/examples/basic-store/src/routes.ts index 64414f5..416471a 100644 --- a/examples/basic-store/src/routes.ts +++ b/examples/basic-store/src/routes.ts @@ -1,8 +1,9 @@ import { lazy } from 'ice'; +import Layout from '@/layouts/index'; // import Home from '@/pages/Home'; // import About from '@/pages/About'; -import Layout from '@/layouts/index'; +// import NotFound from '@/pages/NotFound'; const Home = lazy(() => import('@/pages/Home')); const About =lazy(() => import('@/pages/About')); diff --git a/examples/icestark-child/build.json b/examples/icestark-child/build.json index 83a20a1..574333a 100644 --- a/examples/icestark-child/build.json +++ b/examples/icestark-child/build.json @@ -1,5 +1,8 @@ { "sourcemap": true, + "router": { + "lazy": true + }, "plugins": [ "build-plugin-icestark" ] diff --git a/lerna.json b/lerna.json index 3701a03..f03bcd6 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "1.1.8", + "version": "1.1.9", "npmClient": "yarn", "useWorkspaces": true, "packages": [ diff --git a/package.json b/package.json index 3daa09a..fb8c99f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ice.js", "private": true, - "version": "1.1.7", + "version": "1.1.8", "workspaces": [ "packages/*" ], diff --git a/packages/create-ice/package.json b/packages/create-ice/package.json index 463ba5b..2f39c73 100644 --- a/packages/create-ice/package.json +++ b/packages/create-ice/package.json @@ -1,6 +1,6 @@ { "name": "create-ice", - "version": "1.1.8", + "version": "1.1.9", "description": "npm init ice", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/icejs/package.json b/packages/icejs/package.json index 37ed612..ab9c430 100644 --- a/packages/icejs/package.json +++ b/packages/icejs/package.json @@ -1,6 +1,6 @@ { "name": "ice.js", - "version": "1.1.8", + "version": "1.1.9", "description": "command line interface and builtin plugin for icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", @@ -21,16 +21,16 @@ }, "dependencies": { "@alib/build-scripts": "^0.1.13", - "build-plugin-ice-config": "1.1.8", - "build-plugin-ice-core": "1.1.8", - "build-plugin-ice-helpers": "1.1.8", - "build-plugin-ice-logger": "1.1.8", - "build-plugin-ice-mpa": "1.1.8", - "build-plugin-ice-request": "1.1.8", - "build-plugin-ice-router": "1.1.8", - "build-plugin-ice-ssr": "1.1.8", - "build-plugin-ice-store": "1.1.8", - "build-plugin-react-app": "1.1.8", + "build-plugin-ice-config": "1.1.9", + "build-plugin-ice-core": "1.1.9", + "build-plugin-ice-helpers": "1.1.9", + "build-plugin-ice-logger": "1.1.9", + "build-plugin-ice-mpa": "1.1.9", + "build-plugin-ice-request": "1.1.9", + "build-plugin-ice-router": "1.1.9", + "build-plugin-ice-ssr": "1.1.9", + "build-plugin-ice-store": "1.1.9", + "build-plugin-react-app": "1.1.9", "chokidar": "^3.3.1", "commander": "^5.0.0", "detect-port": "^1.3.0", diff --git a/packages/plugin-config/package.json b/packages/plugin-config/package.json index 632d465..e14c4e7 100644 --- a/packages/plugin-config/package.json +++ b/packages/plugin-config/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-config", - "version": "1.1.8", + "version": "1.1.9", "description": "Define application config in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-core/package.json b/packages/plugin-core/package.json index 50fa39b..474cad2 100644 --- a/packages/plugin-core/package.json +++ b/packages/plugin-core/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-core", - "version": "1.1.8", + "version": "1.1.9", "description": "the core plugin for icejs.", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-helpers/package.json b/packages/plugin-helpers/package.json index 0daed23..66d4042 100644 --- a/packages/plugin-helpers/package.json +++ b/packages/plugin-helpers/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-helpers", - "version": "1.1.8", + "version": "1.1.9", "description": "builtin helpers in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-icestark/package.json b/packages/plugin-icestark/package.json index 24008aa..173a94f 100644 --- a/packages/plugin-icestark/package.json +++ b/packages/plugin-icestark/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-icestark", - "version": "1.1.8", + "version": "1.1.9", "description": "Easy use `icestark` in icejs.", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-logger/package.json b/packages/plugin-logger/package.json index 29b1568..7c42eec 100644 --- a/packages/plugin-logger/package.json +++ b/packages/plugin-logger/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-logger", - "version": "1.1.8", + "version": "1.1.9", "description": "builtin logger in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-mpa/package.json b/packages/plugin-mpa/package.json index 565de8e..70f0dfa 100644 --- a/packages/plugin-mpa/package.json +++ b/packages/plugin-mpa/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-mpa", - "version": "1.1.8", + "version": "1.1.9", "description": "enable mpa project for icejs framework", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-react-app/package.json b/packages/plugin-react-app/package.json index becbbfa..9c2548a 100644 --- a/packages/plugin-react-app/package.json +++ b/packages/plugin-react-app/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-react-app", - "version": "1.1.8", + "version": "1.1.9", "description": "The basic webpack configuration for ice project", "author": "ice-admin@alibaba-inc.com", "main": "src/index.js", diff --git a/packages/plugin-rematch/package.json b/packages/plugin-rematch/package.json index 211fb85..9c34fdc 100644 --- a/packages/plugin-rematch/package.json +++ b/packages/plugin-rematch/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-rematch", - "version": "1.1.8", + "version": "1.1.9", "description": "Easy use `rematch` in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-request/package.json b/packages/plugin-request/package.json index d117960..ef6c9f3 100644 --- a/packages/plugin-request/package.json +++ b/packages/plugin-request/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-request", - "version": "1.1.8", + "version": "1.1.9", "description": "request for build-plugin-ice-request", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-rml/package.json b/packages/plugin-rml/package.json index 9824207..6ecf5ab 100644 --- a/packages/plugin-rml/package.json +++ b/packages/plugin-rml/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-rml", - "version": "1.1.8", + "version": "1.1.9", "description": "RML loader for icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-router/package.json b/packages/plugin-router/package.json index 76170b4..c817cd4 100644 --- a/packages/plugin-router/package.json +++ b/packages/plugin-router/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-router", - "version": "1.1.8", + "version": "1.1.9", "description": "build-plugin-ice-router", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-service/package.json b/packages/plugin-service/package.json index 8d256ee..fc4282d 100644 --- a/packages/plugin-service/package.json +++ b/packages/plugin-service/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-service", - "version": "1.1.8", + "version": "1.1.9", "description": "service pulgin", "author": "ice-admin@alibaba-inc.com", "homepage": "https://github.com/ice-lab/icejs#readme", diff --git a/packages/plugin-ssr/package.json b/packages/plugin-ssr/package.json index 36a5ad1..80e318e 100644 --- a/packages/plugin-ssr/package.json +++ b/packages/plugin-ssr/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-ssr", - "version": "1.1.8", + "version": "1.1.9", "description": "ssr plugin", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-store/package.json b/packages/plugin-store/package.json index 1e8aa50..5360b1d 100644 --- a/packages/plugin-store/package.json +++ b/packages/plugin-store/package.json @@ -1,6 +1,6 @@ { "name": "build-plugin-ice-store", - "version": "1.1.8", + "version": "1.1.9", "description": "builtin `icestore` in icejs", "author": "ice-admin@alibaba-inc.com", "homepage": "", diff --git a/packages/plugin-store/src/babelPluginReplacePath.ts b/packages/plugin-store/src/babelPluginReplacePath.ts index 912b925..8d5f40d 100644 --- a/packages/plugin-store/src/babelPluginReplacePath.ts +++ b/packages/plugin-store/src/babelPluginReplacePath.ts @@ -1,38 +1,29 @@ import * as path from 'path'; -module.exports = ({ types: t }, { routesPath, alias }) => { - // match: - // src/pages/home | src/pages/home/index | src/pages/home/index(.tsx|.jsx) - // src/pages/index | src/pages/home/index(.tsx|.jsx) - const regex = /src\/pages\/\w+((.tsx|.jsx?)$|(\/index(.tsx|.jsx?))?$)/; - const isConventionRouting = /\.ice\/routes\.ts/.test(routesPath); +// 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?))?$)/; +module.exports = ({ types: t }, { routesPath, alias, applyMethod }) => { return { visitor: { ImportDeclaration(nodePath, state) { const isRoutesFile = (routesPath === state.filename); if (isRoutesFile) { - let value = nodePath.node.source.value; - if (typeof value === 'string') { - // 约定式路由 + const { source } = nodePath.node; + if (t.isStringLiteral(source)) { + const { value } = source; + // 约定式路由: // e.g: import Home from '../src/pages/Home/index.tsx'; - if (isConventionRouting && value.startsWith('../src/pages')) { - const [, , , pageName] = value.split('/'); - // replace to: import Home from './pages/Home/Home' - value = `./pages/${pageName}/${pageName}`; - replaceWith(t, nodePath, value); - } else { - // 配置式路由 - // default alias: import Home from '@/pages/Home'; - // custom alias: import Home from '$pages/Home'; - // relative path: import Home from '../pages/Home' - const matchedPagePath = matchRelativePath(routesPath, value) || matchAliasPath(alias, value); - if (matchedPagePath && regex.test(matchedPagePath)) { - const [, , pageName] = matchedPagePath.split('/'); - // replace to: import Home from 'ice/Home/Home' - value = `ice/${pageName}/${pageName}`; - replaceWith(t, nodePath, value); - } + // e.g: import Index from '../src/pages/index.tsx; + // 配置式路由: + // 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 }); + // replace to: import Home from 'ice/pages/Home' + if (newValue) { + replaceWith(t, nodePath, newValue); } } } @@ -44,27 +35,18 @@ module.exports = ({ types: t }, { routesPath, alias }) => { if (t.isImport(nodePath.node.callee)) { const args = nodePath.node.arguments; for (let i = 0; i < args.length; i++) { - let value = args[i].value; + const value = args[i].value; if (typeof value === 'string') { - // 约定式路由 + // 约定式路由: // e.g: const Home = lazy(() => import(/* webpackChunkName: 'Home' */ '../src/pages/Home/index.tsx')); - if (isConventionRouting && value.startsWith('../src/pages')) { - const [, , , pageName] = value.split('/'); - // replace to: import Home from './pages/Home/Home' - value = `./pages/${pageName}/${pageName}`; - args[i].value = value; - } else { - // 配置式路由 - // 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 matchedPagePath = matchRelativePath(routesPath, value) || matchAliasPath(alias, value); - if (matchedPagePath && regex.test(matchedPagePath)) { - const [, , pageName] = matchedPagePath.split('/'); - // replace to: const Home =lazy (() => import('ice/Home/Home')); - value = `ice/${pageName}/${pageName}`; - args[i].value = value; - } + // 配置式路由: + // 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 }); + // replace to: const Home =lazy (() => import('ice/Home/Home')); + if (newValue) { + args[i].value = newValue; } } } @@ -79,11 +61,18 @@ interface IAlias { [key: string]: string; } +interface IGetConfigRoutePathParmas { + routesPath: string; + value: string; + alias: IAlias; + applyMethod: Function; +} + // enum alias: // case1: { "@": "./src", "@pages": "./src/pages" } // case2: { "@src": "./src", "@pages": "./src/pages" } // case3: { "@": "./src", "@/pages": "./src/pages" } -function matchAliasPath(alias: IAlias, value: string): string { +function matchAliasPath(alias: IAlias, value: string, applyMethod: Function): string { let aliasPath = ''; // use default alias if (!Object.keys(alias).length) { @@ -93,7 +82,7 @@ function matchAliasPath(alias: IAlias, value: string): string { Object.keys(alias).forEach(currKey => { if (value.startsWith(currKey)) { const [, ...args] = value.split(currKey); - const currAliasPath = path.join(alias[currKey], ...args); + const currAliasPath = applyMethod('formatPath', path.join(alias[currKey], ...args)); if (currAliasPath.includes('src/pages')) { aliasPath = currAliasPath; } @@ -102,14 +91,40 @@ function matchAliasPath(alias: IAlias, value: string): string { return aliasPath; } -function matchRelativePath(routesPath: string, value: string) { +/** + * 匹配配置式路由下使用的相对路径并返回相对的 src 的相对路径 + */ +function matchRelativePath(routesPath: string, value: string, applyMethod: Function): string { let relativePath = ''; if (/^(\.\/|\.{2}\/)/.test(value)) { - relativePath = path.relative(process.cwd(), path.join(routesPath, '..', value)); + relativePath = applyMethod('formatPath', + path.relative(process.cwd(), path.join(routesPath, '..', value)) + ); } return relativePath; } +/** + * 格式化路由的替换路径值 + */ +function formatPagePath({ routesPath, value, alias, applyMethod }: IGetConfigRoutePathParmas): string { + const matchedPagePath = matchRelativePath(routesPath, value, applyMethod) || matchAliasPath(alias, value, applyMethod); + if (matchedPagePath && pathRegExp.test(matchedPagePath)) { + let newValue = ''; + if (/src\/pages\/index(.tsx|.jsx?)$/.test(value)) { + newValue = value.replace(/\.{2}\/src\/pages/, './pages/index'); + } else { + const [, , pageName] = matchedPagePath.split('/'); + newValue = pageName ? `ice/${pageName}/${pageName}` : ''; + } + + return newValue; + } +} + +/** + * AST 节点替换 + */ function replaceWith(t, nodePath, value) { nodePath.replaceWith( t.ImportDeclaration( diff --git a/packages/plugin-store/src/index.ts b/packages/plugin-store/src/index.ts index 71590cc..194d7f9 100644 --- a/packages/plugin-store/src/index.ts +++ b/packages/plugin-store/src/index.ts @@ -39,7 +39,7 @@ export default async (api) => { ...(userConfig.babelPlugins as [] || []), [ require.resolve('./babelPluginReplacePath'), - { routesPath, alias: userConfig.alias } + { routesPath, alias: userConfig.alias, applyMethod } ] ] );