resolveVue.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.resolveCompiler = exports.resolveVue = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const fs_extra_1 = __importDefault(require("fs-extra"));
  9. const pathUtils_1 = require("./pathUtils");
  10. const chalk_1 = __importDefault(require("chalk"));
  11. const fsUtils_1 = require("./fsUtils");
  12. let resolved = undefined;
  13. // Resolve the correct `vue` and `@vue.compiler-sfc` to use.
  14. // If the user project has local installations of these, they should be used;
  15. // otherwise, fallback to the dependency of Vite itself.
  16. function resolveVue(root) {
  17. if (resolved) {
  18. return resolved;
  19. }
  20. let vueVersion;
  21. let vueBasePath;
  22. let compilerPath;
  23. const projectPkg = JSON.parse(fsUtils_1.lookupFile(root, ['package.json']) || `{}`);
  24. let isLocal = !!(projectPkg.dependencies && projectPkg.dependencies.vue);
  25. if (isLocal) {
  26. try {
  27. const userVuePkg = pathUtils_1.resolveFrom(root, 'vue/package.json');
  28. vueBasePath = path_1.default.dirname(userVuePkg);
  29. vueVersion = fs_extra_1.default.readJSONSync(userVuePkg).version;
  30. isLocal = true;
  31. }
  32. catch (e) {
  33. // user has vue listed but not actually installed.
  34. isLocal = false;
  35. }
  36. }
  37. if (isLocal) {
  38. // user has local vue, verify that the same version of @vue/compiler-sfc
  39. // is also installed.
  40. try {
  41. const compilerPkgPath = pathUtils_1.resolveFrom(root, '@vue/compiler-sfc/package.json');
  42. const compilerPkg = require(compilerPkgPath);
  43. if (compilerPkg.version !== vueVersion) {
  44. throw new Error();
  45. }
  46. compilerPath = path_1.default.join(path_1.default.dirname(compilerPkgPath), compilerPkg.main);
  47. }
  48. catch (e) {
  49. // user has local vue but has no compiler-sfc
  50. console.error(chalk_1.default.red(`[vite] Error: a local installation of \`vue\` is detected but ` +
  51. `no matching \`@vue/compiler-sfc\` is found. Make sure to install ` +
  52. `both and use the same version.`));
  53. compilerPath = require.resolve('@vue/compiler-sfc');
  54. }
  55. }
  56. else {
  57. // user has no local vue, use vite's dependency version
  58. vueVersion = require('vue/package.json').version;
  59. vueBasePath = path_1.default.dirname(require.resolve('vue/package.json'));
  60. compilerPath = require.resolve('@vue/compiler-sfc');
  61. }
  62. const resolvePath = (name, from) => pathUtils_1.resolveFrom(from, `@vue/${name}/dist/${name}.esm-bundler.js`);
  63. // resolve nested dependencies with correct base dirs so that this works with
  64. // strict package managers - e.g. pnpm / yarn 2
  65. const runtimeDomPath = resolvePath('runtime-dom', vueBasePath);
  66. const runtimeCorePath = resolvePath('runtime-core', runtimeDomPath);
  67. const reactivityPath = resolvePath('reactivity', runtimeCorePath);
  68. const sharedPath = resolvePath('shared', runtimeCorePath);
  69. resolved = {
  70. version: vueVersion,
  71. vue: runtimeDomPath,
  72. '@vue/runtime-dom': runtimeDomPath,
  73. '@vue/runtime-core': runtimeCorePath,
  74. '@vue/reactivity': reactivityPath,
  75. '@vue/shared': sharedPath,
  76. compiler: compilerPath,
  77. isLocal
  78. };
  79. return resolved;
  80. }
  81. exports.resolveVue = resolveVue;
  82. function resolveCompiler(cwd) {
  83. return require(resolveVue(cwd).compiler);
  84. }
  85. exports.resolveCompiler = resolveCompiler;
  86. //# sourceMappingURL=resolveVue.js.map