build.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.notFound = exports.createBuildOptions = exports.buildOptions = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const debug_1 = __importDefault(require("debug"));
  10. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  11. const entry_1 = require("../plugins/entry");
  12. const debugChunk = (0, debug_1.default)('uni:chunk');
  13. function buildOptions() {
  14. const platform = process.env.UNI_PLATFORM;
  15. const inputDir = process.env.UNI_INPUT_DIR;
  16. const outputDir = process.env.UNI_OUTPUT_DIR;
  17. // 开始编译时,清空输出目录
  18. if (fs_1.default.existsSync(outputDir)) {
  19. (0, uni_cli_shared_1.emptyDir)(outputDir, ['project.config.json', 'project.private.config.json']);
  20. }
  21. return createBuildOptions(inputDir, platform);
  22. }
  23. exports.buildOptions = buildOptions;
  24. function createBuildOptions(inputDir, platform) {
  25. const { renderDynamicImport } = (0, uni_cli_shared_1.dynamicImportPolyfill)();
  26. return {
  27. // sourcemap: 'inline', // TODO
  28. // target: ['chrome53'], // 由小程序自己启用 es6 编译
  29. emptyOutDir: false,
  30. lib: {
  31. // 必须使用 lib 模式,否则会生成 preload 等代码
  32. fileName: 'app.js',
  33. entry: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
  34. formats: ['cjs'],
  35. },
  36. rollupOptions: {
  37. input: parseRollupInput(inputDir, platform),
  38. output: {
  39. entryFileNames(chunk) {
  40. if (chunk.name === 'main') {
  41. return 'app.js';
  42. }
  43. return chunk.name + '.js';
  44. },
  45. format: 'cjs',
  46. manualChunks: createMoveToVendorChunkFn(),
  47. chunkFileNames: createChunkFileNames(inputDir),
  48. plugins: [
  49. {
  50. name: 'dynamic-import-polyfill',
  51. renderDynamicImport(options) {
  52. const { targetModuleId } = options;
  53. if (targetModuleId && (0, uni_cli_shared_1.isMiniProgramAssetFile)(targetModuleId)) {
  54. return {
  55. left: 'Promise.resolve(require(',
  56. right: '))',
  57. };
  58. }
  59. return renderDynamicImport.call(this, options);
  60. },
  61. },
  62. ],
  63. },
  64. },
  65. };
  66. }
  67. exports.createBuildOptions = createBuildOptions;
  68. function parseRollupInput(inputDir, platform) {
  69. const inputOptions = {
  70. app: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
  71. };
  72. if (process.env.UNI_MP_PLUGIN) {
  73. return inputOptions;
  74. }
  75. const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir);
  76. const plugins = manifestJson[platform]?.plugins || {};
  77. Object.keys(plugins).forEach((name) => {
  78. const pluginExport = plugins[name].export;
  79. if (!pluginExport) {
  80. return;
  81. }
  82. const pluginExportFile = path_1.default.resolve(inputDir, pluginExport);
  83. if (!fs_1.default.existsSync(pluginExportFile)) {
  84. notFound(pluginExportFile);
  85. }
  86. inputOptions[(0, uni_cli_shared_1.removeExt)(pluginExport)] = pluginExportFile;
  87. });
  88. return inputOptions;
  89. }
  90. function isVueJs(id) {
  91. return id.includes('\0plugin-vue:export-helper');
  92. }
  93. const chunkFileNameBlackList = ['main', 'pages.json', 'manifest.json'];
  94. function createMoveToVendorChunkFn() {
  95. const cache = new Map();
  96. const inputDir = (0, uni_cli_shared_1.normalizePath)(process.env.UNI_INPUT_DIR);
  97. return (id, { getModuleInfo }) => {
  98. const normalizedId = (0, uni_cli_shared_1.normalizePath)(id);
  99. const filename = normalizedId.split('?')[0];
  100. // 处理资源文件
  101. if (uni_cli_shared_1.DEFAULT_ASSETS_RE.test(filename)) {
  102. debugChunk('common/assets', normalizedId);
  103. return 'common/assets';
  104. }
  105. // 处理项目内的js,ts文件
  106. if (uni_cli_shared_1.EXTNAME_JS_RE.test(filename)) {
  107. if (filename.startsWith(inputDir) && !filename.includes('node_modules')) {
  108. const chunkFileName = (0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizePath)(path_1.default.relative(inputDir, filename)));
  109. if (!chunkFileNameBlackList.includes(chunkFileName) &&
  110. !(0, uni_cli_shared_1.hasJsonFile)(chunkFileName) // 无同名的page,component
  111. ) {
  112. debugChunk(chunkFileName, normalizedId);
  113. return chunkFileName;
  114. }
  115. return;
  116. }
  117. // 非项目内的 js 资源,均打包到 vendor
  118. debugChunk('common/vendor', normalizedId);
  119. return 'common/vendor';
  120. }
  121. if (isVueJs(normalizedId) ||
  122. (normalizedId.includes('node_modules') &&
  123. !(0, uni_cli_shared_1.isCSSRequest)(normalizedId) &&
  124. // 使用原始路径,格式化的可能找不到模块信息 https://github.com/dcloudio/uni-app/issues/3425
  125. staticImportedByEntry(id, getModuleInfo, cache))) {
  126. debugChunk('common/vendor', id);
  127. return 'common/vendor';
  128. }
  129. };
  130. }
  131. function staticImportedByEntry(id, getModuleInfo, cache, importStack = []) {
  132. if (cache.has(id)) {
  133. return cache.get(id);
  134. }
  135. if (importStack.includes(id)) {
  136. // circular deps!
  137. cache.set(id, false);
  138. return false;
  139. }
  140. const mod = getModuleInfo(id);
  141. if (!mod) {
  142. cache.set(id, false);
  143. return false;
  144. }
  145. if (mod.isEntry) {
  146. cache.set(id, true);
  147. return true;
  148. }
  149. const someImporterIs = mod.importers.some((importer) => staticImportedByEntry(importer, getModuleInfo, cache, importStack.concat(id)));
  150. cache.set(id, someImporterIs);
  151. return someImporterIs;
  152. }
  153. function createChunkFileNames(inputDir) {
  154. return function chunkFileNames(chunk) {
  155. if (chunk.isDynamicEntry && chunk.facadeModuleId) {
  156. let id = chunk.facadeModuleId;
  157. if ((0, entry_1.isUniPageUrl)(id)) {
  158. id = path_1.default.resolve(process.env.UNI_INPUT_DIR, (0, entry_1.parseVirtualPagePath)(id));
  159. }
  160. else if ((0, entry_1.isUniComponentUrl)(id)) {
  161. id = path_1.default.resolve(process.env.UNI_INPUT_DIR, (0, entry_1.parseVirtualComponentPath)(id));
  162. }
  163. return (0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizeMiniProgramFilename)(id, inputDir)) + '.js';
  164. }
  165. return '[name].js';
  166. };
  167. }
  168. function notFound(filename) {
  169. console.log();
  170. console.error(uni_cli_shared_1.M['file.notfound'].replace('{file}', filename));
  171. console.log();
  172. process.exit(0);
  173. }
  174. exports.notFound = notFound;