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

Commit

Permalink
Release v1.1.9
Browse files Browse the repository at this point in the history
* fix: file path formats on Windows (#270)

* fix: compatible single file page (#275)

* v1.1.9
  • Loading branch information
chenbin92 authored Apr 23, 2020
1 parent 9520a33 commit b62582c
Show file tree
Hide file tree
Showing 22 changed files with 98 additions and 79 deletions.
3 changes: 2 additions & 1 deletion examples/basic-store/src/routes.ts
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down
3 changes: 3 additions & 0 deletions examples/icestark-child/build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"sourcemap": true,
"router": {
"lazy": true
},
"plugins": [
"build-plugin-icestark"
]
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.8",
"version": "1.1.9",
"npmClient": "yarn",
"useWorkspaces": true,
"packages": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ice.js",
"private": true,
"version": "1.1.7",
"version": "1.1.8",
"workspaces": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/create-ice/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-ice",
"version": "1.1.8",
"version": "1.1.9",
"description": "npm init ice",
"author": "[email protected]",
"homepage": "",
Expand Down
22 changes: 11 additions & 11 deletions packages/icejs/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-config",
"version": "1.1.8",
"version": "1.1.9",
"description": "Define application config in icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-core",
"version": "1.1.8",
"version": "1.1.9",
"description": "the core plugin for icejs.",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-helpers",
"version": "1.1.8",
"version": "1.1.9",
"description": "builtin helpers in icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-icestark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-icestark",
"version": "1.1.8",
"version": "1.1.9",
"description": "Easy use `icestark` in icejs.",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-logger",
"version": "1.1.8",
"version": "1.1.9",
"description": "builtin logger in icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-mpa/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-react-app/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"main": "src/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-rematch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-rematch",
"version": "1.1.8",
"version": "1.1.9",
"description": "Easy use `rematch` in icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-request/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-rml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-rml",
"version": "1.1.8",
"version": "1.1.9",
"description": "RML loader for icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-router",
"version": "1.1.8",
"version": "1.1.9",
"description": "build-plugin-ice-router",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-service",
"version": "1.1.8",
"version": "1.1.9",
"description": "service pulgin",
"author": "[email protected]",
"homepage": "https://github.com/ice-lab/icejs#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ssr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-ssr",
"version": "1.1.8",
"version": "1.1.9",
"description": "ssr plugin",
"author": "[email protected]",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-store",
"version": "1.1.8",
"version": "1.1.9",
"description": "builtin `icestore` in icejs",
"author": "[email protected]",
"homepage": "",
Expand Down
113 changes: 64 additions & 49 deletions packages/plugin-store/src/babelPluginReplacePath.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Expand All @@ -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;
}
}
}
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default async (api) => {
...(userConfig.babelPlugins as [] || []),
[
require.resolve('./babelPluginReplacePath'),
{ routesPath, alias: userConfig.alias }
{ routesPath, alias: userConfig.alias, applyMethod }
]
]
);
Expand Down

0 comments on commit b62582c

Please sign in to comment.