No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDevModeHandle = exports.ɵcodegenPublicDirectory = exports.build = exports.discover = exports.vitePluginDiscover = exports.viteDiscoverWithNpmDependency = exports.init = exports.initViteTemplate = exports.DEFAULT_BUILD_SCRIPT = exports.type = exports.support = exports.name = void 0;
  4. const child_process_1 = require("child_process");
  5. const fs_1 = require("fs");
  6. const fs_extra_1 = require("fs-extra");
  7. const path_1 = require("path");
  8. const __1 = require("..");
  9. const proxy_1 = require("../../hosting/proxy");
  10. const prompt_1 = require("../../prompt");
  11. const utils_1 = require("../utils");
  12. exports.name = "Vite";
  13. exports.support = "experimental";
  14. exports.type = 4;
  15. const CLI_COMMAND = (0, path_1.join)("node_modules", ".bin", process.platform === "win32" ? "vite.cmd" : "vite");
  16. exports.DEFAULT_BUILD_SCRIPT = ["vite build", "tsc && vite build"];
  17. const initViteTemplate = (template) => async (setup) => await init(setup, template);
  18. exports.initViteTemplate = initViteTemplate;
  19. async function init(setup, baseTemplate = "vanilla") {
  20. const template = await (0, prompt_1.promptOnce)({
  21. type: "list",
  22. default: "JavaScript",
  23. message: "What language would you like to use?",
  24. choices: [
  25. { name: "JavaScript", value: baseTemplate },
  26. { name: "TypeScript", value: `${baseTemplate}-ts` },
  27. ],
  28. });
  29. (0, child_process_1.execSync)(`npm create vite@latest ${setup.hosting.source} --yes -- --template ${template}`, {
  30. stdio: "inherit",
  31. });
  32. (0, child_process_1.execSync)(`npm install`, { stdio: "inherit", cwd: setup.hosting.source });
  33. }
  34. exports.init = init;
  35. const viteDiscoverWithNpmDependency = (dep) => async (dir) => await discover(dir, undefined, dep);
  36. exports.viteDiscoverWithNpmDependency = viteDiscoverWithNpmDependency;
  37. const vitePluginDiscover = (plugin) => async (dir) => await discover(dir, plugin);
  38. exports.vitePluginDiscover = vitePluginDiscover;
  39. async function discover(dir, plugin, npmDependency) {
  40. if (!(0, fs_1.existsSync)((0, path_1.join)(dir, "package.json")))
  41. return;
  42. const additionalDep = npmDependency && (0, __1.findDependency)(npmDependency, { cwd: dir, depth: 0, omitDev: true });
  43. const depth = plugin ? undefined : 0;
  44. const configFilesExist = await Promise.all([
  45. (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "vite.config.js")),
  46. (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "vite.config.ts")),
  47. ]);
  48. const anyConfigFileExists = configFilesExist.some((it) => it);
  49. if (!anyConfigFileExists && !(0, __1.findDependency)("vite", { cwd: dir, depth, omitDev: false }))
  50. return;
  51. if (npmDependency && !additionalDep)
  52. return;
  53. const { appType, publicDir: publicDirectory, plugins } = await getConfig(dir);
  54. if (plugin && !plugins.find(({ name }) => name === plugin))
  55. return;
  56. return { mayWantBackend: appType !== "spa", publicDirectory };
  57. }
  58. exports.discover = discover;
  59. async function build(root) {
  60. const { build } = (0, __1.relativeRequire)(root, "vite");
  61. await (0, utils_1.warnIfCustomBuildScript)(root, exports.name, exports.DEFAULT_BUILD_SCRIPT);
  62. await build({ root });
  63. }
  64. exports.build = build;
  65. async function ɵcodegenPublicDirectory(root, dest) {
  66. const viteConfig = await getConfig(root);
  67. const viteDistPath = (0, path_1.join)(root, viteConfig.build.outDir);
  68. await (0, fs_extra_1.copy)(viteDistPath, dest);
  69. }
  70. exports.ɵcodegenPublicDirectory = ɵcodegenPublicDirectory;
  71. async function getDevModeHandle(dir) {
  72. const host = new Promise((resolve) => {
  73. const serve = (0, child_process_1.spawn)(CLI_COMMAND, [], { cwd: dir });
  74. serve.stdout.on("data", (data) => {
  75. process.stdout.write(data);
  76. const match = data.toString().match(/(http:\/\/.+:\d+)/);
  77. if (match)
  78. resolve(match[1]);
  79. });
  80. serve.stderr.on("data", (data) => {
  81. process.stderr.write(data);
  82. });
  83. });
  84. return (0, proxy_1.proxyRequestHandler)(await host, "Vite Development Server", { forceCascade: true });
  85. }
  86. exports.getDevModeHandle = getDevModeHandle;
  87. async function getConfig(root) {
  88. const { resolveConfig } = (0, __1.relativeRequire)(root, "vite");
  89. return await resolveConfig({ root }, "build", "production");
  90. }