swico 0.6.0-beta21 → 0.6.0-beta23
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 +1 -1
- package/src/utils/config.js +41 -78
- package/src/utils/tools.js +0 -1
- package/src/template/hooks/react.d.ts +0 -12
- package/src/template/hooks/react.js +0 -47
- package/src/template/hooks/vue.d.ts +0 -3
- package/src/template/hooks/vue.js +0 -40
- package/src/template/react/config.prod.d.ts +0 -3
- package/src/template/react/config.prod.js +0 -5
- package/src/template/react/routes.prod.d.ts +0 -3
- package/src/template/react/routes.prod.js +0 -3
- package/src/template/vue/config.prod.d.ts +0 -3
- package/src/template/vue/config.prod.js +0 -5
- package/src/template/vue/routes.prod.d.ts +0 -6
- package/src/template/vue/routes.prod.js +0 -3
package/package.json
CHANGED
package/src/utils/config.js
CHANGED
|
@@ -80,7 +80,7 @@ var chalk_1 = __importDefault(require("chalk"));
|
|
|
80
80
|
var tools_1 = require("./tools");
|
|
81
81
|
var projectPath = process.cwd();
|
|
82
82
|
var getProjectConfig = function (env) { return __awaiter(void 0, void 0, void 0, function () {
|
|
83
|
-
var configDir, configPath, customConfig, _loop_1, _i, _a, key, templateType, routerConfig, entryPath, templatePath;
|
|
83
|
+
var configDir, configPath, customConfig, _loop_1, _i, _a, key, templateType, routerConfig, envPath, entryPath, templatePath;
|
|
84
84
|
var _b, _c, _d;
|
|
85
85
|
return __generator(this, function (_e) {
|
|
86
86
|
switch (_e.label) {
|
|
@@ -178,7 +178,8 @@ var getProjectConfig = function (env) { return __awaiter(void 0, void 0, void 0,
|
|
|
178
178
|
return [4, initCliIndexFile(templateType)];
|
|
179
179
|
case 6:
|
|
180
180
|
_e.sent();
|
|
181
|
-
|
|
181
|
+
envPath = env === 'dev' ? '' : '.prod/';
|
|
182
|
+
entryPath = path_1.default.resolve(projectPath, "./src/.swico/".concat(envPath, "index.js"));
|
|
182
183
|
templatePath = path_1.default.join(projectPath, '/src/index.ejs');
|
|
183
184
|
return [2, {
|
|
184
185
|
projectPath: projectPath,
|
|
@@ -246,7 +247,7 @@ var initCliIndexFile = function (templateType) { return __awaiter(void 0, void 0
|
|
|
246
247
|
}); };
|
|
247
248
|
var formatRouterConfig = function (_a, templateType, env) {
|
|
248
249
|
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;
|
|
249
|
-
var
|
|
250
|
+
var envPath = env === 'dev' ? '/' : '/.prod/';
|
|
250
251
|
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
251
252
|
var formatRouter, textData, modifiedText, configFilePath, replaceConfigText;
|
|
252
253
|
return __generator(this, function (_a) {
|
|
@@ -255,10 +256,10 @@ var formatRouterConfig = function (_a, templateType, env) {
|
|
|
255
256
|
formatRouter = getFormatRouter(routes, templateType);
|
|
256
257
|
textData = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = ".concat(JSON.stringify(formatRouter), ";");
|
|
257
258
|
modifiedText = textData.replace(/"(\(\)=>import\('[^']+'\))"/g, '$1');
|
|
258
|
-
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, "./src/.swico
|
|
259
|
+
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "routes.js")), modifiedText)];
|
|
259
260
|
case 1:
|
|
260
261
|
_a.sent();
|
|
261
|
-
configFilePath = path_1.default.resolve(projectPath, "./src/.swico
|
|
262
|
+
configFilePath = path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "config.js"));
|
|
262
263
|
replaceConfigText = fs_extra_1.default.readFileSync(configFilePath, 'utf8');
|
|
263
264
|
if (base) {
|
|
264
265
|
replaceConfigText = replaceConfigText.replace(/exports\.routerBase\s*=\s*(.*)/gm, function (match, p1) {
|
|
@@ -279,123 +280,85 @@ var formatRouterConfig = function (_a, templateType, env) {
|
|
|
279
280
|
});
|
|
280
281
|
}); });
|
|
281
282
|
};
|
|
282
|
-
var getCopyFileNameList = function (templateType, env) {
|
|
283
|
-
var list = ['hooks.js', 'index.js', 'history.js', 'loading.js'];
|
|
284
|
-
switch (true) {
|
|
285
|
-
case templateType === 'vue' && env === 'dev':
|
|
286
|
-
list.push('config.js', 'Container.vue', 'routes.js');
|
|
287
|
-
break;
|
|
288
|
-
case templateType === 'vue' && env === 'prod':
|
|
289
|
-
list.push('config.prod.js', 'Container.vue', 'routes.prod.js');
|
|
290
|
-
break;
|
|
291
|
-
case templateType === 'react' && env === 'dev':
|
|
292
|
-
list.push('config.js', 'history.js', 'routes.js');
|
|
293
|
-
break;
|
|
294
|
-
case templateType === 'react' && env === 'prod':
|
|
295
|
-
list.push('config.prod.js', 'history.js', 'routes.prod.js');
|
|
296
|
-
break;
|
|
297
|
-
default:
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
return list;
|
|
301
|
-
};
|
|
302
283
|
var initTemplateConfig = function (routerConfig, templateType, env) {
|
|
284
|
+
var envPath = env === 'dev' ? '/' : '/.prod/';
|
|
303
285
|
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
304
|
-
var copyTargetPath,
|
|
286
|
+
var copyTargetPath, replaceIndexText, envAncPath, e_1, e_2, e_3;
|
|
305
287
|
return __generator(this, function (_a) {
|
|
306
288
|
switch (_a.label) {
|
|
307
289
|
case 0:
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
290
|
+
copyTargetPath = path_1.default.resolve(projectPath, "./src/.swico".concat(envPath));
|
|
291
|
+
return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../template/".concat(templateType)), copyTargetPath, function (fileName) {
|
|
292
|
+
return !fileName.endsWith('.d.ts') &&
|
|
293
|
+
(templateType === 'vue' ? fileName !== 'Container.js' : true);
|
|
294
|
+
})];
|
|
312
295
|
case 1:
|
|
313
296
|
_a.sent();
|
|
314
297
|
return [4, formatRouterConfig(routerConfig, templateType, env)];
|
|
315
298
|
case 2:
|
|
316
299
|
_a.sent();
|
|
317
|
-
replaceIndexText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath,
|
|
300
|
+
replaceIndexText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), 'utf8');
|
|
301
|
+
replaceIndexText = replaceIndexText.replaceAll('require("../global");', '');
|
|
318
302
|
if (env === 'prod') {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
303
|
+
if (templateType === 'react') {
|
|
304
|
+
replaceIndexText = replaceIndexText.replaceAll('require("../layout")', 'require("../../layout")');
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
replaceIndexText = replaceIndexText.replaceAll('require("../layout/Layout")', 'require("../../layout/Layout")');
|
|
308
|
+
}
|
|
325
309
|
}
|
|
310
|
+
envAncPath = env === 'dev' ? '..' : '../..';
|
|
326
311
|
_a.label = 3;
|
|
327
312
|
case 3:
|
|
328
313
|
_a.trys.push([3, 5, , 6]);
|
|
329
314
|
return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/global.ts'), fs_extra_1.default.constants.F_OK)];
|
|
330
315
|
case 4:
|
|
331
316
|
_a.sent();
|
|
332
|
-
replaceIndexText = replaceIndexText.replaceAll(
|
|
333
|
-
replaceIndexText =
|
|
317
|
+
replaceIndexText = replaceIndexText.replaceAll("require(\"".concat(envAncPath, "/global\");"), '');
|
|
318
|
+
replaceIndexText = "require(\"".concat(envAncPath, "/global\");\n") + replaceIndexText;
|
|
334
319
|
return [3, 6];
|
|
335
320
|
case 5:
|
|
336
321
|
e_1 = _a.sent();
|
|
337
|
-
replaceIndexText = replaceIndexText.replaceAll(
|
|
322
|
+
replaceIndexText = replaceIndexText.replaceAll("require(\"".concat(envAncPath, "/global\");"), '');
|
|
338
323
|
return [3, 6];
|
|
339
324
|
case 6:
|
|
340
325
|
_a.trys.push([6, 8, , 9]);
|
|
341
326
|
return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/global.less'), fs_extra_1.default.constants.F_OK)];
|
|
342
327
|
case 7:
|
|
343
328
|
_a.sent();
|
|
344
|
-
replaceIndexText = replaceIndexText.replaceAll(
|
|
345
|
-
replaceIndexText =
|
|
329
|
+
replaceIndexText = replaceIndexText.replaceAll("require(\"".concat(envAncPath, "/global.less\");"), '');
|
|
330
|
+
replaceIndexText = "require(\"".concat(envAncPath, "/global.less\");\n") + replaceIndexText;
|
|
346
331
|
return [3, 9];
|
|
347
332
|
case 8:
|
|
348
333
|
e_2 = _a.sent();
|
|
349
|
-
replaceIndexText = replaceIndexText.replaceAll(
|
|
334
|
+
replaceIndexText = replaceIndexText.replaceAll("require(\"".concat(envAncPath, "/global.less\");"), '');
|
|
350
335
|
return [3, 9];
|
|
351
336
|
case 9:
|
|
352
|
-
if (!(templateType === 'vue')) return [3,
|
|
337
|
+
if (!(templateType === 'vue')) return [3, 11];
|
|
353
338
|
replaceIndexText = replaceIndexText.replace('"./Container"', '"./Container.vue"');
|
|
354
|
-
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath,
|
|
339
|
+
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), replaceIndexText)];
|
|
355
340
|
case 10:
|
|
356
341
|
_a.sent();
|
|
357
|
-
|
|
358
|
-
if (env === 'prod') {
|
|
359
|
-
replaceHooksText = replaceHooksText.replaceAll('require("./config")', 'require("./config.prod")');
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
replaceHooksText = replaceIndexText.replaceAll('require("./config.prod")', 'require("./config")');
|
|
363
|
-
}
|
|
364
|
-
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/hooks.js'), replaceHooksText)];
|
|
342
|
+
return [3, 17];
|
|
365
343
|
case 11:
|
|
366
|
-
|
|
367
|
-
|
|
344
|
+
if (!(templateType === 'react')) return [3, 17];
|
|
345
|
+
_a.label = 12;
|
|
368
346
|
case 12:
|
|
369
|
-
|
|
370
|
-
_a.label = 13;
|
|
371
|
-
case 13:
|
|
372
|
-
_a.trys.push([13, 15, 16, 19]);
|
|
347
|
+
_a.trys.push([12, 14, 15, 17]);
|
|
373
348
|
return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
|
|
374
|
-
case
|
|
349
|
+
case 13:
|
|
375
350
|
_a.sent();
|
|
376
351
|
replaceIndexText = replaceIndexText.replace('"./loading"', '"../loading"');
|
|
377
|
-
return [3,
|
|
378
|
-
case
|
|
352
|
+
return [3, 17];
|
|
353
|
+
case 14:
|
|
379
354
|
e_3 = _a.sent();
|
|
380
355
|
replaceIndexText = replaceIndexText.replace('"../loading"', '"./loading"');
|
|
381
|
-
return [3,
|
|
382
|
-
case
|
|
383
|
-
case
|
|
384
|
-
_a.sent();
|
|
385
|
-
replaceHistoryText = fs_extra_1.default.readFileSync(path_1.default.resolve(projectPath, './src/.swico/history.js'), 'utf8');
|
|
386
|
-
if (env === 'prod') {
|
|
387
|
-
replaceHistoryText = replaceHistoryText.replaceAll('require("./config")', 'require("./config.prod")');
|
|
388
|
-
replaceHistoryText = replaceHistoryText.replaceAll('require("./routes")', 'require("./routes.prod")');
|
|
389
|
-
}
|
|
390
|
-
else {
|
|
391
|
-
replaceHistoryText = replaceHistoryText.replaceAll('require("./config.prod")', 'require("./config")');
|
|
392
|
-
replaceHistoryText = replaceHistoryText.replaceAll('require("./routes.prod")', 'require("./routes")');
|
|
393
|
-
}
|
|
394
|
-
return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, './src/.swico/history.js'), replaceHistoryText)];
|
|
395
|
-
case 18:
|
|
356
|
+
return [3, 17];
|
|
357
|
+
case 15: return [4, (0, tools_1.writeFile)(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), replaceIndexText)];
|
|
358
|
+
case 16:
|
|
396
359
|
_a.sent();
|
|
397
360
|
return [7];
|
|
398
|
-
case
|
|
361
|
+
case 17:
|
|
399
362
|
resolve(null);
|
|
400
363
|
return [2];
|
|
401
364
|
}
|
package/src/utils/tools.js
CHANGED
|
@@ -172,7 +172,6 @@ var initIndexFile = function () { return __awaiter(void 0, void 0, void 0, funct
|
|
|
172
172
|
switch (_a.label) {
|
|
173
173
|
case 0:
|
|
174
174
|
fileText = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, '../index.js'), 'utf8');
|
|
175
|
-
console.log('fileText', fileText);
|
|
176
175
|
targetPath = path_1.default.resolve(__dirname, '../index.js');
|
|
177
176
|
formatHooksPath = path_1.default
|
|
178
177
|
.resolve(process.cwd(), './src/.swico/hooks')
|
|
@@ -1,47 +0,0 @@
|
|
|
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 qs_1 = __importDefault(require("qs"));
|
|
9
|
-
var history_1 = require("../react/history");
|
|
10
|
-
var routes_1 = require("../react/routes");
|
|
11
|
-
var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
|
|
12
|
-
var patternWithValuesParts = urlPatternWithValues.split('/');
|
|
13
|
-
var patternWithParamsParts = urlPatternWithParams.split('/');
|
|
14
|
-
if (patternWithValuesParts.length !== patternWithParamsParts.length) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
for (var i = 0; i < patternWithValuesParts.length; i++) {
|
|
18
|
-
var valuePart = patternWithValuesParts[i];
|
|
19
|
-
var paramPart = patternWithParamsParts[i];
|
|
20
|
-
if (paramPart.startsWith(':') && paramPart !== '') {
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
if (valuePart !== paramPart) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return true;
|
|
28
|
-
};
|
|
29
|
-
var useLocation = function () {
|
|
30
|
-
var _a, _b;
|
|
31
|
-
var location = (0, react_router_dom_1.useLocation)();
|
|
32
|
-
var params = (0, react_router_dom_1.useParams)();
|
|
33
|
-
var search = location.search, hash = location.hash, pathname = location.pathname;
|
|
34
|
-
var pathNameList = (0, history_1.getPathNameList)(routes_1.routes);
|
|
35
|
-
var name = (_a = pathNameList.find(function (item) { return (params && compareURLPatterns(pathname, item.path)) || item.path === pathname; })) === null || _a === void 0 ? void 0 : _a.name;
|
|
36
|
-
var query = search ? qs_1.default.parse(search.startsWith('?') ? search.slice(1) : search) : {};
|
|
37
|
-
return {
|
|
38
|
-
name: name,
|
|
39
|
-
path: pathname,
|
|
40
|
-
pathname: (_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.pathname,
|
|
41
|
-
search: search,
|
|
42
|
-
query: query,
|
|
43
|
-
hash: hash,
|
|
44
|
-
params: params
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
exports.useLocation = useLocation;
|
|
@@ -1,40 +0,0 @@
|
|
|
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("../vue/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;
|