config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.createConfig = void 0;
  7. const os_1 = __importDefault(require("os"));
  8. const fs_1 = __importDefault(require("fs"));
  9. const path_1 = __importDefault(require("path"));
  10. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  11. const utils_1 = require("../utils");
  12. const esbuildPrePlugin_1 = require("./esbuild/esbuildPrePlugin");
  13. const ssr_1 = require("./configureServer/ssr");
  14. const shared_1 = require("@vue/shared");
  15. function createConfig(options) {
  16. return function config(config, env) {
  17. const inputDir = process.env.UNI_INPUT_DIR;
  18. if ((0, uni_cli_shared_1.isInHBuilderX)()) {
  19. if (!fs_1.default.existsSync(path_1.default.resolve(inputDir, 'index.html'))) {
  20. console.error(`请确认您的项目模板是否支持vue3:根目录缺少 index.html`);
  21. process.exit();
  22. }
  23. }
  24. const server = {
  25. host: true,
  26. hmr: {
  27. // mac 内置浏览器版本较低不支持 globalThis,而 overlay 使用了 globalThis
  28. overlay: os_1.default.platform() !== 'win32'
  29. ? process.env.UNI_H5_BROWSER !== 'builtin'
  30. : true,
  31. },
  32. fs: { strict: false },
  33. watch: {
  34. ignored: [
  35. '**/uniCloud-aliyun/**',
  36. '**/uniCloud-tcb/**',
  37. '**/uni_modules/uniCloud/**',
  38. '**/__snapshots__/**',
  39. (0, uni_cli_shared_1.normalizePath)(path_1.default.join(inputDir, 'unpackage/**')),
  40. (0, uni_cli_shared_1.normalizePath)(path_1.default.join(inputDir, 'dist/**')),
  41. ],
  42. },
  43. ...(0, uni_cli_shared_1.getDevServerOptions)((0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir)),
  44. };
  45. if (server.port === '') {
  46. delete server.port;
  47. }
  48. const { server: userServer } = config;
  49. if (userServer) {
  50. if ((0, shared_1.hasOwn)(userServer, 'host')) {
  51. server.host = userServer.host;
  52. }
  53. if ((0, shared_1.hasOwn)(userServer, 'fs')) {
  54. (0, shared_1.extend)(server.fs, userServer.fs);
  55. }
  56. if ((0, shared_1.hasOwn)(userServer, 'watch')) {
  57. (0, shared_1.extend)(server.watch, userServer.watch);
  58. }
  59. }
  60. return {
  61. css: {
  62. postcss: {
  63. plugins: (0, uni_cli_shared_1.initPostcssPlugin)({
  64. uniApp: (0, uni_cli_shared_1.parseRpx2UnitOnce)(inputDir, process.env.UNI_PLATFORM),
  65. }),
  66. },
  67. },
  68. optimizeDeps: {
  69. entries: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
  70. exclude: ssr_1.external,
  71. esbuildOptions: {
  72. plugins: [(0, esbuildPrePlugin_1.esbuildPrePlugin)()],
  73. },
  74. },
  75. define: (0, utils_1.createDefine)(env.command, config),
  76. server,
  77. ssr: {
  78. external: ssr_1.external,
  79. },
  80. build: {
  81. target: process.env.UNI_APP_X === 'true'
  82. ? ['es2015', 'edge79', 'firefox62', 'chrome64', 'safari11.1']
  83. : undefined,
  84. rollupOptions: {
  85. // resolveSSRExternal 会判定package.json,hbx 工程可能没有,通过 rollup 来配置
  86. external: (0, uni_cli_shared_1.isSsr)(env.command, config) ? ssr_1.external : [],
  87. output: {
  88. chunkFileNames(chunkInfo) {
  89. const { assetsDir } = options.resolvedConfig.build;
  90. if (chunkInfo.facadeModuleId) {
  91. const dirname = path_1.default.relative(inputDir, path_1.default.dirname(chunkInfo.facadeModuleId));
  92. if (dirname) {
  93. return path_1.default.posix.join(assetsDir, (0, uni_cli_shared_1.normalizePath)(dirname).replace(/\//g, '-') +
  94. '-[name].[hash].js');
  95. }
  96. }
  97. return path_1.default.posix.join(assetsDir, '[name].[hash].js');
  98. },
  99. },
  100. },
  101. },
  102. };
  103. };
  104. }
  105. exports.createConfig = createConfig;