glyphix 1.0.32 → 1.0.34
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/bin/glyphix.js +203 -113
- package/package.json +1 -1
package/bin/glyphix.js
CHANGED
|
@@ -132,129 +132,219 @@ function buildGlyphix(args) {
|
|
|
132
132
|
var import_esbuild = __toESM(require("esbuild"));
|
|
133
133
|
var import_node_path3 = require("path");
|
|
134
134
|
var import_node_fs = require("fs");
|
|
135
|
-
var import_fs_extra2 = require("fs-extra");
|
|
136
|
-
var import_eslint = require("eslint");
|
|
137
|
-
var import_glob = require("glob");
|
|
138
|
-
var import_chalk2 = __toESM(require("chalk"));
|
|
139
135
|
var import_dotenv = __toESM(require("dotenv"));
|
|
140
136
|
var import_lodash = require("lodash");
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
137
|
+
var import_node_child_process = require("child_process");
|
|
138
|
+
var GxGenerate = class {
|
|
139
|
+
constructor() {
|
|
140
|
+
// 执行 gx 命令的目录路径
|
|
141
|
+
this.workspaceRoot = process.cwd();
|
|
142
|
+
// 正在构建的应用的 src 目录路径
|
|
143
|
+
this.sourceRoot = (0, import_node_path3.resolve)(
|
|
144
|
+
this.workspaceRoot,
|
|
145
|
+
process.env["VITE_SOURCE_DIR"]
|
|
146
|
+
);
|
|
147
|
+
// 构建的 build 目录路径
|
|
148
|
+
this.buildRoot = (0, import_node_path3.resolve)(
|
|
149
|
+
this.workspaceRoot,
|
|
150
|
+
process.env["VITE_BUILD_DIR"]
|
|
151
|
+
);
|
|
152
|
+
// gx 工具传递过来的入口文件,包括 app.{t,j}s 和各个页面的 js 文件路径
|
|
153
|
+
this.entries = [];
|
|
154
|
+
const content = JSON.parse(
|
|
155
|
+
(0, import_node_fs.readFileSync)((0, import_node_path3.resolve)(this.buildRoot, "entry.vite.json")).toString()
|
|
156
|
+
);
|
|
157
|
+
this.entries = content.entry.map(
|
|
158
|
+
(item) => (0, import_node_path3.resolve)(this.buildRoot, item)
|
|
159
|
+
);
|
|
154
160
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
161
|
+
loadEnv() {
|
|
162
|
+
const dotenvTags = [
|
|
163
|
+
// 本地环境
|
|
164
|
+
"development",
|
|
165
|
+
// 测试环境
|
|
166
|
+
// 比如:单元测试
|
|
167
|
+
"test",
|
|
168
|
+
// 部署环境
|
|
169
|
+
// 比如:日常、预发、线上
|
|
170
|
+
"production"
|
|
171
|
+
];
|
|
172
|
+
if (!dotenvTags.includes(process.env.GLYPHIX_ENV ?? "")) {
|
|
173
|
+
process.env.GLYPHIX_ENV = dotenvTags[0];
|
|
174
|
+
}
|
|
175
|
+
const dotenvPath = (0, import_node_path3.resolve)(this.sourceRoot, "..", ".env");
|
|
176
|
+
const dotenvFiles = [
|
|
177
|
+
dotenvPath,
|
|
178
|
+
`${dotenvPath}.local`,
|
|
179
|
+
`${dotenvPath}.${process.env.GLYPHIX_ENV}`,
|
|
180
|
+
`${dotenvPath}.${process.env.GLYPHIX_ENV}.local`
|
|
181
|
+
].filter((item) => {
|
|
182
|
+
return (0, import_node_fs.existsSync)(item);
|
|
183
|
+
});
|
|
184
|
+
dotenvFiles.reverse().forEach((dotenvFile) => {
|
|
185
|
+
import_dotenv.default.config({ path: (0, import_node_path3.resolve)(this.sourceRoot, "..", dotenvFile) });
|
|
186
|
+
});
|
|
187
|
+
let envDefine = {};
|
|
188
|
+
Array.from(Object.keys(process.env)).forEach((key) => {
|
|
189
|
+
if (key.startsWith("GLYPHIX"))
|
|
190
|
+
envDefine[`process.env.${key}`] = JSON.stringify(process.env[key]);
|
|
191
|
+
});
|
|
192
|
+
return envDefine;
|
|
193
|
+
}
|
|
194
|
+
generateEsbuildConfig() {
|
|
195
|
+
const resolveExtensions = [".ts", ".js"];
|
|
196
|
+
const that = this;
|
|
197
|
+
const internalConfig = {
|
|
198
|
+
entryPoints: this.entries,
|
|
199
|
+
// 入口文件
|
|
200
|
+
bundle: true,
|
|
201
|
+
// 启用打包
|
|
202
|
+
outdir: (0, import_node_path3.resolve)(this.buildRoot, ".vite-dist"),
|
|
203
|
+
// 输出目录
|
|
204
|
+
splitting: true,
|
|
205
|
+
// 启用代码分割
|
|
206
|
+
format: "esm",
|
|
207
|
+
// 输出为 ESM 格式
|
|
208
|
+
resolveExtensions,
|
|
209
|
+
external: ["@system*"],
|
|
210
|
+
// 将 lodash 和 axios 作为外部依赖
|
|
211
|
+
platform: "node",
|
|
212
|
+
absWorkingDir: (0, import_node_path3.resolve)(this.sourceRoot, ".."),
|
|
213
|
+
target: "node14",
|
|
214
|
+
// 目标平台设置为最新 ECMAScript
|
|
215
|
+
define: {
|
|
216
|
+
"process.env.GLYPHIX_APP_VERSION": `"${this.getGitVersion()}"`,
|
|
217
|
+
...this.loadEnv()
|
|
218
|
+
},
|
|
219
|
+
nodePaths: [(0, import_node_path3.resolve)(this.sourceRoot, "..", "node_modules")],
|
|
220
|
+
sourcemap: true,
|
|
221
|
+
minify: process.env.GLYPHIX_ENV === "development" ? false : true,
|
|
222
|
+
plugins: [
|
|
223
|
+
{
|
|
224
|
+
name: "glyphix-resolve-path",
|
|
225
|
+
setup(build) {
|
|
226
|
+
build.onResolve({ filter: /^\// }, (args) => {
|
|
227
|
+
if ((0, import_node_fs.existsSync)(args.path)) {
|
|
219
228
|
return {
|
|
220
|
-
path:
|
|
229
|
+
path: args.path
|
|
221
230
|
};
|
|
222
231
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
232
|
+
for (let i = 0; i < resolveExtensions.length; i++) {
|
|
233
|
+
const fullPath = (0, import_node_path3.join)(
|
|
234
|
+
that.buildRoot,
|
|
235
|
+
`${args.path}${resolveExtensions[i]}`
|
|
236
|
+
);
|
|
237
|
+
if ((0, import_node_fs.existsSync)(fullPath)) {
|
|
238
|
+
return {
|
|
239
|
+
path: fullPath
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const fullIndexPath = (0, import_node_path3.join)(
|
|
243
|
+
that.buildRoot,
|
|
244
|
+
`${args.path}/index${resolveExtensions[i]}`
|
|
245
|
+
);
|
|
246
|
+
if ((0, import_node_fs.existsSync)(fullIndexPath)) {
|
|
247
|
+
return {
|
|
248
|
+
path: fullIndexPath
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
path: args.path
|
|
254
|
+
};
|
|
255
|
+
});
|
|
256
|
+
}
|
|
228
257
|
}
|
|
258
|
+
]
|
|
259
|
+
};
|
|
260
|
+
const customConfigPath = (0, import_node_path3.resolve)(this.buildRoot, "glyphix.config.js");
|
|
261
|
+
if (!(0, import_node_fs.existsSync)(customConfigPath)) return internalConfig;
|
|
262
|
+
try {
|
|
263
|
+
const useConfig = require(customConfigPath);
|
|
264
|
+
return (0, import_lodash.merge)(internalConfig, useConfig);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.error(error);
|
|
267
|
+
}
|
|
268
|
+
return internalConfig;
|
|
269
|
+
}
|
|
270
|
+
judgeIsWorkspace(parentPath, childPath) {
|
|
271
|
+
const resolvedParent = (0, import_node_path3.resolve)(parentPath);
|
|
272
|
+
const resolvedChild = (0, import_node_path3.resolve)(childPath);
|
|
273
|
+
const normalizedParent = resolvedParent + import_node_path3.sep;
|
|
274
|
+
const normalizedChild = resolvedChild + import_node_path3.sep;
|
|
275
|
+
return !normalizedChild.startsWith(normalizedParent);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* 安装依赖
|
|
279
|
+
* 1. 判断是否是 workspace, 非 workspace 不用安装
|
|
280
|
+
* 2. workspace 项目,需要将 package.json 拷贝到构建目录,在构建目录安装依赖
|
|
281
|
+
* @returns
|
|
282
|
+
*/
|
|
283
|
+
installDependencies() {
|
|
284
|
+
return new Promise((resolve3, reject) => {
|
|
285
|
+
console.log(
|
|
286
|
+
"is workspace project",
|
|
287
|
+
this.judgeIsWorkspace(
|
|
288
|
+
(0, import_node_path3.resolve)(this.sourceRoot, ".."),
|
|
289
|
+
this.buildRoot
|
|
290
|
+
)
|
|
291
|
+
);
|
|
292
|
+
if (!this.judgeIsWorkspace(
|
|
293
|
+
(0, import_node_path3.resolve)(this.sourceRoot, ".."),
|
|
294
|
+
this.buildRoot
|
|
295
|
+
)) {
|
|
296
|
+
resolve3();
|
|
297
|
+
return;
|
|
229
298
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
299
|
+
const curDir = process.cwd();
|
|
300
|
+
const userConfig = (0, import_node_path3.resolve)(
|
|
301
|
+
this.sourceRoot,
|
|
302
|
+
"..",
|
|
303
|
+
"glyphix.config.js"
|
|
304
|
+
);
|
|
305
|
+
process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
|
|
306
|
+
const installDepProcess = (0, import_node_child_process.spawn)("npm", ["install"]);
|
|
307
|
+
installDepProcess.on("message", (msg) => {
|
|
308
|
+
console.debug(msg);
|
|
309
|
+
});
|
|
310
|
+
installDepProcess.on("error", (err) => {
|
|
311
|
+
console.error(err);
|
|
312
|
+
});
|
|
313
|
+
installDepProcess.on("close", (code) => {
|
|
314
|
+
if (code === 0) {
|
|
315
|
+
console.info("dependencies install complete");
|
|
316
|
+
process.chdir(curDir);
|
|
317
|
+
resolve3();
|
|
318
|
+
} else {
|
|
319
|
+
process.chdir(curDir);
|
|
320
|
+
process.exit(1);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
});
|
|
239
324
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
325
|
+
getGitVersion() {
|
|
326
|
+
return (0, import_node_child_process.execSync)("git rev-parse HEAD").toString().trim();
|
|
327
|
+
}
|
|
328
|
+
build() {
|
|
329
|
+
const curDir = process.cwd();
|
|
330
|
+
this.installDependencies().then(() => {
|
|
331
|
+
const config = this.generateEsbuildConfig();
|
|
332
|
+
process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
|
|
333
|
+
return import_esbuild.default.build(config);
|
|
334
|
+
}).then(() => {
|
|
335
|
+
process.chdir(curDir);
|
|
336
|
+
console.log("Build completed!");
|
|
337
|
+
}).catch((error) => {
|
|
338
|
+
console.error("Build failed:", error);
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
};
|
|
254
342
|
function generateGlyphix() {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
343
|
+
console.info("start gen", process.env["VITE_SOURCE_DIR"]);
|
|
344
|
+
if (!process.env["VITE_BUILD_DIR"] || !process.env["VITE_SOURCE_DIR"])
|
|
345
|
+
throw Error("not set glyphix project path");
|
|
346
|
+
const generate = new GxGenerate();
|
|
347
|
+
generate.build();
|
|
258
348
|
}
|
|
259
349
|
|
|
260
350
|
// src/cli/index.ts
|