index.cjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const path = require('node:path');
  4. const node_fs = require('node:fs');
  5. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
  6. const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
  7. const defaultCacheDir = "node_modules/.vite";
  8. function viteBasicSslPlugin() {
  9. return {
  10. name: "vite:basic-ssl",
  11. async configResolved(config) {
  12. const certificate = await getCertificate((config.cacheDir ?? defaultCacheDir) + "/basic-ssl");
  13. const https = () => ({ cert: certificate, key: certificate });
  14. config.server.https = Object.assign({}, config.server.https, https());
  15. config.preview.https = Object.assign({}, config.preview.https, https());
  16. }
  17. };
  18. }
  19. async function getCertificate(cacheDir) {
  20. const cachePath = path__default.join(cacheDir, "_cert.pem");
  21. try {
  22. const [stat, content] = await Promise.all([
  23. node_fs.promises.stat(cachePath),
  24. node_fs.promises.readFile(cachePath, "utf8")
  25. ]);
  26. if (Date.now() - stat.ctime.valueOf() > 30 * 24 * 60 * 60 * 1e3) {
  27. throw new Error("cache is outdated.");
  28. }
  29. return content;
  30. } catch {
  31. const content = (await import('./chunks/certificate.cjs')).createCertificate();
  32. node_fs.promises.mkdir(cacheDir, { recursive: true }).then(() => node_fs.promises.writeFile(cachePath, content)).catch(() => {
  33. });
  34. return content;
  35. }
  36. }
  37. module.exports = viteBasicSslPlugin;
  38. module.exports["default"] = viteBasicSslPlugin;
  39. module.exports.getCertificate = getCertificate;