copy.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uniViteCopyPlugin = void 0;
  4. const watcher_1 = require("../../watcher");
  5. const messages_1 = require("../../messages");
  6. const logs_1 = require("../../logs");
  7. const uni_shared_1 = require("@dcloudio/uni-shared");
  8. function uniViteCopyPlugin({ targets, verbose, }) {
  9. let resolvedConfig;
  10. let initialized = false;
  11. return {
  12. name: 'uni:copy',
  13. apply: 'build',
  14. configResolved(config) {
  15. resolvedConfig = config;
  16. },
  17. writeBundle() {
  18. if (initialized) {
  19. return;
  20. }
  21. if (resolvedConfig.build.ssr) {
  22. return;
  23. }
  24. initialized = true;
  25. return new Promise((resolve) => {
  26. Promise.all(targets.map(({ watchOptions, ...target }) => {
  27. return new Promise((resolve) => {
  28. // 防抖,可能短时间触发很多次add,unlink
  29. const onChange = (0, uni_shared_1.debounce)(() => {
  30. (0, logs_1.resetOutput)('log');
  31. (0, logs_1.output)('log', messages_1.M['dev.watching.end']);
  32. }, 100, { setTimeout, clearTimeout });
  33. new watcher_1.FileWatcher({
  34. verbose,
  35. ...target,
  36. }).watch({
  37. cwd: process.env.UNI_INPUT_DIR,
  38. ...watchOptions,
  39. }, (watcher) => {
  40. if (process.env.NODE_ENV !== 'development' ||
  41. process.env.UNI_AUTOMATOR_CONFIG) {
  42. // 生产或自动化测试模式下,延迟 close,否则会影响 chokidar 初始化的 add 等事件
  43. setTimeout(() => {
  44. watcher.close().then(() => resolve(void 0));
  45. }, 2000);
  46. }
  47. else {
  48. resolve(void 0);
  49. }
  50. }, onChange);
  51. });
  52. })).then(() => resolve());
  53. });
  54. },
  55. };
  56. }
  57. exports.uniViteCopyPlugin = uniViteCopyPlugin;