weapp-vite 1.7.0 → 1.7.1

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.
@@ -3,7 +3,7 @@ import logger from "@weapp-core/logger";
3
3
  var logger_default = logger;
4
4
 
5
5
  // package.json
6
- var version = "1.7.0";
6
+ var version = "1.7.1";
7
7
 
8
8
  // src/constants.ts
9
9
  var VERSION = version;
@@ -14,7 +14,7 @@ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
14
14
  // src/context.ts
15
15
  import { createRequire } from "node:module";
16
16
  import process from "node:process";
17
- import { addExtension, defu, get as get2, isObject as isObject3, objectHash, removeExtension } from "@weapp-core/shared";
17
+ import { addExtension as addExtension2, defu as defu2, get as get2, isObject as isObject3, objectHash, removeExtension as removeExtension2 } from "@weapp-core/shared";
18
18
  import fs5 from "fs-extra";
19
19
  import path5 from "pathe";
20
20
  import { build, loadConfigFromFile } from "vite";
@@ -43,13 +43,14 @@ function getOutputExtensions(_platform) {
43
43
  function getWeappViteConfig() {
44
44
  return {
45
45
  enhance: {
46
- wxml: true
46
+ wxml: true,
47
+ wxs: true
47
48
  }
48
49
  };
49
50
  }
50
51
 
51
52
  // src/plugins/index.ts
52
- import { isObject as isObject2 } from "@weapp-core/shared";
53
+ import { isObject as isObject2, removeExtension } from "@weapp-core/shared";
53
54
  import { fdir as Fdir } from "fdir";
54
55
  import fs4 from "fs-extra";
55
56
  import MagicString2 from "magic-string";
@@ -267,13 +268,135 @@ function regExpTest(arr = [], str) {
267
268
  throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
268
269
  }
269
270
 
270
- // src/utils/wxml.ts
271
+ // src/wxml/index.ts
272
+ import { addExtension, removeExtensionDeep } from "@weapp-core/shared";
271
273
  import { Parser } from "htmlparser2";
272
274
  import MagicString from "magic-string";
275
+
276
+ // src/wxs/index.ts
277
+ import babel from "@babel/core";
278
+ import t from "@babel/types";
279
+ import { defu } from "@weapp-core/shared";
280
+ function transformWxsCode(code, options) {
281
+ const { filename } = defu(options, {
282
+ filename: "wxs.ts"
283
+ });
284
+ return babel.transformSync(code, {
285
+ presets: [
286
+ ["@babel/preset-env", {
287
+ targets: {
288
+ node: 0
289
+ }
290
+ }],
291
+ ["@babel/preset-typescript", {}]
292
+ ],
293
+ filename,
294
+ plugins: [
295
+ {
296
+ visitor: {
297
+ Directive: {
298
+ enter(p) {
299
+ p.remove();
300
+ }
301
+ },
302
+ ExpressionStatement(p) {
303
+ const expression = p.node.expression;
304
+ if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && t.isIdentifier(expression.callee.object) && expression.callee.object.name === "Object" && t.isIdentifier(expression.callee.property) && expression.callee.property.name === "defineProperty" && expression.arguments.length >= 2 && t.isIdentifier(expression.arguments[0]) && expression.arguments[0].name === "exports" && t.isStringLiteral(expression.arguments[1]) && expression.arguments[1].value === "__esModule") {
305
+ p.remove();
306
+ }
307
+ },
308
+ NewExpression: {
309
+ enter(p) {
310
+ if (p.get("callee").isIdentifier({
311
+ name: "RegExp"
312
+ })) {
313
+ p.replaceWith(
314
+ t.callExpression(t.identifier("getRegExp"), p.get("arguments").map((x) => x.node))
315
+ );
316
+ } else if (p.get("callee").isIdentifier({
317
+ name: "Date"
318
+ })) {
319
+ p.replaceWith(
320
+ t.callExpression(t.identifier("getDate"), p.get("arguments").map((x) => x.node))
321
+ );
322
+ }
323
+ }
324
+ },
325
+ RegExpLiteral: {
326
+ enter(p) {
327
+ const args = [t.stringLiteral(p.node.pattern)];
328
+ if (p.node.flags) {
329
+ args.push(t.stringLiteral(p.node.flags));
330
+ }
331
+ p.replaceWith(
332
+ t.callExpression(t.identifier("getRegExp"), args)
333
+ );
334
+ }
335
+ },
336
+ MemberExpression: {
337
+ enter(p) {
338
+ if (p.get("object").isIdentifier({
339
+ name: "exports"
340
+ })) {
341
+ p.replaceWith(
342
+ t.memberExpression(
343
+ t.memberExpression(t.identifier("module"), t.identifier("exports")),
344
+ p.get("property").node
345
+ )
346
+ );
347
+ }
348
+ }
349
+ }
350
+ }
351
+ }
352
+ ]
353
+ });
354
+ }
355
+
356
+ // src/wxml/index.ts
357
+ var srcImportTagsMap = {
358
+ // audio: ['src', 'poster'],
359
+ // video: ['src', 'poster'],
360
+ // image: ['src'],
361
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
362
+ wxs: ["src"]
363
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
364
+ // import: ['src'],
365
+ // include: ['src'],
366
+ };
273
367
  function processWxml(wxml) {
274
368
  const ms = new MagicString(wxml.toString());
369
+ const deps = [];
370
+ let currentTagName = "";
371
+ let importAttrs;
372
+ let attrs = {};
275
373
  const parser = new Parser({
276
- onattribute(name, _value, _quote) {
374
+ onopentagname(name) {
375
+ currentTagName = name;
376
+ importAttrs = srcImportTagsMap[currentTagName];
377
+ },
378
+ onattribute(name, value, quote) {
379
+ attrs[name] = value;
380
+ if (importAttrs) {
381
+ for (const attrName of importAttrs) {
382
+ if (attrName === name) {
383
+ deps.push({
384
+ name,
385
+ value,
386
+ quote,
387
+ tagName: currentTagName,
388
+ start: parser.startIndex,
389
+ end: parser.endIndex,
390
+ attrs
391
+ });
392
+ if (currentTagName === "wxs" && name === "src") {
393
+ if (/\.wxs.[jt]s$/.test(value)) {
394
+ ms.update(parser.startIndex + 5, parser.endIndex - 1, addExtension(removeExtensionDeep(value), ".wxs"));
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
277
400
  if (name.startsWith("@")) {
278
401
  const start = parser.startIndex;
279
402
  const end = parser.startIndex + name.length;
@@ -301,6 +424,20 @@ function processWxml(wxml) {
301
424
  ms.update(start, end, rep);
302
425
  }
303
426
  }
427
+ },
428
+ onclosetag() {
429
+ currentTagName = "";
430
+ attrs = {};
431
+ importAttrs = void 0;
432
+ },
433
+ ontext(data) {
434
+ if (currentTagName === "wxs" && jsExtensions.includes(attrs.lang)) {
435
+ const res = transformWxsCode(data);
436
+ if (res && res.code) {
437
+ ms.update(parser.startIndex, parser.endIndex, `
438
+ ${res.code}`);
439
+ }
440
+ }
304
441
  }
305
442
  });
306
443
  parser.write(
@@ -308,6 +445,7 @@ function processWxml(wxml) {
308
445
  );
309
446
  parser.end();
310
447
  return {
448
+ deps,
311
449
  code: ms.toString()
312
450
  };
313
451
  }
@@ -426,7 +564,32 @@ function vitePluginWeapp(ctx, subPackageMeta) {
426
564
  const source = isMedia ? await fs4.readFile(filepath) : await fs4.readFile(filepath, "utf8");
427
565
  const fileName = ctx.relativeSrcRoot(file);
428
566
  if (isHtml || isWxml) {
429
- const _source = weapp?.enhance?.wxml ? processWxml(source).code : source;
567
+ let _source;
568
+ if (weapp?.enhance?.wxml) {
569
+ const { code, deps } = processWxml(source);
570
+ _source = code;
571
+ for (const wxsDep of deps.filter((x) => x.tagName === "wxs")) {
572
+ if (jsExtensions.includes(wxsDep.attrs.lang) || /\.wxs\.[jt]s$/.test(wxsDep.value)) {
573
+ const wxsPath = path4.resolve(path4.dirname(filepath), wxsDep.value);
574
+ if (await fs4.exists(wxsPath)) {
575
+ this.addWatchFile(wxsPath);
576
+ const code2 = await fs4.readFile(wxsPath, "utf8");
577
+ const res = transformWxsCode(code2, {
578
+ filename: wxsPath
579
+ });
580
+ if (res && res.code) {
581
+ this.emitFile({
582
+ type: "asset",
583
+ fileName: ctx.relativeSrcRoot(relative(removeExtension(wxsPath))),
584
+ source: res.code
585
+ });
586
+ }
587
+ }
588
+ }
589
+ }
590
+ } else {
591
+ _source = source;
592
+ }
430
593
  this.emitFile({
431
594
  type: "asset",
432
595
  fileName: isHtml ? changeFileExtension(fileName, ctx.outputExtensions.wxml) : fileName,
@@ -573,15 +736,6 @@ function vitePluginWeapp(ctx, subPackageMeta) {
573
736
  // src/context.ts
574
737
  var debug2 = createDebugger("weapp-vite:context");
575
738
  var require2 = createRequire(import.meta.url);
576
- var logBuildAppFinishOnlyShowOnce = false;
577
- function logBuildAppFinish() {
578
- if (!logBuildAppFinishOnlyShowOnce) {
579
- logger_default.success("\u5E94\u7528\u6784\u5EFA\u5B8C\u6210\uFF01\u9884\u89C8\u65B9\u5F0F ( `2` \u79CD\u9009\u5176\u4E00\u5373\u53EF)\uFF1A");
580
- logger_default.info("\u6267\u884C `npm run open` / `yarn open` / `pnpm open` \u76F4\u63A5\u5728 `\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177` \u91CC\u6253\u5F00\u5F53\u524D\u5E94\u7528");
581
- logger_default.info("\u6216\u624B\u52A8\u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177,\u5BFC\u5165\u6839\u76EE\u5F55(`project.config.json` \u6587\u4EF6\u6240\u5728\u7684\u76EE\u5F55),\u5373\u53EF\u9884\u89C8\u6548\u679C");
582
- logBuildAppFinishOnlyShowOnce = true;
583
- }
584
- }
585
739
  function logBuildIndependentSubPackageFinish(root) {
586
740
  logger_default.success(`\u72EC\u7ACB\u5206\u5305 ${root} \u6784\u5EFA\u5B8C\u6210\uFF01`);
587
741
  }
@@ -612,7 +766,7 @@ var CompilerContext = class {
612
766
  */
613
767
  defineEnv;
614
768
  constructor(options) {
615
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = defu(options, {
769
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = defu2(options, {
616
770
  cwd: process.cwd(),
617
771
  isDev: false,
618
772
  projectConfig: {},
@@ -690,7 +844,6 @@ var CompilerContext = class {
690
844
  if (e.code === "END") {
691
845
  debug2?.("dev watcher listen end");
692
846
  await this.buildSubPackage();
693
- logBuildAppFinish();
694
847
  resolve(e);
695
848
  } else if (e.code === "ERROR") {
696
849
  reject(e);
@@ -702,7 +855,7 @@ var CompilerContext = class {
702
855
  }
703
856
  getConfig(subPackageMeta, ...configs) {
704
857
  if (this.isDev) {
705
- return defu(
858
+ return defu2(
706
859
  this.inlineConfig,
707
860
  ...configs,
708
861
  {
@@ -727,7 +880,7 @@ var CompilerContext = class {
727
880
  }
728
881
  );
729
882
  } else {
730
- const inlineConfig = defu(
883
+ const inlineConfig = defu2(
731
884
  this.inlineConfig,
732
885
  ...configs,
733
886
  {
@@ -745,20 +898,19 @@ var CompilerContext = class {
745
898
  }
746
899
  }
747
900
  async runProd() {
748
- if (this.mpDistRoot) {
749
- await fs5.emptyDir(this.outDir);
750
- logger_default.success(`\u5DF2\u6E05\u7A7A ${this.mpDistRoot} \u76EE\u5F55`);
751
- }
752
901
  debug2?.("prod build start");
753
902
  const output = await build(
754
903
  this.getConfig()
755
904
  );
756
905
  debug2?.("prod build end");
757
906
  await this.buildSubPackage();
758
- logBuildAppFinish();
759
907
  return output;
760
908
  }
761
909
  async build() {
910
+ if (this.mpDistRoot) {
911
+ await fs5.emptyDir(this.outDir);
912
+ logger_default.success(`\u5DF2\u6E05\u7A7A ${this.mpDistRoot} \u76EE\u5F55`);
913
+ }
762
914
  debug2?.("build start");
763
915
  if (this.isDev) {
764
916
  await this.runDev();
@@ -789,7 +941,7 @@ var CompilerContext = class {
789
941
  command: this.isDev ? "serve" : "build",
790
942
  mode: this.mode
791
943
  }, void 0, this.cwd);
792
- this.inlineConfig = defu({
944
+ this.inlineConfig = defu2({
793
945
  configFile: false
794
946
  }, loaded?.config, {
795
947
  build: {
@@ -800,11 +952,11 @@ var CompilerContext = class {
800
952
  entryFileNames: (chunkInfo) => {
801
953
  const name = this.relativeSrcRoot(chunkInfo.name);
802
954
  if (name.endsWith(".ts")) {
803
- const baseFileName = removeExtension(name);
955
+ const baseFileName = removeExtension2(name);
804
956
  if (baseFileName.endsWith(".wxs")) {
805
957
  return baseFileName;
806
958
  }
807
- return addExtension(baseFileName, ".js");
959
+ return addExtension2(baseFileName, ".js");
808
960
  }
809
961
  return name;
810
962
  }
@@ -902,7 +1054,7 @@ var CompilerContext = class {
902
1054
  logger_default.success(`${dep} \u4F9D\u8D56\u672A\u53D1\u751F\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5904\u7406!`);
903
1055
  continue;
904
1056
  }
905
- const mergedOptions = defu(options, {
1057
+ const mergedOptions = defu2(options, {
906
1058
  entry: {
907
1059
  index: require2.resolve(dep)
908
1060
  },
@@ -1066,7 +1218,7 @@ var CompilerContext = class {
1066
1218
  entries: this.entries
1067
1219
  };
1068
1220
  debug2?.("scanComponentEntry start", componentEntry);
1069
- const baseName = removeExtension(path5.resolve(dirname, componentEntry));
1221
+ const baseName = removeExtension2(path5.resolve(dirname, componentEntry));
1070
1222
  const jsEntry = await findJsEntry(baseName);
1071
1223
  const partialEntry = {
1072
1224
  path: jsEntry