build.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.buildApp = exports.initBuildOptions = exports.buildSSR = exports.build = exports.buildByVite = void 0;
  30. const path_1 = __importDefault(require("path"));
  31. const fs_extra_1 = __importDefault(require("fs-extra"));
  32. const shared_1 = require("@vue/shared");
  33. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  34. const utils_1 = require("./utils");
  35. async function buildByVite(inlineConfig) {
  36. return Promise.resolve().then(() => __importStar(require('vite'))).then(({ build }) => build(inlineConfig));
  37. }
  38. exports.buildByVite = buildByVite;
  39. async function build(options) {
  40. if (process.env.UNI_APP_X !== 'true' && options.platform === 'app') {
  41. return buildApp(options);
  42. }
  43. return buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, (0, utils_1.cleanOptions)(options))));
  44. }
  45. exports.build = build;
  46. async function buildSSR(options) {
  47. const outputDir = process.env.UNI_OUTPUT_DIR;
  48. const ssrClientDir = path_1.default.resolve(outputDir, 'client');
  49. process.env.UNI_OUTPUT_DIR = ssrClientDir;
  50. const ssrBuildClientOptions = (0, utils_1.cleanOptions)(options);
  51. ssrBuildClientOptions.ssrManifest = true;
  52. ssrBuildClientOptions.outDir = process.env.UNI_OUTPUT_DIR;
  53. process.env.UNI_SSR_CLIENT = 'true';
  54. await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, ssrBuildClientOptions)));
  55. const ssrServerDir = path_1.default.resolve(outputDir, 'server');
  56. process.env.UNI_OUTPUT_DIR = ssrServerDir;
  57. const ssrBuildServerOptions = (0, utils_1.cleanOptions)(options);
  58. ssrBuildServerOptions.ssr = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'entry-server.js');
  59. ssrBuildServerOptions.outDir = process.env.UNI_OUTPUT_DIR;
  60. process.env.UNI_SSR_CLIENT = '';
  61. process.env.UNI_SSR_SERVER = 'true';
  62. await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, ssrBuildServerOptions)));
  63. // copy ssr-manfiest.json to server
  64. const assets = ['ssr-manifest.json', 'index.html'];
  65. assets.forEach((asset) => {
  66. const ssrManifestFile = path_1.default.join(ssrClientDir, asset);
  67. if (fs_extra_1.default.existsSync(ssrManifestFile)) {
  68. fs_extra_1.default.copyFileSync(ssrManifestFile, path_1.default.join(ssrServerDir, asset));
  69. }
  70. });
  71. }
  72. exports.buildSSR = buildSSR;
  73. function initBuildOptions(options, build) {
  74. return {
  75. root: process.env.VITE_ROOT_DIR,
  76. configFile: options.config,
  77. base: options.base,
  78. logLevel: options.logLevel,
  79. clearScreen: options.clearScreen,
  80. mode: options.mode,
  81. build,
  82. };
  83. }
  84. exports.initBuildOptions = initBuildOptions;
  85. function buildManifestJson() {
  86. const platform = 'app';
  87. const inputDir = process.env.UNI_INPUT_DIR;
  88. const outputDir = process.env.UNI_OUTPUT_DIR;
  89. const pkg = require(path_1.default.resolve(__dirname, '../../package.json'));
  90. process.env.UNI_COMPILER_VERSION =
  91. process.env.UNI_COMPILER_VERSION ||
  92. pkg['uni-app']?.['compilerVersion'] ||
  93. '';
  94. const manifestJson = (0, uni_cli_shared_1.normalizeAppManifestJson)((0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir), (0, uni_cli_shared_1.parsePagesJsonOnce)(inputDir, platform));
  95. fs_extra_1.default.outputFileSync(path_1.default.resolve(outputDir, 'manifest.json'), JSON.stringify(manifestJson, null, 2));
  96. }
  97. async function buildApp(options) {
  98. if (options.manifest) {
  99. return buildManifestJson();
  100. }
  101. let appWatcher;
  102. if (options.watch) {
  103. appWatcher = new AppWatcher();
  104. }
  105. if (process.env.UNI_RENDERER === 'native') {
  106. // 纯原生渲染时,main.js + App.vue 需要跟页面分开,独立编译(因为需要包含 Vuex 等共享内容)
  107. process.env.UNI_COMPILER = 'nvue';
  108. process.env.UNI_RENDERER_NATIVE = 'appService';
  109. const nvueAppBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvueAppService: true, nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
  110. if (appWatcher) {
  111. appWatcher.setFirstWatcher(nvueAppBuilder);
  112. }
  113. process.env.UNI_RENDERER_NATIVE = 'pages';
  114. const nvueBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
  115. if (appWatcher) {
  116. appWatcher.setSecondWatcher(nvueBuilder);
  117. return appWatcher;
  118. }
  119. return;
  120. }
  121. // 指定为 vue 方便 App 插件初始化 vue 所需插件列表
  122. process.env.UNI_COMPILER = 'vue';
  123. const vueBuilder = await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, (0, utils_1.cleanOptions)(options))));
  124. if (appWatcher) {
  125. appWatcher.setFirstWatcher(vueBuilder);
  126. }
  127. // 临时指定为 nvue 方便 App 插件初始化 nvue 所需插件列表
  128. process.env.UNI_COMPILER = 'nvue';
  129. const nvueBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
  130. // 还原为 vue
  131. process.env.UNI_COMPILER = 'vue';
  132. if (appWatcher) {
  133. appWatcher.setSecondWatcher(nvueBuilder);
  134. return appWatcher;
  135. }
  136. }
  137. exports.buildApp = buildApp;
  138. class AppWatcher {
  139. constructor() {
  140. this._firstStart = false;
  141. this._firstEnd = false;
  142. this._secondStart = false;
  143. this._secondEnd = false;
  144. }
  145. on(_event, callback) {
  146. this._callback = callback;
  147. }
  148. setFirstWatcher(firstWatcher) {
  149. firstWatcher.on('event', (event) => {
  150. if (event.code === 'BUNDLE_START') {
  151. this._bundleFirstStart(event);
  152. }
  153. else if (event.code === 'BUNDLE_END') {
  154. this._bundleFirstEnd(event);
  155. }
  156. });
  157. }
  158. setSecondWatcher(secondWatcher) {
  159. secondWatcher.on('event', (event) => {
  160. if (event.code === 'BUNDLE_START') {
  161. this._bundleSecondStart(event);
  162. }
  163. else if (event.code === 'BUNDLE_END') {
  164. this._bundleSecondEnd(event);
  165. }
  166. });
  167. }
  168. _bundleFirstStart(event) {
  169. this._firstStart = true;
  170. this._bundleStart(event);
  171. }
  172. _bundleFirstEnd(event) {
  173. this._firstEnd = true;
  174. this._bundleEnd(event);
  175. }
  176. _bundleSecondStart(event) {
  177. this._secondStart = true;
  178. this._bundleStart(event);
  179. }
  180. _bundleSecondEnd(event) {
  181. this._secondEnd = true;
  182. this._bundleEnd(event);
  183. }
  184. _bundleStart(event) {
  185. if (this._firstStart && this._secondStart) {
  186. this._callback(event);
  187. }
  188. }
  189. _bundleEnd(event) {
  190. if (this._firstEnd && this._secondEnd) {
  191. this._callback(event);
  192. }
  193. }
  194. }