alias.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.formatInstallHBuilderXPluginTips = exports.moduleAliasFormatter = exports.installHBuilderXPlugin = exports.initModuleAlias = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const module_alias_1 = __importDefault(require("module-alias"));
  9. const env_1 = require("./env");
  10. const hbxPlugins = {
  11. // typescript: 'compile-typescript/node_modules/typescript',
  12. less: 'compile-less/node_modules/less',
  13. sass: 'compile-dart-sass/node_modules/sass',
  14. stylus: 'compile-stylus/node_modules/stylus',
  15. pug: 'compile-pug-cli/node_modules/pug',
  16. };
  17. function initModuleAlias() {
  18. const compilerSfcPath = require.resolve('@vue/compiler-sfc');
  19. const serverRendererPath = require.resolve('@vue/server-renderer');
  20. module_alias_1.default.addAliases({
  21. '@vue/shared': require.resolve('@vue/shared'),
  22. '@vue/shared/dist/shared.esm-bundler.js': require.resolve('@vue/shared/dist/shared.esm-bundler.js'),
  23. '@vue/compiler-dom': require.resolve('@vue/compiler-dom'),
  24. '@vue/compiler-sfc': compilerSfcPath,
  25. '@vue/server-renderer': serverRendererPath,
  26. 'vue/compiler-sfc': compilerSfcPath,
  27. 'vue/server-renderer': serverRendererPath,
  28. });
  29. if (process.env.VITEST) {
  30. module_alias_1.default.addAliases({
  31. vue: '@dcloudio/uni-h5-vue',
  32. 'vue/package.json': '@dcloudio/uni-h5-vue/package.json',
  33. });
  34. }
  35. if ((0, env_1.isInHBuilderX)()) {
  36. // 又是为了复用 HBuilderX 的插件逻辑,硬编码映射
  37. Object.keys(hbxPlugins).forEach((name) => {
  38. module_alias_1.default.addAlias(name, path_1.default.resolve(process.env.UNI_HBUILDERX_PLUGINS, hbxPlugins[name]));
  39. });
  40. // web 平台用了 vite 内置 css 插件,该插件会加载预编译器如scss、less等,需要转向到 HBuilderX 的对应编译器插件
  41. if (process.env.UNI_PLATFORM === 'h5' ||
  42. process.env.UNI_PLATFORM === 'web') {
  43. // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/packages.ts#L92
  44. // 拦截预编译器
  45. const join = path_1.default.join;
  46. path_1.default.join = function (...paths) {
  47. if (paths.length === 4) {
  48. // path.join(basedir, 'node_modules', pkgName, 'package.json')
  49. // const basedir = paths[0]
  50. const nodeModules = paths[1]; // = node_modules
  51. const pkgName = paths[2];
  52. const packageJson = paths[3]; // = package.json
  53. if (nodeModules === 'node_modules' &&
  54. packageJson === 'package.json' &&
  55. hbxPlugins[pkgName]) {
  56. return path_1.default.resolve(process.env.UNI_HBUILDERX_PLUGINS, hbxPlugins[pkgName], packageJson);
  57. }
  58. }
  59. return join(...paths);
  60. };
  61. // https://github.com/vitejs/vite/blob/892916d040a035edde1add93c192e0b0c5c9dd86/packages/vite/src/node/plugins/css.ts#L1481
  62. // const oldSync = resovle.sync
  63. // resovle.sync = (id: string, opts?: SyncOpts) => {
  64. // if ((hbxPlugins as any)[id]) {
  65. // return path.resolve(
  66. // process.env.UNI_HBUILDERX_PLUGINS,
  67. // hbxPlugins[id as keyof typeof hbxPlugins]
  68. // )
  69. // }
  70. // return oldSync(id, opts)
  71. // }
  72. }
  73. }
  74. }
  75. exports.initModuleAlias = initModuleAlias;
  76. function supportAutoInstallPlugin() {
  77. return !!process.env.HX_Version;
  78. }
  79. function installHBuilderXPlugin(plugin) {
  80. if (!supportAutoInstallPlugin()) {
  81. return;
  82. }
  83. return console.error(`%HXRunUniAPPPluginName%${plugin}%HXRunUniAPPPluginName%`);
  84. }
  85. exports.installHBuilderXPlugin = installHBuilderXPlugin;
  86. const installPreprocessorTips = {};
  87. exports.moduleAliasFormatter = {
  88. test(msg) {
  89. return msg.includes('Preprocessor dependency');
  90. },
  91. format(msg) {
  92. let lang = '';
  93. let preprocessor = '';
  94. if (msg.includes(`"sass"`)) {
  95. lang = 'sass';
  96. preprocessor = 'compile-dart-sass';
  97. }
  98. else if (msg.includes(`"less"`)) {
  99. lang = 'less';
  100. preprocessor = 'compile-less';
  101. }
  102. else if (msg.includes('"stylus"')) {
  103. lang = 'stylus';
  104. preprocessor = 'compile-stylus';
  105. }
  106. if (lang) {
  107. // 仅提醒一次
  108. if (installPreprocessorTips[lang]) {
  109. return '';
  110. }
  111. installPreprocessorTips[lang] = true;
  112. installHBuilderXPlugin(preprocessor);
  113. return formatInstallHBuilderXPluginTips(lang, preprocessor);
  114. }
  115. return msg;
  116. },
  117. };
  118. function formatInstallHBuilderXPluginTips(lang, preprocessor) {
  119. return `预编译器错误:代码使用了${lang}语言,但未安装相应的编译器插件,${supportAutoInstallPlugin() ? '正在从' : '请前往'}插件市场安装该插件:
  120. https://ext.dcloud.net.cn/plugin?name=${preprocessor}`;
  121. }
  122. exports.formatInstallHBuilderXPluginTips = formatInstallHBuilderXPluginTips;