Skip to content

Commit

Permalink
feat(*): 添加 rollup 的 alias 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Jul 21, 2021
1 parent e3891ef commit 6c7df70
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
'new-cap': 0, // 函数首字母不能大写
indent: 0, // prettier 已经处理好了
'object-curly-spacing': 0, // prettier 自动格式化这个部分
'max-len': ['error', 120],
'max-len': 0,
'operator-linebreak': ['error', 'before'],
'quote-props': ['error', 'as-needed'],
'operator-linebreak': 0,
Expand Down
45 changes: 31 additions & 14 deletions FakeServer/FakeServer.drawio
Original file line number Diff line number Diff line change
@@ -1,50 +1,67 @@
<mxfile host="65bd71144e">
<diagram id="GILjV6VJQhHbRAu13tSy" name="第 1 页">
<mxGraphModel dx="926" dy="619" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<mxGraphModel dx="926" dy="644" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="8" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;" edge="1" parent="1" source="2" target="5">
<mxCell id="8" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;" parent="1" source="2" target="5" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="9" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;" edge="1" parent="1" source="2" target="6">
<mxCell id="9" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;" parent="1" source="2" target="6" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="10" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;strokeWidth=3;" edge="1" parent="1" source="2" target="7">
<mxCell id="10" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;strokeWidth=3;fillColor=#1ba1e2;strokeColor=#006EAF;" parent="1" source="2" target="7" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="2" value="Router" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#f8cecc;sketch=1;strokeColor=#b85450;fontStyle=1;fontFamily=Comic Sans MS;fontSize=24;" vertex="1" parent="1">
<mxCell id="2" value="Router" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#f8cecc;sketch=1;strokeColor=#b85450;fontStyle=1;fontFamily=Comic Sans MS;fontSize=24;" parent="1" vertex="1">
<mxGeometry x="420" y="210" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="4" style="edgeStyle=entityRelationEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;sketch=1;strokeWidth=3;" edge="1" parent="1" source="3" target="2">
<mxCell id="4" style="edgeStyle=entityRelationEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;sketch=1;strokeWidth=3;fillColor=#1ba1e2;strokeColor=#006EAF;" parent="1" source="3" target="2" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="3" value="AJAX&lt;br&gt;API" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#e1d5e7;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#9673a6;" vertex="1" parent="1">
<mxGeometry x="160" y="210" width="120" height="60" as="geometry"/>
<mxCell id="23" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;strokeWidth=3;fillColor=#d5e8d4;strokeColor=#82b366;" edge="1" parent="1" source="3" target="17">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="230" y="160"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="3" value="AJAX&lt;br&gt;API Wrapper" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#e1d5e7;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#9673a6;" parent="1" vertex="1">
<mxGeometry x="160" y="210" width="150" height="90" as="geometry"/>
</mxCell>
<mxCell id="5" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxCell id="5" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" parent="1" vertex="1">
<mxGeometry x="640" y="110" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="6" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxCell id="6" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" parent="1" vertex="1">
<mxGeometry x="640" y="210" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="15" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;elbow=vertical;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;strokeWidth=3;" edge="1" parent="1" source="7" target="11">
<mxCell id="15" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;elbow=vertical;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;strokeWidth=3;fillColor=#1ba1e2;strokeColor=#006EAF;" parent="1" source="7" target="11" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="7" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxCell id="7" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" parent="1" vertex="1">
<mxGeometry x="640" y="300" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="14" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;elbow=vertical;entryX=0.5;entryY=1;entryDx=0;entryDy=0;strokeWidth=3;" edge="1" parent="1" source="11" target="3">
<mxCell id="14" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;elbow=vertical;entryX=0.5;entryY=1;entryDx=0;entryDy=0;strokeWidth=3;fillColor=#d5e8d4;strokeColor=#82b366;" parent="1" source="11" target="3" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="720" y="460" as="targetPoint"/>
<Array as="points">
<mxPoint x="580" y="410"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="11" value="Main&lt;br&gt;&amp;nbsp;Callback" style="rhombus;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=-1;hachureGap=8;fillStyle=cross-hatch;fillColor=#d5e8d4;sketch=1;fontFamily=Comic Sans MS;fontSize=18;strokeColor=#82b366;" vertex="1" parent="1">
<mxCell id="11" value="Main&lt;br&gt;&amp;nbsp;Callback" style="rhombus;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=-1;hachureGap=8;fillStyle=cross-hatch;fillColor=#d5e8d4;sketch=1;fontFamily=Comic Sans MS;fontSize=18;strokeColor=#82b366;" parent="1" vertex="1">
<mxGeometry x="860" y="300" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="24" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;strokeWidth=3;fillColor=#1ba1e2;strokeColor=#006EAF;" edge="1" parent="1" source="17" target="3">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="90" y="230"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="17" value="True AJAX API" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#e1d5e7;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#9673a6;" vertex="1" parent="1">
<mxGeometry x="20" y="80" width="150" height="90" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
Expand Down
47 changes: 23 additions & 24 deletions FakeServer/parseURL.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -11,44 +11,43 @@
* Licensed under an MIT-style license.
*/
const key = [
"source",
"protocol",
"authority",
"userInfo",
"user",
"password",
"host",
"port",
"relative",
"path",
"directory",
"file",
"query",
"hash",
'source',
'protocol',
'authority',
'userInfo',
'user',
'password',
'host',
'port',
'relative',
'path',
'directory',
'file',
'query',
'hash',
]; // keys available to query
const parser = {
//less intuitive, more accurate to the specs
// less intuitive, more accurate to the specs
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
// more intuitive, fails on relative paths and deviates from specs
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,
};

function parseUri(url, strictMode = false) {
const res = parser[strictMode ? "strict" : "loose"].exec(decodeURI(url));
const res = parser[strictMode ? 'strict' : 'loose'].exec(decodeURI(url));

const uri = key.reduce((col, keyName, index) => {
col[keyName] = res[index] || "";
col[keyName] = res[index] || '';
return col;
}, {});

// build query and hash parameters
uri.queryParams = parseString(uri.query);

// compile a 'base' domain attribute
uri["base"] = uri.host
? (uri.protocol ? uri.protocol + "://" + uri.host : uri.host) +
(uri.port ? ":" + uri.port : "")
: "";
uri['base'] = uri.host
? (uri.protocol ? uri.protocol + '://' + uri.host : uri.host) + (uri.port ? ':' + uri.port : '')
: '';

return uri;
}
Expand All @@ -58,11 +57,11 @@ function parseString(queryString) {
.split(/&|;/)
.reduce(function (collection, pair) {
try {
pair = decodeURIComponent(pair.replace(/\+/g, " "));
pair = decodeURIComponent(pair.replace(/\+/g, ' '));
} catch (e) {
// ignore
}
let [key, value] = pair.split(/=/g);
const [key, value] = pair.split(/=/g);
collection[key] = value;
return collection;
}, {});
Expand Down
32 changes: 27 additions & 5 deletions FakeServer/router.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { map } from 'rxjs/operators';
import { of } from 'rxjs';
import { pathToRegexp } from 'path-to-regexp';
import { parseURL } from './parseURL.js';
function normalizeRouters(Routers) {
return Object.entries(Routers);
}
function createRegexp(path, config = {}) {
return pathToRegexp(path, [], config);
}
class Response {
constructor(path, options = {}) {}
send() {}
}

// 每一个 Route 只有一个 main 函数进行数据的返回
class Route {
constructor(path, cb) {}
constructor(pathRegexp, cb) {
this.query = cb;
}
use(plugin) {}
}
class Router {
Expand All @@ -24,8 +33,21 @@ class Router {
this.#RouteMatchers.push(pathRegexp);
return route;
}
deleteRouter() {}
enableRouter() {}
disableRouter() {}
receiveMessage(path, message) {}
receiveMessage(path, options = {}) {
const req = parseURL(path);
const res = new Response(path, options);
let target;
this.#RouteMatchers.some((i) => {
if (path.match(i)) {
target = this.#RouterMap.get(i);
return true;
}
return false;
});

if (target instanceof Router) {
return target.query(req, res);
} else {
}
}
}
Loading

0 comments on commit 6c7df70

Please sign in to comment.