@uni-helper/vite-plugin-uni-pages 0.3.5 → 0.3.7

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/dist/index.cjs CHANGED
@@ -156,32 +156,76 @@ function mergePageMetaDataArray(pageMetaData) {
156
156
  }
157
157
  return result;
158
158
  }
159
- async function execScript(code, filename) {
160
- const jsCode = ts__namespace.transpileModule(code, {
161
- compilerOptions: {
162
- module: ts__namespace.ModuleKind.CommonJS,
163
- target: ts__namespace.ScriptTarget.ESNext
164
- }
165
- }).outputText;
166
- const dir = path__default.dirname(filename);
167
- const vmContext = {
168
- require: node_module.createRequire(dir),
169
- module: {},
170
- exports: {},
171
- __filename: filename,
172
- __dirname: dir
173
- };
174
- const script = new vm__default.Script(jsCode);
159
+ async function execScript(options) {
160
+ const { imports = [], code, filename } = options;
161
+ let jsCode = "";
175
162
  try {
176
- script.runInNewContext(vmContext);
163
+ const tmpCode = `${imports.join("\n")}
164
+ export default ${code}`;
165
+ jsCode = ts__namespace.transpileModule(tmpCode, {
166
+ compilerOptions: {
167
+ module: ts__namespace.ModuleKind.CommonJS,
168
+ // 生成的模块格式为 CommonJS(Node.js 默认格式)
169
+ target: ts__namespace.ScriptTarget.ES2022,
170
+ // 编译后的 JavaScript 目标版本
171
+ noEmit: true,
172
+ // 不生成输出文件
173
+ strict: false,
174
+ // 关闭所有严格类型检查选项
175
+ noImplicitAny: false,
176
+ // 允许表达式和 any 类型
177
+ strictNullChecks: false,
178
+ // 关闭严格的 null 和 undefined 检查
179
+ strictFunctionTypes: false,
180
+ // 关闭函数参数的严格逆变比较
181
+ strictBindCallApply: false,
182
+ // 关闭对 bind、call 和 apply 方法的严格类型检查
183
+ strictPropertyInitialization: false,
184
+ // 关闭类属性初始化的严格检查
185
+ noImplicitThis: false,
186
+ // 允许 this 表达式具有隐式的 any 类型
187
+ alwaysStrict: false,
188
+ // 不以严格模式解析并为每个源文件生成 "use strict" 指令
189
+ allowJs: true,
190
+ // 允许编译 JavaScript 文件
191
+ checkJs: false,
192
+ // 不检查 JavaScript 文件中的类型
193
+ skipLibCheck: true,
194
+ // 跳过对 TypeScript 声明文件 (*.d.ts) 的类型检查
195
+ esModuleInterop: true,
196
+ // 启用 ES 模块互操作性,允许使用 import 导入 CommonJS 模块
197
+ removeComments: true
198
+ // 删除注释
199
+ },
200
+ jsDocParsingMode: ts__namespace.JSDocParsingMode.ParseNone
201
+ // 不解析 JSDoc
202
+ }).outputText;
203
+ const dir = path__default.dirname(filename);
204
+ const vmContext = {
205
+ module: {},
206
+ exports: {},
207
+ __filename: filename,
208
+ __dirname: dir,
209
+ require: node_module.createRequire(dir),
210
+ import: (id) => import(id)
211
+ };
212
+ const script = new vm__default.Script(jsCode, { filename });
213
+ await script.runInNewContext(vmContext, {
214
+ timeout: 1e3
215
+ // 设置超时避免长时间运行
216
+ });
217
+ const result = vmContext.exports.default || vmContext.exports;
218
+ if (typeof result === "function") {
219
+ return Promise.resolve(result());
220
+ }
221
+ return Promise.resolve(result);
177
222
  } catch (error) {
178
- throw new Error(`${filename}: ${error.message}`);
179
- }
180
- const result = vmContext.exports.default || vmContext.exports;
181
- if (typeof result === "function") {
182
- return Promise.resolve(result());
223
+ throw new Error(`EXEC SCRIPT FAIL IN ${filename}: ${error.message}
224
+
225
+ ${jsCode}
226
+
227
+ `);
183
228
  }
184
- return Promise.resolve(result);
185
229
  }
186
230
  function getDefaultExport(expr) {
187
231
  return expr.default === void 0 ? expr : expr.default;
@@ -429,11 +473,11 @@ async function tryPageMetaFromMacro(sfc) {
429
473
  const imports = findImports(ast.body).map((imp) => babelGenerate(imp).code);
430
474
  const [macroOption] = macro.arguments;
431
475
  const code = babelGenerate(macroOption).code;
432
- let script = "";
433
- const importScript = imports.join("");
434
- script += importScript;
435
- script += `export default ${code}`;
436
- const result = await execScript(script, sfc.filename);
476
+ const result = await execScript({
477
+ imports,
478
+ code,
479
+ filename: sfc.filename
480
+ });
437
481
  return result;
438
482
  }
439
483
  return void 0;
package/dist/index.d.cts CHANGED
@@ -3012,11 +3012,19 @@ declare function isTargetFile(path: string): boolean;
3012
3012
  */
3013
3013
  declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
3014
3014
  /**
3015
- * 执行 TypeScript 代码字符串并返回其返回值
3016
- * @param code - TypeScript 代码字符串
3017
- * @returns 返回值
3015
+ * 执行一段 TypeScript / JavaScript 脚本代码,并返回执行结果
3016
+ *
3017
+ * @param options - 执行脚本所需的配置项
3018
+ * @param options.imports - 需要引入的模块导入语句列表
3019
+ * @param options.code - 实际要执行的 TypeScript 代码内容
3020
+ * @param options.filename - 脚本文件名,用于错误定位和上下文环境
3021
+ * @returns 返回脚本执行后的结果,若导出的是函数则执行后返回其返回值
3018
3022
  */
3019
- declare function execScript(code: string, filename: string): Promise<any>;
3023
+ declare function execScript(options: {
3024
+ imports: string[];
3025
+ code: string;
3026
+ filename: string;
3027
+ }): Promise<any>;
3020
3028
  declare const babelGenerate: typeof babelGenerator;
3021
3029
 
3022
3030
  interface CustomBlock {
package/dist/index.d.mts CHANGED
@@ -3012,11 +3012,19 @@ declare function isTargetFile(path: string): boolean;
3012
3012
  */
3013
3013
  declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
3014
3014
  /**
3015
- * 执行 TypeScript 代码字符串并返回其返回值
3016
- * @param code - TypeScript 代码字符串
3017
- * @returns 返回值
3015
+ * 执行一段 TypeScript / JavaScript 脚本代码,并返回执行结果
3016
+ *
3017
+ * @param options - 执行脚本所需的配置项
3018
+ * @param options.imports - 需要引入的模块导入语句列表
3019
+ * @param options.code - 实际要执行的 TypeScript 代码内容
3020
+ * @param options.filename - 脚本文件名,用于错误定位和上下文环境
3021
+ * @returns 返回脚本执行后的结果,若导出的是函数则执行后返回其返回值
3018
3022
  */
3019
- declare function execScript(code: string, filename: string): Promise<any>;
3023
+ declare function execScript(options: {
3024
+ imports: string[];
3025
+ code: string;
3026
+ filename: string;
3027
+ }): Promise<any>;
3020
3028
  declare const babelGenerate: typeof babelGenerator;
3021
3029
 
3022
3030
  interface CustomBlock {
package/dist/index.d.ts CHANGED
@@ -3012,11 +3012,19 @@ declare function isTargetFile(path: string): boolean;
3012
3012
  */
3013
3013
  declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
3014
3014
  /**
3015
- * 执行 TypeScript 代码字符串并返回其返回值
3016
- * @param code - TypeScript 代码字符串
3017
- * @returns 返回值
3015
+ * 执行一段 TypeScript / JavaScript 脚本代码,并返回执行结果
3016
+ *
3017
+ * @param options - 执行脚本所需的配置项
3018
+ * @param options.imports - 需要引入的模块导入语句列表
3019
+ * @param options.code - 实际要执行的 TypeScript 代码内容
3020
+ * @param options.filename - 脚本文件名,用于错误定位和上下文环境
3021
+ * @returns 返回脚本执行后的结果,若导出的是函数则执行后返回其返回值
3018
3022
  */
3019
- declare function execScript(code: string, filename: string): Promise<any>;
3023
+ declare function execScript(options: {
3024
+ imports: string[];
3025
+ code: string;
3026
+ filename: string;
3027
+ }): Promise<any>;
3020
3028
  declare const babelGenerate: typeof babelGenerator;
3021
3029
 
3022
3030
  interface CustomBlock {
package/dist/index.mjs CHANGED
@@ -122,32 +122,76 @@ function mergePageMetaDataArray(pageMetaData) {
122
122
  }
123
123
  return result;
124
124
  }
125
- async function execScript(code, filename) {
126
- const jsCode = ts.transpileModule(code, {
127
- compilerOptions: {
128
- module: ts.ModuleKind.CommonJS,
129
- target: ts.ScriptTarget.ESNext
130
- }
131
- }).outputText;
132
- const dir = path.dirname(filename);
133
- const vmContext = {
134
- require: createRequire(dir),
135
- module: {},
136
- exports: {},
137
- __filename: filename,
138
- __dirname: dir
139
- };
140
- const script = new vm.Script(jsCode);
125
+ async function execScript(options) {
126
+ const { imports = [], code, filename } = options;
127
+ let jsCode = "";
141
128
  try {
142
- script.runInNewContext(vmContext);
129
+ const tmpCode = `${imports.join("\n")}
130
+ export default ${code}`;
131
+ jsCode = ts.transpileModule(tmpCode, {
132
+ compilerOptions: {
133
+ module: ts.ModuleKind.CommonJS,
134
+ // 生成的模块格式为 CommonJS(Node.js 默认格式)
135
+ target: ts.ScriptTarget.ES2022,
136
+ // 编译后的 JavaScript 目标版本
137
+ noEmit: true,
138
+ // 不生成输出文件
139
+ strict: false,
140
+ // 关闭所有严格类型检查选项
141
+ noImplicitAny: false,
142
+ // 允许表达式和 any 类型
143
+ strictNullChecks: false,
144
+ // 关闭严格的 null 和 undefined 检查
145
+ strictFunctionTypes: false,
146
+ // 关闭函数参数的严格逆变比较
147
+ strictBindCallApply: false,
148
+ // 关闭对 bind、call 和 apply 方法的严格类型检查
149
+ strictPropertyInitialization: false,
150
+ // 关闭类属性初始化的严格检查
151
+ noImplicitThis: false,
152
+ // 允许 this 表达式具有隐式的 any 类型
153
+ alwaysStrict: false,
154
+ // 不以严格模式解析并为每个源文件生成 "use strict" 指令
155
+ allowJs: true,
156
+ // 允许编译 JavaScript 文件
157
+ checkJs: false,
158
+ // 不检查 JavaScript 文件中的类型
159
+ skipLibCheck: true,
160
+ // 跳过对 TypeScript 声明文件 (*.d.ts) 的类型检查
161
+ esModuleInterop: true,
162
+ // 启用 ES 模块互操作性,允许使用 import 导入 CommonJS 模块
163
+ removeComments: true
164
+ // 删除注释
165
+ },
166
+ jsDocParsingMode: ts.JSDocParsingMode.ParseNone
167
+ // 不解析 JSDoc
168
+ }).outputText;
169
+ const dir = path.dirname(filename);
170
+ const vmContext = {
171
+ module: {},
172
+ exports: {},
173
+ __filename: filename,
174
+ __dirname: dir,
175
+ require: createRequire(dir),
176
+ import: (id) => import(id)
177
+ };
178
+ const script = new vm.Script(jsCode, { filename });
179
+ await script.runInNewContext(vmContext, {
180
+ timeout: 1e3
181
+ // 设置超时避免长时间运行
182
+ });
183
+ const result = vmContext.exports.default || vmContext.exports;
184
+ if (typeof result === "function") {
185
+ return Promise.resolve(result());
186
+ }
187
+ return Promise.resolve(result);
143
188
  } catch (error) {
144
- throw new Error(`${filename}: ${error.message}`);
145
- }
146
- const result = vmContext.exports.default || vmContext.exports;
147
- if (typeof result === "function") {
148
- return Promise.resolve(result());
189
+ throw new Error(`EXEC SCRIPT FAIL IN ${filename}: ${error.message}
190
+
191
+ ${jsCode}
192
+
193
+ `);
149
194
  }
150
- return Promise.resolve(result);
151
195
  }
152
196
  function getDefaultExport(expr) {
153
197
  return expr.default === void 0 ? expr : expr.default;
@@ -395,11 +439,11 @@ async function tryPageMetaFromMacro(sfc) {
395
439
  const imports = findImports(ast.body).map((imp) => babelGenerate(imp).code);
396
440
  const [macroOption] = macro.arguments;
397
441
  const code = babelGenerate(macroOption).code;
398
- let script = "";
399
- const importScript = imports.join("");
400
- script += importScript;
401
- script += `export default ${code}`;
402
- const result = await execScript(script, sfc.filename);
442
+ const result = await execScript({
443
+ imports,
444
+ code,
445
+ filename: sfc.filename
446
+ });
403
447
  return result;
404
448
  }
405
449
  return void 0;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@uni-helper/vite-plugin-uni-pages",
3
3
  "type": "module",
4
- "version": "0.3.5",
4
+ "version": "0.3.7",
5
5
  "description": "File system based routing for uni-app applications using Vite",
6
6
  "author": "KeJun",
7
7
  "maintainers": [
@@ -82,6 +82,6 @@
82
82
  "scripts": {
83
83
  "build": "unbuild",
84
84
  "stub": "unbuild --stub",
85
- "debug": "cross-env NODE_ENV=development unbuild"
85
+ "debug": "cross-env NODE_ENV=development unbuild --sourcemap"
86
86
  }
87
87
  }