swico 0.5.0-beta9 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swico",
3
- "version": "0.5.0-beta9",
3
+ "version": "0.5.1",
4
4
  "description": "Simple, efficient, and practical front-end framework",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -10,7 +10,7 @@ export declare const Link: FC<{
10
10
  children: ReactNode;
11
11
  }>;
12
12
  export { Outlet } from 'react-router-dom';
13
- export { useLocation } from './template/hooks/react';
13
+ export { useLocation } from './template/react/hooks';
14
14
  export interface DefineSwicoConfigType {
15
15
  (env: 'base', config: CustomConfigType['base']): any;
16
16
  (env: 'dev', config: CustomConfigType['dev']): any;
@@ -13,8 +13,8 @@ var Link = function (_a) {
13
13
  exports.Link = Link;
14
14
  var react_router_dom_2 = require("react-router-dom");
15
15
  Object.defineProperty(exports, "Outlet", { enumerable: true, get: function () { return react_router_dom_2.Outlet; } });
16
- var react_2 = require("./template/hooks/react");
17
- Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return react_2.useLocation; } });
16
+ var hooks_1 = require("./template/react/hooks");
17
+ Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return hooks_1.useLocation; } });
18
18
  var defineConfig = function (env, config) { return config; };
19
19
  exports.defineConfig = defineConfig;
20
20
  var defineGlobal = function (config) {
@@ -3,7 +3,7 @@ import { App } from 'vue';
3
3
  import { Router } from 'vue-router';
4
4
  export type CustomConfigType = GlobalData['customConfig'];
5
5
  export { RouterView as Outlet } from 'vue-router';
6
- export { useLocation } from './template/hooks/vue';
6
+ export { useLocation } from './template/vue/hooks';
7
7
  export type { GlobalSwicoConfigType };
8
8
  export declare const Link: import("vue").DefineComponent<{
9
9
  to: {
package/src/index.vue.js CHANGED
@@ -5,8 +5,8 @@ var vue_1 = require("vue");
5
5
  var vue_router_1 = require("vue-router");
6
6
  var vue_router_2 = require("vue-router");
7
7
  Object.defineProperty(exports, "Outlet", { enumerable: true, get: function () { return vue_router_2.RouterView; } });
8
- var vue_2 = require("./template/hooks/vue");
9
- Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return vue_2.useLocation; } });
8
+ var hooks_1 = require("./template/vue/hooks");
9
+ Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return hooks_1.useLocation; } });
10
10
  exports.Link = (0, vue_1.defineComponent)({
11
11
  components: {
12
12
  RouterLink: vue_router_1.RouterLink
@@ -1,12 +1,11 @@
1
1
  import { ConfigRouterType } from '../../utils/config';
2
- import { RoutesItemType } from './index';
3
2
  export { unstable_HistoryRouter as HistoryRouter } from 'react-router-dom';
4
3
  export declare const formatAncPath: (ancPath: string) => string;
5
- export declare const getPathNameList: (routes: RoutesItemType[]) => Array<{
4
+ export declare const pathNameList: {
6
5
  path: string;
7
6
  name: string;
8
- }>;
9
- export declare const getHistory: (routerBase: ConfigRouterType['base'], routerType: ConfigRouterType['type'], routes: RoutesItemType[]) => {
7
+ }[];
8
+ export declare const getHistory: (routerBase: ConfigRouterType['base'], routerType: ConfigRouterType['type']) => {
10
9
  history: SwicoHistoryType;
11
10
  originalHistory: import("history").HashHistory;
12
11
  };
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getHistory = exports.getPathNameList = exports.formatAncPath = exports.HistoryRouter = void 0;
6
+ exports.getHistory = exports.pathNameList = exports.formatAncPath = exports.HistoryRouter = void 0;
7
7
  var qs_1 = __importDefault(require("qs"));
8
8
  var history_1 = require("history");
9
+ var routes_1 = __importDefault(require("./routes"));
9
10
  var react_router_dom_1 = require("react-router-dom");
10
11
  Object.defineProperty(exports, "HistoryRouter", { enumerable: true, get: function () { return react_router_dom_1.unstable_HistoryRouter; } });
11
12
  var interpolatePath = function (pathTemplate, params) {
@@ -41,11 +42,10 @@ var getPathNameList = function (routes) {
41
42
  children.some(function (item) { return checkRouteItem(item, newAncPath); });
42
43
  }
43
44
  };
44
- console.log('rrr', routes);
45
45
  routes.forEach(function (item) { return checkRouteItem(item, ''); });
46
46
  return list;
47
47
  };
48
- exports.getPathNameList = getPathNameList;
48
+ exports.pathNameList = getPathNameList(routes_1.default);
49
49
  var getLocation = function (originalHistory) {
50
50
  var _a = originalHistory.location, pathname = _a.pathname, hash = _a.hash, search = _a.search;
51
51
  return {
@@ -80,11 +80,10 @@ var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
80
80
  }
81
81
  return obj;
82
82
  };
83
- var getHistory = function (routerBase, routerType, routes) {
83
+ var getHistory = function (routerBase, routerType) {
84
84
  var _a;
85
85
  var history;
86
86
  var originalHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
87
- var pathNameList = (0, exports.getPathNameList)(routes);
88
87
  var lastIndexBase = routerBase[routerBase.length - 1];
89
88
  var formatRouterBase = lastIndexBase === '/' ? routerBase.slice(0, routerBase.length - 1) : routerBase;
90
89
  history = {
@@ -97,7 +96,7 @@ var getHistory = function (routerBase, routerType, routes) {
97
96
  originalHistory.push("".concat(formatRouterBase).concat(to));
98
97
  break;
99
98
  case 'object':
100
- var formatOption = getFormatHistoryOption(to, formatRouterBase, pathNameList, 'push');
99
+ var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'push');
101
100
  originalHistory.push(formatOption);
102
101
  break;
103
102
  default:
@@ -110,7 +109,7 @@ var getHistory = function (routerBase, routerType, routes) {
110
109
  originalHistory.replace("".concat(formatRouterBase).concat(to));
111
110
  break;
112
111
  case 'object':
113
- var formatOption = getFormatHistoryOption(to, formatRouterBase, pathNameList, 'replace');
112
+ var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'replace');
114
113
  originalHistory.replace(formatOption);
115
114
  break;
116
115
  default:
@@ -0,0 +1 @@
1
+ export declare const useLocation: UseLocationType;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useLocation = void 0;
7
+ var react_router_dom_1 = require("react-router-dom");
8
+ var history_1 = require("./history");
9
+ var qs_1 = __importDefault(require("qs"));
10
+ var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
11
+ var patternWithValuesParts = urlPatternWithValues.split('/');
12
+ var patternWithParamsParts = urlPatternWithParams.split('/');
13
+ if (patternWithValuesParts.length !== patternWithParamsParts.length) {
14
+ return false;
15
+ }
16
+ for (var i = 0; i < patternWithValuesParts.length; i++) {
17
+ var valuePart = patternWithValuesParts[i];
18
+ var paramPart = patternWithParamsParts[i];
19
+ if (paramPart.startsWith(':') && paramPart !== '') {
20
+ continue;
21
+ }
22
+ if (valuePart !== paramPart) {
23
+ return false;
24
+ }
25
+ }
26
+ return true;
27
+ };
28
+ var useLocation = function () {
29
+ var _a, _b;
30
+ var location = (0, react_router_dom_1.useLocation)();
31
+ var params = (0, react_router_dom_1.useParams)();
32
+ var search = location.search, hash = location.hash, pathname = location.pathname;
33
+ var name = (_a = history_1.pathNameList.find(function (item) {
34
+ return (Object.keys(params).length > 0 && compareURLPatterns(pathname, item.path)) ||
35
+ item.path === pathname;
36
+ })) === null || _a === void 0 ? void 0 : _a.name;
37
+ var query = search ? qs_1.default.parse(search.startsWith('?') ? search.slice(1) : search) : {};
38
+ return {
39
+ name: name,
40
+ path: pathname,
41
+ pathname: (_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.pathname,
42
+ search: '',
43
+ query: query,
44
+ hash: hash,
45
+ params: params
46
+ };
47
+ };
48
+ exports.useLocation = useLocation;
@@ -29,7 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  var client_1 = require("react-dom/client");
30
30
  var react_1 = __importStar(require("react"));
31
31
  var react_router_dom_1 = require("react-router-dom");
32
- var routes_1 = require("./routes");
32
+ var routes_1 = __importDefault(require("./routes"));
33
33
  var loading_1 = __importDefault(require("./loading"));
34
34
  var history_1 = require("./history");
35
35
  var config_1 = require("./config");
@@ -43,7 +43,7 @@ var renderChildrenRouteList = function (childrenRoutes, ancPathKey) {
43
43
  });
44
44
  };
45
45
  var App = function () {
46
- var _a = (0, history_1.getHistory)(config_1.routerBase, config_1.routerType, routes_1.routes), history = _a.history, originalHistory = _a.originalHistory;
46
+ var _a = (0, history_1.getHistory)(config_1.routerBase, config_1.routerType), history = _a.history, originalHistory = _a.originalHistory;
47
47
  if (config_1.routerBase && config_1.routerBase !== '/') {
48
48
  if (config_1.routerType === 'browser' && window.location.pathname === '/') {
49
49
  window.location.replace("".concat(config_1.routerBase));
@@ -54,7 +54,7 @@ var App = function () {
54
54
  };
55
55
  return (react_1.default.createElement(history_1.HistoryRouter, { basename: config_1.routerBase, history: originalHistory },
56
56
  react_1.default.createElement(react_router_dom_1.Routes, null,
57
- react_1.default.createElement(react_router_dom_1.Route, { element: react_1.default.createElement(layout_1.default, null), path: '' }, renderChildrenRouteList(routes_1.routes, '')))));
57
+ react_1.default.createElement(react_router_dom_1.Route, { element: react_1.default.createElement(layout_1.default, null), path: '' }, renderChildrenRouteList(routes_1.default, '')))));
58
58
  };
59
59
  var root = (0, client_1.createRoot)(document.getElementById('root'));
60
60
  root.render(react_1.default.createElement(App, null));
@@ -1,2 +1,3 @@
1
1
  import { RoutesItemType } from './index';
2
- export declare const routes: RoutesItemType[];
2
+ declare const _default: RoutesItemType[];
3
+ export default _default;
@@ -1,4 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.routes = void 0;
4
- exports.routes = [];
3
+ exports.default = [];
@@ -0,0 +1,2 @@
1
+ export declare const getFormatRouterBase: (routerBase: any) => any;
2
+ export declare const useLocation: UseLocationType;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useLocation = exports.getFormatRouterBase = void 0;
7
+ var vue_router_1 = require("vue-router");
8
+ var qs_1 = __importDefault(require("qs"));
9
+ var vue_1 = require("vue");
10
+ var config_1 = require("./config");
11
+ var getFormatRouterBase = function (routerBase) {
12
+ var lastIndexBase = routerBase[routerBase.length - 1];
13
+ return lastIndexBase === '/' ? routerBase.slice(0, routerBase.length - 1) : routerBase;
14
+ };
15
+ exports.getFormatRouterBase = getFormatRouterBase;
16
+ var useLocation = function () {
17
+ var route = (0, vue_router_1.useRoute)();
18
+ var location = (0, vue_1.reactive)({
19
+ name: '',
20
+ path: '',
21
+ search: '',
22
+ hash: '',
23
+ pathname: '',
24
+ params: {},
25
+ query: {}
26
+ });
27
+ (0, vue_1.watchEffect)(function () {
28
+ var hash = route.hash, path = route.path, params = route.params, fullPath = route.fullPath, query = route.query, name = route.name;
29
+ var search = query ? qs_1.default.stringify(query) : '';
30
+ location.name = name;
31
+ location.path = path;
32
+ location.pathname = (0, exports.getFormatRouterBase)(config_1.routerBase) + path;
33
+ location.search = search;
34
+ location.query = query;
35
+ location.hash = hash;
36
+ location.params = params;
37
+ });
38
+ return location;
39
+ };
40
+ exports.useLocation = useLocation;
@@ -172,10 +172,10 @@ var getProjectConfig = function (env) { return __awaiter(void 0, void 0, void 0,
172
172
  case 4:
173
173
  templateType = customConfig['base'].template;
174
174
  routerConfig = __assign(__assign(__assign({}, exports.initConfig.router), customConfig['base'].router), customConfig[env].router);
175
- return [4, initTemplateRouterConfig(routerConfig, templateType)];
175
+ return [4, initTemplateConfig(routerConfig, templateType)];
176
176
  case 5:
177
177
  _e.sent();
178
- return [4, handleCliIndexFile(templateType)];
178
+ return [4, initCliIndexFile(templateType)];
179
179
  case 6:
180
180
  _e.sent();
181
181
  entryPath = path_1.default.resolve(projectPath, './src/.swico/index.js');
@@ -219,8 +219,8 @@ var getFormatRouter = function (routes, templateType) {
219
219
  return _main(item);
220
220
  });
221
221
  };
222
- var handleCliIndexFile = function (templateType) { return __awaiter(void 0, void 0, void 0, function () {
223
- var targetPath, targetTypesPath, replaceFileText, fileTypesText;
222
+ var initCliIndexFile = function (templateType) { return __awaiter(void 0, void 0, void 0, function () {
223
+ var targetPath, targetTypesPath, replaceFileText, fileTypesText, formatHooksPath;
224
224
  return __generator(this, function (_a) {
225
225
  switch (_a.label) {
226
226
  case 0:
@@ -228,6 +228,12 @@ var handleCliIndexFile = function (templateType) { return __awaiter(void 0, void
228
228
  targetTypesPath = path_1.default.resolve(__dirname, '../index.d.ts');
229
229
  replaceFileText = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, "../index.".concat(templateType, ".js")), 'utf8');
230
230
  fileTypesText = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, "../index.".concat(templateType, ".d.ts")), 'utf8');
231
+ if (templateType === 'react') {
232
+ formatHooksPath = path_1.default
233
+ .resolve(projectPath, './src/.swico/hooks')
234
+ .replaceAll('\\', '/');
235
+ replaceFileText = replaceFileText.replaceAll('require("./template/react/hooks");', "require(\"".concat(formatHooksPath, "\");"));
236
+ }
231
237
  return [4, (0, tools_1.writeFile)(targetPath, replaceFileText)];
232
238
  case 1:
233
239
  _a.sent();
@@ -238,25 +244,21 @@ var handleCliIndexFile = function (templateType) { return __awaiter(void 0, void
238
244
  }
239
245
  });
240
246
  }); };
241
- var formatTemplateFileText = function (_a, templateType) {
247
+ var formatRouterConfig = function (_a, templateType) {
242
248
  var _b = _a.routes, routes = _b === void 0 ? [] : _b, _c = _a.type, type = _c === void 0 ? 'browser' : _c, _d = _a.base, base = _d === void 0 ? '/' : _d;
243
249
  return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
244
- var formatRouter, textData, modifiedText, indexFilePath, configFilePath, indexText, configText, replaceIndexText, replaceConfigText;
250
+ var formatRouter, textData, modifiedText, configFilePath, replaceConfigText;
245
251
  return __generator(this, function (_a) {
246
252
  switch (_a.label) {
247
253
  case 0:
248
254
  formatRouter = getFormatRouter(routes, templateType);
249
- textData = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.routes = ".concat(JSON.stringify(formatRouter), ";");
255
+ textData = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = ".concat(JSON.stringify(formatRouter), ";");
250
256
  modifiedText = textData.replace(/"(\(\)=>import\('[^']+'\))"/g, '$1');
251
- return [4, (0, tools_1.writeFile)(path_1.default.resolve(__dirname, "../template/".concat(templateType, "/routes.js")), modifiedText)];
257
+ return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/routes.js'), modifiedText)];
252
258
  case 1:
253
259
  _a.sent();
254
- indexFilePath = path_1.default.resolve(__dirname, "../template/".concat(templateType, "/index.js"));
255
- configFilePath = path_1.default.resolve(__dirname, "../template/".concat(templateType, "/config.js"));
256
- indexText = fs_extra_1.default.readFileSync(indexFilePath, 'utf8');
257
- configText = fs_extra_1.default.readFileSync(configFilePath, 'utf8');
258
- replaceIndexText = indexText;
259
- replaceConfigText = configText;
260
+ configFilePath = path_1.default.resolve(projectPath, './src/.swico/config.js');
261
+ replaceConfigText = fs_extra_1.default.readFileSync(configFilePath, 'utf8');
260
262
  if (base) {
261
263
  replaceConfigText = replaceConfigText.replace(/exports\.routerBase\s*=\s*(.*)/gm, function (match, p1) {
262
264
  return "exports.routerBase = '".concat(templateType === 'react' && type === 'hash' ? '/' : base, "';");
@@ -267,11 +269,8 @@ var formatTemplateFileText = function (_a, templateType) {
267
269
  return "exports.routerType = '".concat(type, "';");
268
270
  });
269
271
  }
270
- return [4, (0, tools_1.writeFile)(indexFilePath, replaceIndexText)];
271
- case 2:
272
- _a.sent();
273
272
  return [4, (0, tools_1.writeFile)(configFilePath, replaceConfigText)];
274
- case 3:
273
+ case 2:
275
274
  _a.sent();
276
275
  resolve(null);
277
276
  return [2];
@@ -279,24 +278,25 @@ var formatTemplateFileText = function (_a, templateType) {
279
278
  });
280
279
  }); });
281
280
  };
282
- var initTemplateRouterConfig = function (routerConfig, templateType) {
281
+ var initTemplateConfig = function (routerConfig, templateType) {
283
282
  return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
284
283
  var copyTargetPath, replaceIndexText, e_1, e_2, e_3;
285
284
  return __generator(this, function (_a) {
286
285
  switch (_a.label) {
287
- case 0: return [4, formatTemplateFileText(routerConfig, templateType)];
288
- case 1:
289
- _a.sent();
286
+ case 0:
290
287
  copyTargetPath = path_1.default.resolve(projectPath, './src/.swico');
291
- if (!fs_extra_1.default.existsSync(copyTargetPath)) return [3, 3];
288
+ if (!fs_extra_1.default.existsSync(copyTargetPath)) return [3, 2];
292
289
  return [4, fs_extra_1.default.remove(copyTargetPath)];
293
- case 2:
290
+ case 1:
294
291
  _a.sent();
295
- _a.label = 3;
296
- case 3: return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../template/".concat(templateType)), copyTargetPath, function (fileName) {
292
+ _a.label = 2;
293
+ case 2: return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../template/".concat(templateType)), copyTargetPath, function (fileName) {
297
294
  return !fileName.endsWith('.d.ts') &&
298
295
  (templateType === 'vue' ? fileName !== 'Container.js' : true);
299
296
  })];
297
+ case 3:
298
+ _a.sent();
299
+ return [4, formatRouterConfig(routerConfig, templateType)];
300
300
  case 4:
301
301
  _a.sent();
302
302
  replaceIndexText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath, './src/.swico/index.js'), 'utf8');