swico 0.5.2 → 0.6.0-beta10

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.2",
3
+ "version": "0.6.0-beta10",
4
4
  "description": "Simple, efficient, and practical front-end framework",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -45,6 +45,7 @@ var config_1 = require("../utils/config");
45
45
  var chalk_1 = __importDefault(require("chalk"));
46
46
  var ora_1 = __importDefault(require("ora"));
47
47
  var tools_1 = require("../utils/tools");
48
+ var package_json_1 = __importDefault(require("../../package.json"));
48
49
  var spinner = (0, ora_1.default)();
49
50
  function default_1() {
50
51
  return __awaiter(this, void 0, void 0, function () {
@@ -53,6 +54,8 @@ function default_1() {
53
54
  switch (_a.label) {
54
55
  case 0:
55
56
  process.env.SWICO_ENV = 'prod';
57
+ tools_1.toast.info("Swico v".concat(package_json_1.default.version));
58
+ spinner.start('Initializing Swico production config...');
56
59
  return [4, (0, config_1.getProjectConfig)('prod')];
57
60
  case 1:
58
61
  projectConfig = _a.sent();
@@ -66,8 +69,8 @@ function default_1() {
66
69
  })];
67
70
  case 2:
68
71
  buildConfig = _a.sent();
72
+ spinner.succeed();
69
73
  compiler = (0, webpack_1.default)(buildConfig);
70
- console.log("".concat(chalk_1.default.hex('#5f72f5')('Swico starts building....'), " \n"));
71
74
  compiler.run(function (err, stats) {
72
75
  if (err) {
73
76
  tools_1.toast.error(err.stack || err.toString());
@@ -59,6 +59,9 @@ var chokidar_1 = __importDefault(require("chokidar"));
59
59
  var path_1 = __importDefault(require("path"));
60
60
  var cross_spawn_1 = __importDefault(require("cross-spawn"));
61
61
  var envPort = process.env.PORT;
62
+ var package_json_1 = __importDefault(require("../../package.json"));
63
+ var ora_1 = __importDefault(require("ora"));
64
+ var spinner = (0, ora_1.default)();
62
65
  var handleWatch = function (projectPath, devServer) {
63
66
  var configFilesWatcher = chokidar_1.default
64
67
  .watch([
@@ -128,6 +131,8 @@ function start() {
128
131
  switch (_b.label) {
129
132
  case 0:
130
133
  process.env.SWICO_ENV = 'dev';
134
+ tools_1.toast.info("Swico v".concat(package_json_1.default.version));
135
+ spinner.start('Initializing Swico development config...');
131
136
  if (!(envPort !== null && envPort !== void 0)) return [3, 1];
132
137
  _a = envPort;
133
138
  return [3, 3];
@@ -144,6 +149,7 @@ function start() {
144
149
  return [4, (0, webpack_dev_1.default)(projectConfig)];
145
150
  case 5:
146
151
  startConfig = _b.sent();
152
+ spinner.succeed();
147
153
  compiler = (0, webpack_1.default)(startConfig);
148
154
  devServer = new webpack_dev_server_1.default(__assign(__assign({}, startConfig.devServer), { port: availablePort }), compiler);
149
155
  _b.label = 6;
@@ -7,8 +7,12 @@ exports.getHistory = exports.pathNameList = exports.formatAncPath = exports.Hist
7
7
  var qs_1 = __importDefault(require("qs"));
8
8
  var history_1 = require("history");
9
9
  var routes_1 = __importDefault(require("./routes"));
10
+ var config_1 = require("./config");
11
+ var hooks_1 = require("./hooks");
10
12
  var react_router_dom_1 = require("react-router-dom");
11
13
  Object.defineProperty(exports, "HistoryRouter", { enumerable: true, get: function () { return react_router_dom_1.unstable_HistoryRouter; } });
14
+ var lastIndexBase = config_1.routerBase[config_1.routerBase.length - 1];
15
+ var formatRouterBase = lastIndexBase === '/' ? config_1.routerBase.slice(0, config_1.routerBase.length - 1) : config_1.routerBase;
12
16
  var interpolatePath = function (pathTemplate, params) {
13
17
  var pathParts = pathTemplate.split('/');
14
18
  var interpolatedParts = pathParts.map(function (part) {
@@ -26,6 +30,23 @@ var interpolatePath = function (pathTemplate, params) {
26
30
  });
27
31
  return interpolatedParts.join('/');
28
32
  };
33
+ var interpolatePathParams = function (pathTemplate, path) {
34
+ var _a;
35
+ if (pathTemplate === void 0) { pathTemplate = ''; }
36
+ var regexPattern = pathTemplate.replace(/:(\w+)/g, '([^/]+)');
37
+ var regex = new RegExp("^".concat(regexPattern, "$"));
38
+ var match = path.match(regex);
39
+ if (match) {
40
+ var params_1 = match.slice(1);
41
+ var paramNames = ((_a = pathTemplate.match(/:(\w+)/g)) === null || _a === void 0 ? void 0 : _a.map(function (name) { return name.slice(1); })) || [];
42
+ var result_1 = {};
43
+ paramNames.forEach(function (name, index) {
44
+ result_1[name] = params_1[index];
45
+ });
46
+ return result_1;
47
+ }
48
+ return {};
49
+ };
29
50
  var formatAncPath = function (ancPath) {
30
51
  var newAncPath = ancPath.startsWith('/') ? ancPath : '/' + ancPath;
31
52
  newAncPath = newAncPath.endsWith('/') ? newAncPath.slice(0, -1) : newAncPath;
@@ -48,14 +69,19 @@ var getPathNameList = function (routes) {
48
69
  exports.pathNameList = getPathNameList(routes_1.default);
49
70
  var getLocation = function (originalHistory) {
50
71
  var _a = originalHistory.location, pathname = _a.pathname, hash = _a.hash, search = _a.search;
72
+ var routerBaseLength = formatRouterBase.length;
73
+ var path = pathname.slice(routerBaseLength);
74
+ var matchPathNameItem = exports.pathNameList.find(function (item) { return item.path === path || (0, hooks_1.compareURLPatterns)(path, item.path); });
75
+ console.log('matchPathNameItem', matchPathNameItem);
76
+ var params = interpolatePathParams(matchPathNameItem === null || matchPathNameItem === void 0 ? void 0 : matchPathNameItem.path, path);
51
77
  return {
52
78
  hash: hash,
53
79
  search: search,
54
80
  pathname: pathname,
55
81
  query: search ? qs_1.default.parse(search.startsWith('?') ? search.slice(1) : search) : {},
56
- name: '',
57
- params: {},
58
- path: ''
82
+ name: matchPathNameItem === null || matchPathNameItem === void 0 ? void 0 : matchPathNameItem.name,
83
+ params: params,
84
+ path: path
59
85
  };
60
86
  };
61
87
  var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
@@ -84,8 +110,6 @@ var getHistory = function (routerBase, routerType) {
84
110
  var _a;
85
111
  var history;
86
112
  var originalHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
87
- var lastIndexBase = routerBase[routerBase.length - 1];
88
- var formatRouterBase = lastIndexBase === '/' ? routerBase.slice(0, routerBase.length - 1) : routerBase;
89
113
  history = {
90
114
  go: originalHistory.go,
91
115
  forward: originalHistory.forward,
@@ -1 +1,2 @@
1
+ export declare const compareURLPatterns: (urlPatternWithValues: string, urlPatternWithParams: string) => boolean;
1
2
  export declare const useLocation: UseLocationType;
@@ -3,7 +3,7 @@ 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.useLocation = void 0;
6
+ exports.useLocation = exports.compareURLPatterns = void 0;
7
7
  var react_router_dom_1 = require("react-router-dom");
8
8
  var history_1 = require("./history");
9
9
  var qs_1 = __importDefault(require("qs"));
@@ -25,13 +25,14 @@ var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
25
25
  }
26
26
  return true;
27
27
  };
28
+ exports.compareURLPatterns = compareURLPatterns;
28
29
  var useLocation = function () {
29
30
  var _a, _b;
30
31
  var location = (0, react_router_dom_1.useLocation)();
31
32
  var params = (0, react_router_dom_1.useParams)();
32
33
  var search = location.search, hash = location.hash, pathname = location.pathname;
33
34
  var name = (_a = history_1.pathNameList.find(function (item) {
34
- return (Object.keys(params).length > 0 && compareURLPatterns(pathname, item.path)) ||
35
+ return (Object.keys(params).length > 0 && (0, exports.compareURLPatterns)(pathname, item.path)) ||
35
36
  item.path === pathname;
36
37
  })) === null || _a === void 0 ? void 0 : _a.name;
37
38
  var query = search ? qs_1.default.parse(search.startsWith('?') ? search.slice(1) : search) : {};
@@ -42,6 +42,6 @@ export interface GlobalData {
42
42
  prod: Pick<GlobalSwicoConfigType, 'plugins' | 'console' | 'copy' | 'devtool' | 'router'>;
43
43
  };
44
44
  }
45
- export declare const getProjectConfig: (env?: GlobalData['env']) => Promise<GlobalData>;
45
+ export declare const getProjectConfig: (env: GlobalData['env']) => Promise<GlobalData>;
46
46
  export declare const getFormatDefineVars: (defineVarsConfigData: any) => Promise<{}>;
47
47
  export declare const initConfig: Omit<GlobalSwicoConfigType, 'template'>;
@@ -172,7 +172,7 @@ 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, initTemplateConfig(routerConfig, templateType)];
175
+ return [4, initTemplateConfig(routerConfig, templateType, env)];
176
176
  case 5:
177
177
  _e.sent();
178
178
  return [4, initCliIndexFile(templateType)];
@@ -246,20 +246,25 @@ var initCliIndexFile = function (templateType) { return __awaiter(void 0, void 0
246
246
  }
247
247
  });
248
248
  }); };
249
- var formatRouterConfig = function (_a, templateType) {
249
+ var formatRouterConfig = function (_a, templateType, env) {
250
250
  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;
251
+ var envPathSuffix = env === 'dev' ? '' : 'prod.';
251
252
  return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
252
253
  var formatRouter, textData, modifiedText, configFilePath, replaceConfigText;
253
254
  return __generator(this, function (_a) {
254
255
  switch (_a.label) {
255
256
  case 0:
257
+ if (env === 'prod') {
258
+ fs_extra_1.default.copyFileSync(path_1.default.resolve(projectPath, './src/.swico/routes.js'), path_1.default.resolve(projectPath, './src/.swico/routes.prod.js'));
259
+ fs_extra_1.default.copyFileSync(path_1.default.resolve(projectPath, './src/.swico/config.js'), path_1.default.resolve(projectPath, './src/.swico/routes.config.js'));
260
+ }
256
261
  formatRouter = getFormatRouter(routes, templateType);
257
262
  textData = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = ".concat(JSON.stringify(formatRouter), ";");
258
263
  modifiedText = textData.replace(/"(\(\)=>import\('[^']+'\))"/g, '$1');
259
- return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/routes.js'), modifiedText)];
264
+ return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, "./src/.swico/routes.".concat(envPathSuffix, "js")), modifiedText)];
260
265
  case 1:
261
266
  _a.sent();
262
- configFilePath = path_1.default.resolve(projectPath, './src/.swico/config.js');
267
+ configFilePath = path_1.default.resolve(projectPath, "./src/.swico/config.".concat(envPathSuffix, "js"));
263
268
  replaceConfigText = fs_extra_1.default.readFileSync(configFilePath, 'utf8');
264
269
  if (base) {
265
270
  replaceConfigText = replaceConfigText.replace(/exports\.routerBase\s*=\s*(.*)/gm, function (match, p1) {
@@ -280,9 +285,9 @@ var formatRouterConfig = function (_a, templateType) {
280
285
  });
281
286
  }); });
282
287
  };
283
- var initTemplateConfig = function (routerConfig, templateType) {
288
+ var initTemplateConfig = function (routerConfig, templateType, env) {
284
289
  return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
285
- var copyTargetPath, replaceIndexText, e_1, e_2, e_3;
290
+ var copyTargetPath, replaceIndexText, replaceHistoryText, e_1, e_2, e_3;
286
291
  return __generator(this, function (_a) {
287
292
  switch (_a.label) {
288
293
  case 0:
@@ -298,10 +303,19 @@ var initTemplateConfig = function (routerConfig, templateType) {
298
303
  })];
299
304
  case 3:
300
305
  _a.sent();
301
- return [4, formatRouterConfig(routerConfig, templateType)];
306
+ return [4, formatRouterConfig(routerConfig, templateType, env)];
302
307
  case 4:
303
308
  _a.sent();
304
309
  replaceIndexText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath, './src/.swico/index.js'), 'utf8');
310
+ replaceHistoryText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath, './src/.swico/history.js'), 'utf8');
311
+ if (env === 'prod') {
312
+ replaceIndexText = replaceIndexText.replaceAll('require("./config");', 'require("./config.prod");');
313
+ replaceHistoryText = replaceHistoryText.replaceAll('require("./config");', 'require("./config.prod");');
314
+ }
315
+ else {
316
+ replaceIndexText = replaceIndexText.replaceAll('require("./config.prod");', 'require("./config");');
317
+ replaceHistoryText = replaceHistoryText.replaceAll('require("./config.prod");', 'require("./config");');
318
+ }
305
319
  _a.label = 5;
306
320
  case 5:
307
321
  _a.trys.push([5, 7, , 8]);
@@ -328,31 +342,37 @@ var initTemplateConfig = function (routerConfig, templateType) {
328
342
  replaceIndexText = replaceIndexText.replaceAll('require("../global.less");', '');
329
343
  return [3, 11];
330
344
  case 11:
331
- if (!(templateType === 'vue')) return [3, 13];
345
+ if (!(templateType === 'vue')) return [3, 14];
332
346
  replaceIndexText = replaceIndexText.replace('"./Container"', '"./Container.vue"');
333
347
  return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/index.js'), replaceIndexText)];
334
348
  case 12:
335
349
  _a.sent();
336
- _a.label = 13;
350
+ return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/history.js'), replaceHistoryText)];
337
351
  case 13:
338
- if (!(templateType === 'react')) return [3, 19];
352
+ _a.sent();
339
353
  _a.label = 14;
340
354
  case 14:
341
- _a.trys.push([14, 16, 17, 19]);
342
- return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
355
+ if (!(templateType === 'react')) return [3, 21];
356
+ _a.label = 15;
343
357
  case 15:
358
+ _a.trys.push([15, 17, 18, 21]);
359
+ return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
360
+ case 16:
344
361
  _a.sent();
345
362
  replaceIndexText = replaceIndexText.replace('"./loading"', '"../loading"');
346
- return [3, 19];
347
- case 16:
363
+ return [3, 21];
364
+ case 17:
348
365
  e_3 = _a.sent();
349
366
  replaceIndexText = replaceIndexText.replace('"../loading"', '"./loading"');
350
- return [3, 19];
351
- case 17: return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/index.js'), replaceIndexText)];
352
- case 18:
367
+ return [3, 21];
368
+ case 18: return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/index.js'), replaceIndexText)];
369
+ case 19:
370
+ _a.sent();
371
+ return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/history.js'), replaceHistoryText)];
372
+ case 20:
353
373
  _a.sent();
354
374
  return [7];
355
- case 19:
375
+ case 21:
356
376
  resolve(null);
357
377
  return [2];
358
378
  }
@@ -1,6 +1,7 @@
1
1
  export declare const copyDirFiles: (src: any, dest: any, filter?: (fileName: any) => boolean) => Promise<any>;
2
2
  export declare const getPort: () => Promise<number>;
3
3
  export declare const toast: {
4
+ info: (message: string) => void;
4
5
  error: (message: string | string[]) => void;
5
6
  warning: (message: string | string[]) => void;
6
7
  };
@@ -101,8 +101,11 @@ var getPort = function () {
101
101
  };
102
102
  exports.getPort = getPort;
103
103
  exports.toast = {
104
+ info: function (message) {
105
+ console.log("\n> ".concat(chalk_1.default.hex('#5f72f5')(message), "\n"));
106
+ },
104
107
  error: function (message) {
105
- console.log("\n".concat(chalk_1.default.hex('#5f72f5')('There are some errors about Swico:'), " \n"));
108
+ console.log("\n> ".concat(chalk_1.default.hex('#5f72f5')('There are some errors about Swico:'), " \n"));
106
109
  if (Array.isArray(message)) {
107
110
  message.forEach(function (item) {
108
111
  console.log(" > ".concat(chalk_1.default.red.bold(item), " \n"));
@@ -113,7 +116,7 @@ exports.toast = {
113
116
  }
114
117
  },
115
118
  warning: function (message) {
116
- console.log("\n".concat(chalk_1.default.hex('#5f72f5')('There are some warnings about Swico:'), " \n"));
119
+ console.log("\n> ".concat(chalk_1.default.hex('#5f72f5')('There are some warnings about Swico:'), " \n"));
117
120
  if (Array.isArray(message)) {
118
121
  message.forEach(function (item) {
119
122
  console.log(" > ".concat(chalk_1.default.hex('#fb8918').bold(item), " \n"));