swico 1.7.3 → 2.0.0-beta.10

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.
Files changed (32) hide show
  1. package/package.json +10 -21
  2. package/src/bin.js +6 -6
  3. package/src/command/build.js +33 -73
  4. package/src/command/start.js +100 -192
  5. package/src/config/rsbpack.dev.js +104 -0
  6. package/src/config/{webpack.base.react.d.ts → rspack.base.react.d.ts} +43 -6
  7. package/src/config/rspack.base.react.js +195 -0
  8. package/src/config/{webpack.base.vue.d.ts → rspack.base.vue.d.ts} +32 -32
  9. package/src/config/rspack.base.vue.js +167 -0
  10. package/src/config/rspack.prod.js +122 -0
  11. package/src/index.js +3 -5
  12. package/src/index.react.js +5 -10
  13. package/src/index.vue.js +4 -6
  14. package/src/project-path/.swico-react/$env/history.js +65 -66
  15. package/src/project-path/.swico-react/$env/index.js +29 -47
  16. package/src/project-path/.swico-react/loading.js +1 -1
  17. package/src/project-path/.swico-react/react-hooks.js +24 -37
  18. package/src/project-path/.swico-vue/$env/history.js +25 -36
  19. package/src/project-path/.swico-vue/$env/index.js +7 -7
  20. package/src/project-path/.swico-vue/Container.js +1 -1
  21. package/src/project-path/.swico-vue/vue-hooks.js +17 -28
  22. package/src/project-path/src/layout/Layout.js +1 -1
  23. package/src/project-path/src/layout/index.js +3 -3
  24. package/src/task.js +13 -58
  25. package/src/utils/config.js +185 -349
  26. package/src/utils/tools.js +103 -199
  27. package/src/config/webpack.base.react.js +0 -315
  28. package/src/config/webpack.base.vue.js +0 -338
  29. package/src/config/webpack.dev.js +0 -159
  30. package/src/config/webpack.prod.js +0 -193
  31. /package/src/config/{webpack.dev.d.ts → rsbpack.dev.d.ts} +0 -0
  32. /package/src/config/{webpack.prod.d.ts → rspack.prod.d.ts} +0 -0
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -42,232 +31,140 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
42
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
43
32
  });
44
33
  };
45
- var __generator = (this && this.__generator) || function (thisArg, body) {
46
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
- function verb(n) { return function (v) { return step([n, v]); }; }
49
- function step(op) {
50
- if (f) throw new TypeError("Generator is already executing.");
51
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
52
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
- if (y = 0, t) op = [op[0] & 2, t.value];
54
- switch (op[0]) {
55
- case 0: case 1: t = op; break;
56
- case 4: _.label++; return { value: op[1], done: false };
57
- case 5: _.label++; y = op[1]; op = [0]; continue;
58
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
- default:
60
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
- if (t[2]) _.ops.pop();
65
- _.trys.pop(); continue;
66
- }
67
- op = body.call(thisArg, _);
68
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
- }
71
- };
72
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
73
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
74
36
  };
75
37
  Object.defineProperty(exports, "__esModule", { value: true });
76
38
  exports.initConfig = exports.getFormatDefineVars = exports.updateIndexFileText = exports.handleGlobalStyleFile = exports.handleLoadingFile = exports.getProjectConfig = void 0;
77
- var fs_extra_1 = __importDefault(require("fs-extra"));
78
- var path_1 = __importDefault(require("path"));
79
- var chalk_1 = __importDefault(require("chalk"));
80
- var tools_1 = require("./tools");
81
- var getProjectConfig = function (env) { return __awaiter(void 0, void 0, void 0, function () {
82
- var projectPath, configDir, configPath, customConfig, _loop_1, _i, _a, key, templateType, routerConfig, envPath, entryPath, templatePath;
83
- var _b, _c;
84
- return __generator(this, function (_d) {
85
- switch (_d.label) {
86
- case 0:
87
- projectPath = process.cwd();
88
- configDir = path_1.default.join(projectPath, '/config');
89
- configPath = {
90
- dev: path_1.default.join(configDir, '/swico.dev.ts'),
91
- prod: path_1.default.join(configDir, '/swico.prod.ts'),
92
- base: path_1.default.join(configDir, '/swico.ts')
93
- };
94
- customConfig = {};
95
- _loop_1 = function (key) {
96
- var curConfigFilePath, exists, configObj, configFields, supportedFieldList_1, configFileName, toastTitle, unSupportedField, msgText, _e, _f;
97
- return __generator(this, function (_g) {
98
- switch (_g.label) {
99
- case 0:
100
- curConfigFilePath = configPath[key];
101
- return [4, fs_extra_1.default.exists(curConfigFilePath)];
102
- case 1:
103
- exists = _g.sent();
104
- if (!exists) return [3, 4];
105
- return [4, Promise.resolve("".concat(curConfigFilePath)).then(function (s) { return __importStar(require(s)); })];
106
- case 2:
107
- configObj = (_g.sent()).default;
108
- configFields = Object.keys(configObj);
109
- configFileName = void 0;
110
- switch (key) {
111
- case 'base':
112
- supportedFieldList_1 = [
113
- 'plugins',
114
- 'publicPath',
115
- 'alias',
116
- 'define',
117
- 'devtool',
118
- 'externals',
119
- 'router',
120
- 'template'
121
- ];
122
- configFileName = 'swico.ts';
123
- break;
124
- case 'dev':
125
- supportedFieldList_1 = ['plugins', 'proxy', 'https', 'devtool', 'router'];
126
- configFileName = 'swico.dev.ts';
127
- break;
128
- case 'prod':
129
- supportedFieldList_1 = ['plugins', 'console', 'copy', 'devtool', 'router'];
130
- configFileName = 'swico.prod.ts';
131
- }
132
- toastTitle = "Swico config file => ".concat(chalk_1.default.hex(tools_1.colorConfig.theme)(configFileName));
133
- unSupportedField = configFields.find(function (field) { return !supportedFieldList_1.includes(field); });
134
- if (configFields.length > 0 && unSupportedField) {
135
- msgText = "The Swico configuration file '".concat(chalk_1.default.blue(configFileName), "' does not support the field '").concat(chalk_1.default.red(unSupportedField), "' ");
136
- tools_1.toast.error(msgText);
137
- process.exit(1);
138
- }
139
- if (key === 'base' && !['vue', 'react'].includes(configObj['template'])) {
140
- tools_1.toast.error("The field '".concat(chalk_1.default.blue('template'), "' does not support the value '").concat(chalk_1.default.red(configObj['template']), "',the value should be 'vue' or 'react' "), { title: toastTitle });
141
- process.exit(1);
142
- }
143
- if (!['hash', 'browser'].includes((_c = (_b = configObj['router']) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : exports.initConfig.router.type)) {
144
- tools_1.toast.error("The field '".concat(chalk_1.default.blue('router.type'), "' does not support the value '").concat(chalk_1.default.red(configObj['router'].type), "',the value should be 'browser' or 'hash' "), { title: toastTitle });
145
- process.exit(1);
146
- }
147
- _e = customConfig;
148
- _f = key;
149
- return [4, Promise.resolve("".concat(curConfigFilePath)).then(function (s) { return __importStar(require(s)); })];
150
- case 3:
151
- _e[_f] = (_g.sent()).default;
152
- _g.label = 4;
153
- case 4: return [2];
154
- }
155
- });
156
- };
157
- _i = 0, _a = Object.keys(configPath);
158
- _d.label = 1;
159
- case 1:
160
- if (!(_i < _a.length)) return [3, 4];
161
- key = _a[_i];
162
- return [5, _loop_1(key)];
163
- case 2:
164
- _d.sent();
165
- _d.label = 3;
166
- case 3:
167
- _i++;
168
- return [3, 1];
169
- case 4:
170
- templateType = customConfig['base'].template;
171
- routerConfig = __assign(__assign(__assign({}, exports.initConfig.router), customConfig['base'].router), customConfig[env].router);
172
- return [4, initTemplateConfig(routerConfig, templateType, env)];
173
- case 5:
174
- _d.sent();
175
- return [4, initCliIndexFile(templateType, env)];
176
- case 6:
177
- _d.sent();
178
- envPath = env === 'dev' ? '.dev/' : '.prod/';
179
- entryPath = path_1.default.resolve(projectPath, "./.swico/".concat(envPath, "index.js"));
180
- templatePath = path_1.default.join(projectPath, '/src/index.ejs');
181
- return [2, {
182
- env: env,
183
- projectPath: projectPath,
184
- entryPath: entryPath,
185
- templatePath: templatePath,
186
- customConfig: customConfig,
187
- templateType: templateType
188
- }];
39
+ const fs_extra_1 = __importDefault(require("fs-extra"));
40
+ const path_1 = __importDefault(require("path"));
41
+ const chalk_1 = __importDefault(require("chalk"));
42
+ const tools_1 = require("./tools");
43
+ const getProjectConfig = (env) => __awaiter(void 0, void 0, void 0, function* () {
44
+ var _a, _b;
45
+ const projectPath = process.cwd();
46
+ const configDir = path_1.default.join(projectPath, '/config');
47
+ const configPath = {
48
+ dev: path_1.default.join(configDir, '/swico.dev.ts'),
49
+ prod: path_1.default.join(configDir, '/swico.prod.ts'),
50
+ base: path_1.default.join(configDir, '/swico.ts')
51
+ };
52
+ const customConfig = {};
53
+ for (const key of Object.keys(configPath)) {
54
+ const curConfigFilePath = configPath[key];
55
+ const exists = yield fs_extra_1.default.exists(curConfigFilePath);
56
+ if (exists) {
57
+ const configObj = (yield Promise.resolve(`${curConfigFilePath}`).then(s => __importStar(require(s)))).default;
58
+ const configFields = Object.keys(configObj);
59
+ let supportedFieldList, configFileName;
60
+ switch (key) {
61
+ case 'base':
62
+ supportedFieldList = [
63
+ 'plugins',
64
+ 'publicPath',
65
+ 'alias',
66
+ 'define',
67
+ 'devtool',
68
+ 'externals',
69
+ 'router',
70
+ 'template'
71
+ ];
72
+ configFileName = 'swico.ts';
73
+ break;
74
+ case 'dev':
75
+ supportedFieldList = ['plugins', 'proxy', 'https', 'devtool', 'router'];
76
+ configFileName = 'swico.dev.ts';
77
+ break;
78
+ case 'prod':
79
+ supportedFieldList = ['plugins', 'console', 'copy', 'devtool', 'router'];
80
+ configFileName = 'swico.prod.ts';
81
+ }
82
+ const toastTitle = `Swico config file => ${chalk_1.default.hex(tools_1.colorConfig.theme)(configFileName)}`;
83
+ const unSupportedField = configFields.find((field) => !supportedFieldList.includes(field));
84
+ if (configFields.length > 0 && unSupportedField) {
85
+ const msgText = `The Swico configuration file '${chalk_1.default.blue(configFileName)}' does not support the field '${chalk_1.default.red(unSupportedField)}' `;
86
+ tools_1.toast.error(msgText);
87
+ process.exit(1);
88
+ }
89
+ if (key === 'base' && !['vue', 'react'].includes(configObj['template'])) {
90
+ tools_1.toast.error(`The field '${chalk_1.default.blue('template')}' does not support the value '${chalk_1.default.red(configObj['template'])}',the value should be 'vue' or 'react' `, { title: toastTitle });
91
+ process.exit(1);
92
+ }
93
+ if (!['hash', 'browser'].includes((_b = (_a = configObj['router']) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : exports.initConfig.router.type)) {
94
+ tools_1.toast.error(`The field '${chalk_1.default.blue('router.type')}' does not support the value '${chalk_1.default.red(configObj['router'].type)}',the value should be 'browser' or 'hash' `, { title: toastTitle });
95
+ process.exit(1);
96
+ }
97
+ customConfig[key] = (yield Promise.resolve(`${curConfigFilePath}`).then(s => __importStar(require(s)))).default;
189
98
  }
190
- });
191
- }); };
99
+ }
100
+ const templateType = customConfig['base'].template;
101
+ const routerConfig = Object.assign(Object.assign(Object.assign({}, exports.initConfig.router), customConfig['base'].router), customConfig[env].router);
102
+ yield initTemplateConfig(routerConfig, templateType, env);
103
+ yield initCliIndexFile(templateType, env);
104
+ const envPath = env === 'dev' ? '.dev/' : '.prod/';
105
+ const entryPath = path_1.default.resolve(projectPath, `./.swico/${envPath}index.js`);
106
+ const templatePath = path_1.default.join(projectPath, '/src/index.ejs');
107
+ return {
108
+ env,
109
+ projectPath,
110
+ entryPath,
111
+ templatePath,
112
+ customConfig,
113
+ templateType
114
+ };
115
+ });
192
116
  exports.getProjectConfig = getProjectConfig;
193
- var initCliIndexFile = function (templateType, env) { return __awaiter(void 0, void 0, void 0, function () {
194
- var projectPath, targetPath, targetTypesPath, replaceFileText, fileTypesText, formatHooksPath, formatHistoryPath;
195
- return __generator(this, function (_a) {
196
- switch (_a.label) {
197
- case 0:
198
- projectPath = process.cwd();
199
- targetPath = path_1.default.resolve(__dirname, '../index.js');
200
- targetTypesPath = path_1.default.resolve(__dirname, '../index.d.ts');
201
- return [4, fs_extra_1.default.readFile(path_1.default.resolve(__dirname, "../index.".concat(templateType, ".js")), 'utf8')];
202
- case 1:
203
- replaceFileText = _a.sent();
204
- return [4, fs_extra_1.default.readFile(path_1.default.resolve(__dirname, "../index.".concat(templateType, ".d.ts")), 'utf8')];
205
- case 2:
206
- fileTypesText = _a.sent();
207
- if (templateType === 'react') {
208
- formatHooksPath = path_1.default
209
- .resolve(projectPath, './.swico/react-hooks')
210
- .replaceAll('\\', '/');
211
- replaceFileText = replaceFileText.replaceAll('require("./project-path/.swico-react/react-hooks");', "require(\"".concat(formatHooksPath, "\");"));
212
- }
213
- formatHistoryPath = path_1.default
214
- .resolve(projectPath, "./.swico/.".concat(env, "/history"))
215
- .replaceAll('\\', '/');
216
- replaceFileText = replaceFileText.replaceAll('require("./mock-history");', "require(\"".concat(formatHistoryPath, "\");"));
217
- return [4, fs_extra_1.default.writeFile(targetPath, replaceFileText)];
218
- case 3:
219
- _a.sent();
220
- return [4, fs_extra_1.default.writeFile(targetTypesPath, fileTypesText)];
221
- case 4:
222
- _a.sent();
223
- return [2];
117
+ const initCliIndexFile = (templateType, env) => __awaiter(void 0, void 0, void 0, function* () {
118
+ const projectPath = process.cwd();
119
+ const targetPath = path_1.default.resolve(__dirname, '../index.js');
120
+ const targetTypesPath = path_1.default.resolve(__dirname, '../index.d.ts');
121
+ let replaceFileText = yield fs_extra_1.default.readFile(path_1.default.resolve(__dirname, `../index.${templateType}.js`), 'utf8');
122
+ const fileTypesText = yield fs_extra_1.default.readFile(path_1.default.resolve(__dirname, `../index.${templateType}.d.ts`), 'utf8');
123
+ if (templateType === 'react') {
124
+ const formatHooksPath = path_1.default
125
+ .resolve(projectPath, './.swico/react-hooks')
126
+ .replaceAll('\\', '/');
127
+ replaceFileText = replaceFileText.replaceAll('require("./project-path/.swico-react/react-hooks");', `require("${formatHooksPath}");`);
128
+ }
129
+ const formatHistoryPath = path_1.default
130
+ .resolve(projectPath, `./.swico/.${env}/history`)
131
+ .replaceAll('\\', '/');
132
+ replaceFileText = replaceFileText.replaceAll('require("./mock-history");', `require("${formatHistoryPath}");`);
133
+ yield fs_extra_1.default.writeFile(targetPath, replaceFileText);
134
+ yield fs_extra_1.default.writeFile(targetTypesPath, fileTypesText);
135
+ });
136
+ const formatRouterConfig = ({ routes = [], type = 'browser', base = '/' }, templateType, env) => {
137
+ const projectPath = process.cwd();
138
+ const envPath = env === 'dev' ? '/.dev/' : '/.prod/';
139
+ return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
140
+ const formatRouter = (0, tools_1.getFormatRouter)(projectPath, routes, templateType);
141
+ const textData = `"use strict";
142
+ Object.defineProperty(exports, "__esModule", { value: true });
143
+ exports.default = ${JSON.stringify(formatRouter)};`;
144
+ let modifiedText = textData.replace(/"(\(\)=>import\('[^']+'\))"/g, '$1');
145
+ yield fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, `./.swico${envPath}routes.js`), modifiedText);
146
+ const configFilePath = path_1.default.resolve(projectPath, `./.swico${envPath}config.js`);
147
+ let replaceConfigText = yield fs_extra_1.default.readFile(configFilePath, 'utf8');
148
+ if (base) {
149
+ replaceConfigText = replaceConfigText.replace(/exports\.routerBase\s*=\s*(.*)/gm, function (match, p1) {
150
+ return `exports.routerBase = '${templateType === 'react' && type === 'hash' ? '/' : base}';`;
151
+ });
224
152
  }
225
- });
226
- }); };
227
- var formatRouterConfig = function (_a, templateType, env) {
228
- 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;
229
- var projectPath = process.cwd();
230
- var envPath = env === 'dev' ? '/.dev/' : '/.prod/';
231
- return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
232
- var formatRouter, textData, modifiedText, configFilePath, replaceConfigText;
233
- return __generator(this, function (_a) {
234
- switch (_a.label) {
235
- case 0:
236
- formatRouter = (0, tools_1.getFormatRouter)(projectPath, routes, templateType);
237
- textData = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = ".concat(JSON.stringify(formatRouter), ";");
238
- modifiedText = textData.replace(/"(\(\)=>import\('[^']+'\))"/g, '$1');
239
- return [4, fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, "./.swico".concat(envPath, "routes.js")), modifiedText)];
240
- case 1:
241
- _a.sent();
242
- configFilePath = path_1.default.resolve(projectPath, "./.swico".concat(envPath, "config.js"));
243
- return [4, fs_extra_1.default.readFile(configFilePath, 'utf8')];
244
- case 2:
245
- replaceConfigText = _a.sent();
246
- if (base) {
247
- replaceConfigText = replaceConfigText.replace(/exports\.routerBase\s*=\s*(.*)/gm, function (match, p1) {
248
- return "exports.routerBase = '".concat(templateType === 'react' && type === 'hash' ? '/' : base, "';");
249
- });
250
- }
251
- if (type) {
252
- replaceConfigText = replaceConfigText.replace(/exports\.routerType\s*=\s*(.*)/gm, function (match, p1) {
253
- return "exports.routerType = '".concat(type, "';");
254
- });
255
- }
256
- return [4, fs_extra_1.default.writeFile(configFilePath, replaceConfigText)];
257
- case 3:
258
- _a.sent();
259
- resolve(null);
260
- return [2];
261
- }
262
- });
263
- }); });
153
+ if (type) {
154
+ replaceConfigText = replaceConfigText.replace(/exports\.routerType\s*=\s*(.*)/gm, function (match, p1) {
155
+ return `exports.routerType = '${type}';`;
156
+ });
157
+ }
158
+ yield fs_extra_1.default.writeFile(configFilePath, replaceConfigText);
159
+ resolve(null);
160
+ }));
264
161
  };
265
- var getGlobalStyleFilePath = function () {
266
- var projectPath = process.cwd();
267
- var filePath, fileType;
268
- var cssPath = path_1.default.resolve(projectPath, './src/global.css');
269
- var lessPath = path_1.default.resolve(projectPath, './src/global.less');
270
- var scssPath = path_1.default.resolve(projectPath, './src/global.scss');
162
+ const getGlobalStyleFilePath = () => {
163
+ const projectPath = process.cwd();
164
+ let filePath, fileType;
165
+ const cssPath = path_1.default.resolve(projectPath, './src/global.css');
166
+ const lessPath = path_1.default.resolve(projectPath, './src/global.less');
167
+ const scssPath = path_1.default.resolve(projectPath, './src/global.scss');
271
168
  switch (true) {
272
169
  case fs_extra_1.default.existsSync(cssPath):
273
170
  filePath = cssPath;
@@ -284,38 +181,27 @@ var getGlobalStyleFilePath = function () {
284
181
  default:
285
182
  break;
286
183
  }
287
- return { filePath: filePath, fileType: fileType };
184
+ return { filePath, fileType };
288
185
  };
289
- var handleLoadingFile = function (replaceIndexText, envPath) { return __awaiter(void 0, void 0, void 0, function () {
290
- var projectPath, newReplaceIndexText, e_1;
291
- return __generator(this, function (_a) {
292
- switch (_a.label) {
293
- case 0:
294
- projectPath = process.cwd();
295
- newReplaceIndexText = replaceIndexText;
296
- _a.label = 1;
297
- case 1:
298
- _a.trys.push([1, 3, , 4]);
299
- return [4, fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK)];
300
- case 2:
301
- _a.sent();
302
- newReplaceIndexText = newReplaceIndexText.replace('"../loading"', '"../../src/loading"');
303
- return [3, 4];
304
- case 3:
305
- e_1 = _a.sent();
306
- newReplaceIndexText = newReplaceIndexText.replace('"../../src/loading"', '"../loading"');
307
- return [3, 4];
308
- case 4: return [2, newReplaceIndexText];
309
- }
310
- });
311
- }); };
186
+ const handleLoadingFile = (replaceIndexText, envPath) => __awaiter(void 0, void 0, void 0, function* () {
187
+ const projectPath = process.cwd();
188
+ let newReplaceIndexText = replaceIndexText;
189
+ try {
190
+ yield fs_extra_1.default.access(path_1.default.resolve(projectPath, './src/loading/index.tsx'), fs_extra_1.default.constants.F_OK);
191
+ newReplaceIndexText = newReplaceIndexText.replace('"../loading"', '"../../src/loading"');
192
+ }
193
+ catch (e) {
194
+ newReplaceIndexText = newReplaceIndexText.replace('"../../src/loading"', '"../loading"');
195
+ }
196
+ return newReplaceIndexText;
197
+ });
312
198
  exports.handleLoadingFile = handleLoadingFile;
313
- var handleGlobalStyleFile = function (replaceIndexText) {
314
- var _a = getGlobalStyleFilePath(), styleFilePath = _a.filePath, styleFileType = _a.fileType;
315
- var newReplaceIndexText = replaceIndexText;
199
+ const handleGlobalStyleFile = (replaceIndexText) => {
200
+ const { filePath: styleFilePath, fileType: styleFileType } = getGlobalStyleFilePath();
201
+ let newReplaceIndexText = replaceIndexText;
316
202
  if (styleFilePath) {
317
- newReplaceIndexText = newReplaceIndexText.replaceAll("require(\"../../src/global.".concat(styleFileType, "\");"), '');
318
- newReplaceIndexText = "require(\"../../src/global.".concat(styleFileType, "\");\n").concat(replaceIndexText);
203
+ newReplaceIndexText = newReplaceIndexText.replaceAll(`require("../../src/global.${styleFileType}");`, '');
204
+ newReplaceIndexText = `require("../../src/global.${styleFileType}");\n${replaceIndexText}`;
319
205
  }
320
206
  else {
321
207
  newReplaceIndexText = newReplaceIndexText.replaceAll(/require\("\.\.\/\.\.\/src\/global.(less|scss|css)"\);/g, '');
@@ -323,100 +209,50 @@ var handleGlobalStyleFile = function (replaceIndexText) {
323
209
  return newReplaceIndexText;
324
210
  };
325
211
  exports.handleGlobalStyleFile = handleGlobalStyleFile;
326
- var updateIndexFileText = function (envPath, newFileText) { return __awaiter(void 0, void 0, void 0, function () {
327
- var projectPath;
328
- return __generator(this, function (_a) {
329
- switch (_a.label) {
330
- case 0:
331
- projectPath = process.cwd();
332
- return [4, fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, "./.swico".concat(envPath, "index.js")), newFileText)];
333
- case 1:
334
- _a.sent();
335
- return [2];
336
- }
337
- });
338
- }); };
212
+ const updateIndexFileText = (envPath, newFileText) => __awaiter(void 0, void 0, void 0, function* () {
213
+ const projectPath = process.cwd();
214
+ yield fs_extra_1.default.writeFile(path_1.default.resolve(projectPath, `./.swico${envPath}index.js`), newFileText);
215
+ });
339
216
  exports.updateIndexFileText = updateIndexFileText;
340
- var initTemplateConfig = function (routerConfig, templateType, env) {
341
- var projectPath = process.cwd();
342
- var envPath = env === 'dev' ? '/.dev/' : '/.prod/';
343
- return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
344
- var copyTargetPath, replaceIndexText, hooksFilePath, replaceHooksText, newReplaceIndexText;
345
- return __generator(this, function (_a) {
346
- switch (_a.label) {
347
- case 0:
348
- copyTargetPath = path_1.default.resolve(projectPath, "./.swico".concat(envPath));
349
- return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../project-path/.swico-".concat(templateType, "/$env")), copyTargetPath, function (fileName) {
350
- return !fileName.endsWith('.d.ts') &&
351
- (templateType === 'vue'
352
- ? !['Container.js', 'vue-hooks.js'].includes(fileName)
353
- : !['react-hooks.js'].includes(fileName));
354
- })];
355
- case 1:
356
- _a.sent();
357
- return [4, (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, "../project-path/.swico-".concat(templateType)), path_1.default.resolve(projectPath, './.swico'), function (fileName) {
358
- return ['react-hooks.js', 'vue-hooks.js', 'loading.js', 'Container.vue'].includes(fileName);
359
- })];
360
- case 2:
361
- _a.sent();
362
- return [4, formatRouterConfig(routerConfig, templateType, env)];
363
- case 3:
364
- _a.sent();
365
- return [4, fs_extra_1.default.readFile(path_1.default.resolve(projectPath, "./.swico".concat(envPath, "index.js")), 'utf8')];
366
- case 4:
367
- replaceIndexText = _a.sent();
368
- hooksFilePath = path_1.default.resolve(projectPath, "./.swico/".concat(templateType, "-hooks.js"));
369
- return [4, fs_extra_1.default.readFile(hooksFilePath, 'utf8')];
370
- case 5:
371
- replaceHooksText = _a.sent();
372
- replaceHooksText = replaceHooksText.replaceAll('$env', ".".concat(env));
373
- return [4, fs_extra_1.default.writeFile(hooksFilePath, replaceHooksText)];
374
- case 6:
375
- _a.sent();
376
- replaceIndexText = (0, exports.handleGlobalStyleFile)(replaceIndexText);
377
- if (!(templateType === 'vue')) return [3, 8];
378
- replaceIndexText = replaceIndexText.replace('"../Container"', '"../Container.vue"');
379
- return [4, (0, exports.updateIndexFileText)(envPath, replaceIndexText)];
380
- case 7:
381
- _a.sent();
382
- return [3, 11];
383
- case 8:
384
- if (!(templateType === 'react')) return [3, 11];
385
- return [4, (0, exports.handleLoadingFile)(replaceIndexText, envPath)];
386
- case 9:
387
- newReplaceIndexText = _a.sent();
388
- return [4, (0, exports.updateIndexFileText)(envPath, newReplaceIndexText)];
389
- case 10:
390
- _a.sent();
391
- _a.label = 11;
392
- case 11:
393
- resolve(null);
394
- return [2];
395
- }
396
- });
397
- }); });
398
- };
399
- var getFormatDefineVars = function (defineVarsConfigData) { return __awaiter(void 0, void 0, void 0, function () {
400
- var formatObj, obj, _i, _a, _b, key, value;
401
- return __generator(this, function (_c) {
402
- switch (_c.label) {
403
- case 0:
404
- formatObj = {};
405
- obj = defineVarsConfigData;
406
- if (!(typeof defineVarsConfigData === 'function')) return [3, 2];
407
- return [4, Promise.resolve(defineVarsConfigData())];
408
- case 1:
409
- obj = _c.sent();
410
- _c.label = 2;
411
- case 2:
412
- for (_i = 0, _a = Object.entries(obj); _i < _a.length; _i++) {
413
- _b = _a[_i], key = _b[0], value = _b[1];
414
- formatObj[key] = JSON.stringify(value);
415
- }
416
- return [2, formatObj];
217
+ const initTemplateConfig = (routerConfig, templateType, env) => {
218
+ const projectPath = process.cwd();
219
+ const envPath = env === 'dev' ? '/.dev/' : '/.prod/';
220
+ return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
221
+ const copyTargetPath = path_1.default.resolve(projectPath, `./.swico${envPath}`);
222
+ yield (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, `../project-path/.swico-${templateType}/$env`), copyTargetPath, (fileName) => !fileName.endsWith('.d.ts') &&
223
+ (templateType === 'vue'
224
+ ? !['Container.js', 'vue-hooks.js'].includes(fileName)
225
+ : !['react-hooks.js'].includes(fileName)));
226
+ yield (0, tools_1.copyDirFiles)(path_1.default.resolve(__dirname, `../project-path/.swico-${templateType}`), path_1.default.resolve(projectPath, './.swico'), (fileName) => ['react-hooks.js', 'vue-hooks.js', 'loading.js', 'Container.vue'].includes(fileName));
227
+ yield formatRouterConfig(routerConfig, templateType, env);
228
+ let replaceIndexText = yield fs_extra_1.default.readFile(path_1.default.resolve(projectPath, `./.swico${envPath}index.js`), 'utf8');
229
+ const hooksFilePath = path_1.default.resolve(projectPath, `./.swico/${templateType}-hooks.js`);
230
+ let replaceHooksText = yield fs_extra_1.default.readFile(hooksFilePath, 'utf8');
231
+ replaceHooksText = replaceHooksText.replaceAll('$env', `.${env}`);
232
+ yield fs_extra_1.default.writeFile(hooksFilePath, replaceHooksText);
233
+ replaceIndexText = (0, exports.handleGlobalStyleFile)(replaceIndexText);
234
+ if (templateType === 'vue') {
235
+ replaceIndexText = replaceIndexText.replace('"../Container"', '"../Container.vue"');
236
+ yield (0, exports.updateIndexFileText)(envPath, replaceIndexText);
417
237
  }
418
- });
419
- }); };
238
+ else if (templateType === 'react') {
239
+ const newReplaceIndexText = yield (0, exports.handleLoadingFile)(replaceIndexText, envPath);
240
+ yield (0, exports.updateIndexFileText)(envPath, newReplaceIndexText);
241
+ }
242
+ resolve(null);
243
+ }));
244
+ };
245
+ const getFormatDefineVars = (defineVarsConfigData) => __awaiter(void 0, void 0, void 0, function* () {
246
+ const formatObj = {};
247
+ let obj = defineVarsConfigData;
248
+ if (typeof defineVarsConfigData === 'function') {
249
+ obj = yield Promise.resolve(defineVarsConfigData());
250
+ }
251
+ for (const [key, value] of Object.entries(obj)) {
252
+ formatObj[key] = JSON.stringify(value);
253
+ }
254
+ return formatObj;
255
+ });
420
256
  exports.getFormatDefineVars = getFormatDefineVars;
421
257
  exports.initConfig = {
422
258
  console: true,