mp-weixin-back 0.0.14 → 0.0.15

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
@@ -4,11 +4,12 @@ const path = require('path');
4
4
  const fs = require('fs');
5
5
  const JSON5 = require('json5');
6
6
  const kolorist = require('kolorist');
7
- const compilerSfc = require('@vue/compiler-sfc');
7
+ const module$1 = require('module');
8
8
  const generate = require('@babel/generator');
9
9
  const MagicString = require('magic-string');
10
10
  const astKit = require('ast-kit');
11
11
 
12
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
13
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
14
 
14
15
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
@@ -407,9 +408,35 @@ const vueWalker = {
407
408
  optionsWalk
408
409
  };
409
410
 
411
+ let compilerPromise = null;
412
+ async function resolveCompiler(root) {
413
+ if (compilerPromise) {
414
+ return compilerPromise;
415
+ }
416
+ compilerPromise = (async () => {
417
+ try {
418
+ const _require = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
419
+ const compilerPath = _require.resolve("@vue/compiler-sfc", { paths: [root] });
420
+ return _require(compilerPath);
421
+ } catch (firstError) {
422
+ try {
423
+ return await import('@vue/compiler-sfc');
424
+ } catch (secondError) {
425
+ throw new Error(
426
+ `\u65E0\u6CD5\u89E3\u6790 @vue/compiler-sfc\u3002
427
+ \u6B64\u63D2\u4EF6\u9700\u8981\u9879\u76EE\u4E2D\u5B89\u88C5 @vue/compiler-sfc\u3002
428
+ \u8BF7\u624B\u52A8\u5B89\u88C5\uFF1Apnpm add -D @vue/compiler-sfc
429
+ `
430
+ );
431
+ }
432
+ }
433
+ })();
434
+ return compilerPromise;
435
+ }
410
436
  async function transformVueFile(code, id) {
411
437
  try {
412
- const sfc = compilerSfc.parse(code).descriptor;
438
+ const sfcCompiler = await resolveCompiler(this.config.root);
439
+ const sfc = sfcCompiler.parse(code).descriptor;
413
440
  const { template, script, scriptSetup } = sfc;
414
441
  if (!template?.content) {
415
442
  return code;
@@ -421,7 +448,7 @@ async function transformVueFile(code, id) {
421
448
  return vueWalker[walker](this, code, sfc, id);
422
449
  } catch (error) {
423
450
  this.log.error("\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E");
424
- this.log.debugLog(String(error));
451
+ this.log.error(String(error));
425
452
  return code;
426
453
  }
427
454
  }
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import path from 'path';
2
2
  import fs from 'fs';
3
3
  import JSON5 from 'json5';
4
4
  import { white, red, green } from 'kolorist';
5
- import { parse } from '@vue/compiler-sfc';
5
+ import { createRequire } from 'module';
6
6
  import generate from '@babel/generator';
7
7
  import MagicString from 'magic-string';
8
8
  import { babelParse, walkAST } from 'ast-kit';
@@ -397,9 +397,35 @@ const vueWalker = {
397
397
  optionsWalk
398
398
  };
399
399
 
400
+ let compilerPromise = null;
401
+ async function resolveCompiler(root) {
402
+ if (compilerPromise) {
403
+ return compilerPromise;
404
+ }
405
+ compilerPromise = (async () => {
406
+ try {
407
+ const _require = createRequire(import.meta.url);
408
+ const compilerPath = _require.resolve("@vue/compiler-sfc", { paths: [root] });
409
+ return _require(compilerPath);
410
+ } catch (firstError) {
411
+ try {
412
+ return await import('@vue/compiler-sfc');
413
+ } catch (secondError) {
414
+ throw new Error(
415
+ `\u65E0\u6CD5\u89E3\u6790 @vue/compiler-sfc\u3002
416
+ \u6B64\u63D2\u4EF6\u9700\u8981\u9879\u76EE\u4E2D\u5B89\u88C5 @vue/compiler-sfc\u3002
417
+ \u8BF7\u624B\u52A8\u5B89\u88C5\uFF1Apnpm add -D @vue/compiler-sfc
418
+ `
419
+ );
420
+ }
421
+ }
422
+ })();
423
+ return compilerPromise;
424
+ }
400
425
  async function transformVueFile(code, id) {
401
426
  try {
402
- const sfc = parse(code).descriptor;
427
+ const sfcCompiler = await resolveCompiler(this.config.root);
428
+ const sfc = sfcCompiler.parse(code).descriptor;
403
429
  const { template, script, scriptSetup } = sfc;
404
430
  if (!template?.content) {
405
431
  return code;
@@ -411,7 +437,7 @@ async function transformVueFile(code, id) {
411
437
  return vueWalker[walker](this, code, sfc, id);
412
438
  } catch (error) {
413
439
  this.log.error("\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E");
414
- this.log.debugLog(String(error));
440
+ this.log.error(String(error));
415
441
  return code;
416
442
  }
417
443
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mp-weixin-back",
3
3
  "type": "module",
4
- "version": "0.0.14",
4
+ "version": "0.0.15",
5
5
  "description": "监听微信小程序的手势返回和页面默认导航栏的返回",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.mjs",
@@ -47,8 +47,8 @@
47
47
  "magic-string": "^0.30.13"
48
48
  },
49
49
  "peerDependencies": {
50
- "@vue/compiler-sfc": "^2.7.0 || ^3.0.0",
51
- "vite": "^2.0.0 || ^3.0.0-0 || ^4.0.0 || ^5.0.0"
50
+ "@vue/compiler-sfc": "^3.2.0",
51
+ "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
52
52
  },
53
53
  "lint-staged": {
54
54
  "*": "prettier --write"
package/utils/index.ts CHANGED
@@ -1,10 +1,43 @@
1
- import { parse } from '@vue/compiler-sfc'
1
+ import { createRequire } from 'module'
2
2
  import { pageContext } from '../src/context'
3
3
  import { vueWalker } from './walker'
4
4
 
5
+ let compilerPromise: Promise<typeof import('@vue/compiler-sfc')> | null = null
6
+
7
+ async function resolveCompiler(root: string): Promise<typeof import('@vue/compiler-sfc')> {
8
+ // 避免重复解析(防止并发调用时的竞态条件)
9
+ if (compilerPromise) {
10
+ return compilerPromise
11
+ }
12
+
13
+ compilerPromise = (async () => {
14
+ // 尝试加载用户项目中的 @vue/compiler-sfc
15
+ try {
16
+ const _require = createRequire(import.meta.url)
17
+ // 尝试从用户根目录解析
18
+ const compilerPath = _require.resolve('@vue/compiler-sfc', { paths: [root] })
19
+ return _require(compilerPath) as typeof import('@vue/compiler-sfc')
20
+ } catch (firstError) {
21
+ try {
22
+ // 降级尝试直接 import
23
+ return await import('@vue/compiler-sfc')
24
+ } catch (secondError) {
25
+ throw new Error(
26
+ `无法解析 @vue/compiler-sfc。\n` +
27
+ `此插件需要项目中安装 @vue/compiler-sfc。\n` +
28
+ `请手动安装:pnpm add -D @vue/compiler-sfc\n`
29
+ )
30
+ }
31
+ }
32
+ })()
33
+
34
+ return compilerPromise
35
+ }
36
+
5
37
  export async function transformVueFile(this: pageContext, code: string, id: string) {
6
38
  try {
7
- const sfc = parse(code).descriptor
39
+ const sfcCompiler = await resolveCompiler(this.config.root)
40
+ const sfc = sfcCompiler.parse(code).descriptor
8
41
  const { template, script, scriptSetup } = sfc
9
42
  if (!template?.content) {
10
43
  return code
@@ -19,7 +52,7 @@ export async function transformVueFile(this: pageContext, code: string, id: stri
19
52
  return vueWalker[walker](this, code, sfc, id)
20
53
  } catch (error) {
21
54
  this.log.error('解析vue文件失败,请检查文件是否正确')
22
- this.log.debugLog(String(error))
55
+ this.log.error(String(error))
23
56
  return code
24
57
  }
25
58
  }