swico 1.3.0-beta.1 → 1.3.0-beta.2
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/README.md +21 -0
- package/package.json +1 -1
- package/src/command/start.js +35 -18
- package/src/utils/config.d.ts +3 -0
- package/src/utils/config.js +54 -37
- package/src/utils/tools.js +3 -9
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# SWICO介绍
|
|
2
|
+
|
|
3
|
+
Swico,发音近似于“斯威扣”。
|
|
4
|
+
|
|
5
|
+
为个人独立开发的一款实用的前端框架,它基于前端工程化最新技术栈实现了前端开发从创建项目,获取模板,本地开发,Git提交校验到打包构建的完整过程。
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## 主要特性
|
|
9
|
+
|
|
10
|
+
- 构建工具基于Webpack5,并配合esbuild-loader和swc-loader提升编译构建速率。
|
|
11
|
+
|
|
12
|
+
- 提供Vue3和React18两种开发模板选择,默认集成TypeScript5。同时最大化统一这两种模板的开发API。
|
|
13
|
+
|
|
14
|
+
- 提供可配置化路由方案,基于React-Router6和Vue-Router4进行二次封装,支持路由嵌套,懒加载,动态参数,装饰校验等功能。
|
|
15
|
+
|
|
16
|
+
- 内置ESLint + Prettier + Husky三大前端编码风格以及代码提交规范约束插件,保证代码风格统一以及 Git 提交规范。
|
|
17
|
+
|
|
18
|
+
- 提供配置文件可对部分项目配置进行修改扩展,例如Alias映射,本地请求代理等。
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
官方文档:[SWICO 官方文档](https://scw1997.github.io/swico-docs)
|
package/package.json
CHANGED
package/src/command/start.js
CHANGED
|
@@ -61,8 +61,10 @@ var path_1 = __importDefault(require("path"));
|
|
|
61
61
|
var _a = process.env, SWICO_PORT = _a.SWICO_PORT, SWICO_RESTART = _a.SWICO_RESTART, SWICO_ROUTER_BASE = _a.SWICO_ROUTER_BASE;
|
|
62
62
|
var package_json_1 = __importDefault(require("../../package.json"));
|
|
63
63
|
var chalk_1 = __importDefault(require("chalk"));
|
|
64
|
+
var fs_extra_1 = __importDefault(require("fs-extra"));
|
|
64
65
|
var currentPort, currentRouterBase = '/';
|
|
65
|
-
var handleWatch = function (projectPath, devServer) {
|
|
66
|
+
var handleWatch = function (projectPath, devServer, env) {
|
|
67
|
+
var envPath = env === 'dev' ? '/.dev/' : '/.prod/';
|
|
66
68
|
var configFilesWatcher = chokidar_1.default
|
|
67
69
|
.watch([path_1.default.join(projectPath, '/config/*.ts'), path_1.default.join(projectPath, '/.eslintrc')], {
|
|
68
70
|
interval: 500,
|
|
@@ -88,33 +90,48 @@ var handleWatch = function (projectPath, devServer) {
|
|
|
88
90
|
}
|
|
89
91
|
});
|
|
90
92
|
}); });
|
|
91
|
-
var
|
|
92
|
-
path_1.default.join(projectPath, '/src/loading/index.tsx'),
|
|
93
|
+
var globalStyleFilesPathList = [
|
|
93
94
|
path_1.default.join(projectPath, '/src/global.css'),
|
|
94
95
|
path_1.default.join(projectPath, '/src/global.less'),
|
|
95
96
|
path_1.default.join(projectPath, '/src/global.scss')
|
|
96
97
|
];
|
|
98
|
+
var loadingFilePath = path_1.default.join(projectPath, '/src/loading/index.tsx');
|
|
97
99
|
var tsTypingsWatcher = chokidar_1.default
|
|
98
100
|
.watch([path_1.default.join(projectPath, '/src/**/*')], {
|
|
99
101
|
interval: 500,
|
|
100
102
|
binaryInterval: 500,
|
|
101
103
|
ignoreInitial: true
|
|
102
104
|
})
|
|
103
|
-
.on('all', function (eventName,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
['add', 'unlink'].includes(eventName))) return [3, 3];
|
|
109
|
-
return [4, devServer.stop()];
|
|
105
|
+
.on('all', function (eventName, filePath, stats) { return __awaiter(void 0, void 0, void 0, function () {
|
|
106
|
+
var replaceIndexText, _a;
|
|
107
|
+
return __generator(this, function (_b) {
|
|
108
|
+
switch (_b.label) {
|
|
109
|
+
case 0: return [4, fs_extra_1.default.readFile(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), 'utf8')];
|
|
110
110
|
case 1:
|
|
111
|
-
|
|
111
|
+
replaceIndexText = _b.sent();
|
|
112
|
+
if (!['add', 'unlink'].includes(eventName)) return [3, 7];
|
|
113
|
+
_a = true;
|
|
114
|
+
switch (_a) {
|
|
115
|
+
case filePath.endsWith('.d.ts'): return [3, 2];
|
|
116
|
+
case globalStyleFilesPathList.includes(filePath): return [3, 5];
|
|
117
|
+
case filePath === loadingFilePath: return [3, 6];
|
|
118
|
+
}
|
|
119
|
+
return [3, 7];
|
|
120
|
+
case 2: return [4, devServer.stop()];
|
|
121
|
+
case 3:
|
|
122
|
+
_b.sent();
|
|
112
123
|
return [4, tsTypingsWatcher.close()];
|
|
113
|
-
case
|
|
114
|
-
|
|
124
|
+
case 4:
|
|
125
|
+
_b.sent();
|
|
115
126
|
restartServer();
|
|
116
|
-
|
|
117
|
-
case
|
|
127
|
+
return [3, 7];
|
|
128
|
+
case 5:
|
|
129
|
+
(0, config_1.handleGlobalStyleFile)(replaceIndexText);
|
|
130
|
+
return [3, 7];
|
|
131
|
+
case 6:
|
|
132
|
+
(0, config_1.handleLoadingFile)(replaceIndexText, envPath);
|
|
133
|
+
_b.label = 7;
|
|
134
|
+
case 7: return [2];
|
|
118
135
|
}
|
|
119
136
|
});
|
|
120
137
|
}); });
|
|
@@ -168,7 +185,7 @@ var createCompileListener = function (compiler) {
|
|
|
168
185
|
function start() {
|
|
169
186
|
var _a, _b, _c, _d, _e, _f;
|
|
170
187
|
return __awaiter(this, void 0, void 0, function () {
|
|
171
|
-
var availablePort, _g, projectConfig, projectPath, customConfig, newRouterBase, startConfig, compiler, oriLogger, devServer, e_1, strErr;
|
|
188
|
+
var availablePort, _g, projectConfig, projectPath, customConfig, env, newRouterBase, startConfig, compiler, oriLogger, devServer, e_1, strErr;
|
|
172
189
|
return __generator(this, function (_h) {
|
|
173
190
|
switch (_h.label) {
|
|
174
191
|
case 0:
|
|
@@ -192,7 +209,7 @@ function start() {
|
|
|
192
209
|
return [4, (0, config_1.getProjectConfig)('dev')];
|
|
193
210
|
case 5:
|
|
194
211
|
projectConfig = _h.sent();
|
|
195
|
-
projectPath = projectConfig.projectPath, customConfig = projectConfig.customConfig;
|
|
212
|
+
projectPath = projectConfig.projectPath, customConfig = projectConfig.customConfig, env = projectConfig.env;
|
|
196
213
|
newRouterBase = (_f = (_c = (_b = (_a = customConfig['dev']) === null || _a === void 0 ? void 0 : _a.router) === null || _b === void 0 ? void 0 : _b.base) !== null && _c !== void 0 ? _c : (_e = (_d = customConfig['base']) === null || _d === void 0 ? void 0 : _d.router) === null || _e === void 0 ? void 0 : _e.base) !== null && _f !== void 0 ? _f : '/';
|
|
197
214
|
return [4, (0, webpack_dev_1.default)(projectConfig)];
|
|
198
215
|
case 6:
|
|
@@ -210,7 +227,7 @@ function start() {
|
|
|
210
227
|
process.env.SWICO_PORT = availablePort.toString();
|
|
211
228
|
createCompileListener(compiler);
|
|
212
229
|
compiler.getInfrastructureLogger = oriLogger;
|
|
213
|
-
handleWatch(projectPath, devServer);
|
|
230
|
+
handleWatch(projectPath, devServer, env);
|
|
214
231
|
if (SWICO_RESTART !== 'true' ||
|
|
215
232
|
(SWICO_RESTART === 'true' && newRouterBase !== SWICO_ROUTER_BASE)) {
|
|
216
233
|
tools_1.toast.info("Project is running at\uFF1A".concat(chalk_1.default.hex('#29abe0')("".concat(startConfig.devServer.server, "://localhost:").concat(availablePort).concat(newRouterBase))));
|
package/src/utils/config.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type ConfigRoutesItemType = {
|
|
|
5
5
|
redirect?: string;
|
|
6
6
|
name?: string;
|
|
7
7
|
decorator?: string;
|
|
8
|
+
custom?: any;
|
|
8
9
|
[key: string]: any;
|
|
9
10
|
};
|
|
10
11
|
export type ConfigRouterType = {
|
|
@@ -43,5 +44,7 @@ export interface GlobalData {
|
|
|
43
44
|
};
|
|
44
45
|
}
|
|
45
46
|
export declare const getProjectConfig: (env: GlobalData['env']) => Promise<GlobalData>;
|
|
47
|
+
export declare const handleLoadingFile: (replaceIndexText: any, envPath: any) => Promise<void>;
|
|
48
|
+
export declare const handleGlobalStyleFile: (replaceIndexText: any) => any;
|
|
46
49
|
export declare const getFormatDefineVars: (defineVarsConfigData: any) => Promise<{}>;
|
|
47
50
|
export declare const initConfig: Omit<GlobalSwicoConfigType, 'template'>;
|
package/src/utils/config.js
CHANGED
|
@@ -73,7 +73,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
73
73
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
74
74
|
};
|
|
75
75
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
76
|
-
exports.initConfig = exports.getFormatDefineVars = exports.getProjectConfig = void 0;
|
|
76
|
+
exports.initConfig = exports.getFormatDefineVars = exports.handleGlobalStyleFile = exports.handleLoadingFile = exports.getProjectConfig = void 0;
|
|
77
77
|
var fs_extra_1 = __importDefault(require("fs-extra"));
|
|
78
78
|
var path_1 = __importDefault(require("path"));
|
|
79
79
|
var chalk_1 = __importDefault(require("chalk"));
|
|
@@ -286,12 +286,49 @@ var getGlobalStyleFilePath = function () {
|
|
|
286
286
|
}
|
|
287
287
|
return { filePath: filePath, fileType: fileType };
|
|
288
288
|
};
|
|
289
|
+
var handleLoadingFile = function (replaceIndexText, envPath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
290
|
+
var e_1;
|
|
291
|
+
return __generator(this, function (_a) {
|
|
292
|
+
switch (_a.label) {
|
|
293
|
+
case 0:
|
|
294
|
+
_a.trys.push([0, 2, 3, 5]);
|
|
295
|
+
return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
|
|
296
|
+
case 1:
|
|
297
|
+
_a.sent();
|
|
298
|
+
replaceIndexText = replaceIndexText.replace('"../loading"', '"../../loading"');
|
|
299
|
+
return [3, 5];
|
|
300
|
+
case 2:
|
|
301
|
+
e_1 = _a.sent();
|
|
302
|
+
replaceIndexText = replaceIndexText.replace('"../../loading"', '"../loading"');
|
|
303
|
+
return [3, 5];
|
|
304
|
+
case 3: return [4, fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), replaceIndexText)];
|
|
305
|
+
case 4:
|
|
306
|
+
_a.sent();
|
|
307
|
+
return [7];
|
|
308
|
+
case 5: return [2];
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}); };
|
|
312
|
+
exports.handleLoadingFile = handleLoadingFile;
|
|
313
|
+
var handleGlobalStyleFile = function (replaceIndexText) {
|
|
314
|
+
var _a = getGlobalStyleFilePath(), styleFilePath = _a.filePath, styleFileType = _a.fileType;
|
|
315
|
+
var newReplaceIndexText = replaceIndexText;
|
|
316
|
+
if (styleFilePath) {
|
|
317
|
+
newReplaceIndexText = newReplaceIndexText.replaceAll("require(\"../../global.".concat(styleFileType, "\");"), '');
|
|
318
|
+
newReplaceIndexText = "require(\"../../global.".concat(styleFileType, "\");\n").concat(replaceIndexText);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
newReplaceIndexText = newReplaceIndexText.replaceAll(/require\("\.\.\/\.\.\/global.(less|scss|css)"\);/g, '');
|
|
322
|
+
}
|
|
323
|
+
return newReplaceIndexText;
|
|
324
|
+
};
|
|
325
|
+
exports.handleGlobalStyleFile = handleGlobalStyleFile;
|
|
289
326
|
var initTemplateConfig = function (routerConfig, templateType, env) {
|
|
290
327
|
var envPath = env === 'dev' ? '/.dev/' : '/.prod/';
|
|
291
328
|
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
292
|
-
var copyTargetPath, replaceIndexText, hooksFilePath, replaceHooksText
|
|
293
|
-
return __generator(this, function (
|
|
294
|
-
switch (
|
|
329
|
+
var copyTargetPath, replaceIndexText, hooksFilePath, replaceHooksText;
|
|
330
|
+
return __generator(this, function (_a) {
|
|
331
|
+
switch (_a.label) {
|
|
295
332
|
case 0:
|
|
296
333
|
copyTargetPath = path_1.default.resolve(projectPath, "./src/.swico".concat(envPath));
|
|
297
334
|
return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../template/".concat(templateType, "/$env")), copyTargetPath, function (fileName) {
|
|
@@ -301,59 +338,39 @@ var initTemplateConfig = function (routerConfig, templateType, env) {
|
|
|
301
338
|
: !['react-hooks.js'].includes(fileName));
|
|
302
339
|
})];
|
|
303
340
|
case 1:
|
|
304
|
-
|
|
341
|
+
_a.sent();
|
|
305
342
|
return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../template/".concat(templateType)), path_1.default.resolve(projectPath, './src/.swico'), function (fileName) {
|
|
306
343
|
return ['react-hooks.js', 'vue-hooks.js', 'loading.js', 'Container.vue'].includes(fileName);
|
|
307
344
|
})];
|
|
308
345
|
case 2:
|
|
309
|
-
|
|
346
|
+
_a.sent();
|
|
310
347
|
return [4, formatRouterConfig(routerConfig, templateType, env)];
|
|
311
348
|
case 3:
|
|
312
|
-
|
|
349
|
+
_a.sent();
|
|
313
350
|
return [4, fs_extra_1.default.readFile(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), 'utf8')];
|
|
314
351
|
case 4:
|
|
315
|
-
replaceIndexText =
|
|
352
|
+
replaceIndexText = _a.sent();
|
|
316
353
|
hooksFilePath = path_1.default.resolve(projectPath, "./src/.swico/".concat(templateType, "-hooks.js"));
|
|
317
354
|
return [4, fs_extra_1.default.readFile(hooksFilePath, 'utf8')];
|
|
318
355
|
case 5:
|
|
319
|
-
replaceHooksText =
|
|
356
|
+
replaceHooksText = _a.sent();
|
|
320
357
|
replaceHooksText = replaceHooksText.replaceAll('$env', ".".concat(env));
|
|
321
358
|
return [4, fs_extra_1.default.writeFile(hooksFilePath, replaceHooksText)];
|
|
322
359
|
case 6:
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if (styleFilePath) {
|
|
326
|
-
replaceIndexText = replaceIndexText.replaceAll("require(\"../../global.".concat(styleFileType, "\");"), '');
|
|
327
|
-
replaceIndexText = "require(\"../../global.".concat(styleFileType, "\");\n").concat(replaceIndexText);
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
replaceIndexText = replaceIndexText.replaceAll(/require\("\.\.\/\.\.\/global.(less|scss|css)"\);/g, '');
|
|
331
|
-
}
|
|
360
|
+
_a.sent();
|
|
361
|
+
replaceIndexText = (0, exports.handleGlobalStyleFile)(replaceIndexText);
|
|
332
362
|
if (!(templateType === 'vue')) return [3, 8];
|
|
333
363
|
replaceIndexText = replaceIndexText.replace('"../Container"', '"../Container.vue"');
|
|
334
364
|
return [4, fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), replaceIndexText)];
|
|
335
365
|
case 7:
|
|
336
|
-
|
|
337
|
-
return [3,
|
|
366
|
+
_a.sent();
|
|
367
|
+
return [3, 9];
|
|
338
368
|
case 8:
|
|
339
|
-
if (
|
|
340
|
-
|
|
369
|
+
if (templateType === 'react') {
|
|
370
|
+
(0, exports.handleLoadingFile)(replaceIndexText, envPath);
|
|
371
|
+
}
|
|
372
|
+
_a.label = 9;
|
|
341
373
|
case 9:
|
|
342
|
-
_b.trys.push([9, 11, 12, 14]);
|
|
343
|
-
return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
|
|
344
|
-
case 10:
|
|
345
|
-
_b.sent();
|
|
346
|
-
replaceIndexText = replaceIndexText.replace('"../loading"', '"../../loading"');
|
|
347
|
-
return [3, 14];
|
|
348
|
-
case 11:
|
|
349
|
-
e_1 = _b.sent();
|
|
350
|
-
replaceIndexText = replaceIndexText.replace('"../../loading"', '"../loading"');
|
|
351
|
-
return [3, 14];
|
|
352
|
-
case 12: return [4, fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, "./src/.swico".concat(envPath, "index.js")), replaceIndexText)];
|
|
353
|
-
case 13:
|
|
354
|
-
_b.sent();
|
|
355
|
-
return [7];
|
|
356
|
-
case 14:
|
|
357
374
|
resolve(null);
|
|
358
375
|
return [2];
|
|
359
376
|
}
|
package/src/utils/tools.js
CHANGED
|
@@ -170,7 +170,7 @@ var copyDirFiles = function (src, dest, filter) { return __awaiter(void 0, void
|
|
|
170
170
|
exports.copyDirFiles = copyDirFiles;
|
|
171
171
|
var getFormatRouter = function (projectPath, routes, templateType) {
|
|
172
172
|
var _main = function (item) {
|
|
173
|
-
var
|
|
173
|
+
var component = item.component, name = item.name, children = item.children, redirect = item.redirect, decorator = item.decorator;
|
|
174
174
|
return decorator
|
|
175
175
|
? __assign(__assign({}, item), { component: "()=>import('".concat(projectPath, "/src/pages/").concat(decorator).concat(templateType === 'vue' ? '.vue' : '', "')"), children: [
|
|
176
176
|
{
|
|
@@ -182,15 +182,9 @@ var getFormatRouter = function (projectPath, routes, templateType) {
|
|
|
182
182
|
redirect: redirect,
|
|
183
183
|
children: children ? children === null || children === void 0 ? void 0 : children.map(function (item) { return _main(item); }) : undefined
|
|
184
184
|
}
|
|
185
|
-
] }) : {
|
|
186
|
-
path: path,
|
|
187
|
-
component: component
|
|
185
|
+
] }) : __assign(__assign({}, item), { component: component
|
|
188
186
|
? "()=>import('".concat(projectPath, "/src/pages/").concat(component).concat(templateType === 'vue' ? '.vue' : '', "')")
|
|
189
|
-
: undefined,
|
|
190
|
-
name: name,
|
|
191
|
-
redirect: redirect,
|
|
192
|
-
children: children ? children === null || children === void 0 ? void 0 : children.map(function (item) { return _main(item); }) : undefined
|
|
193
|
-
};
|
|
187
|
+
: undefined, name: name, redirect: redirect, children: children ? children === null || children === void 0 ? void 0 : children.map(function (item) { return _main(item); }) : undefined });
|
|
194
188
|
};
|
|
195
189
|
return routes.map(function (item) {
|
|
196
190
|
return _main(item);
|