@nizza/devkit 0.1.0-alpha.12 → 0.1.0-alpha.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ import path from 'node:path';
2
+ import { promises } from 'node:fs';
3
+
4
+ const defaultCacheDir = "node_modules/.vite";
5
+ function viteBasicSslPlugin() {
6
+ return {
7
+ name: "vite:basic-ssl",
8
+ async configResolved(config) {
9
+ const certificate = await getCertificate((config.cacheDir ?? defaultCacheDir) + "/basic-ssl");
10
+ const https = () => ({ cert: certificate, key: certificate });
11
+ config.server.https = Object.assign({}, config.server.https, https());
12
+ config.preview.https = Object.assign({}, config.preview.https, https());
13
+ }
14
+ };
15
+ }
16
+ async function getCertificate(cacheDir) {
17
+ const cachePath = path.join(cacheDir, "_cert.pem");
18
+ try {
19
+ const [stat, content] = await Promise.all([
20
+ promises.stat(cachePath),
21
+ promises.readFile(cachePath, "utf8")
22
+ ]);
23
+ if (Date.now() - stat.ctime.valueOf() > 30 * 24 * 60 * 60 * 1e3) {
24
+ throw new Error("cache is outdated.");
25
+ }
26
+ return content;
27
+ } catch {
28
+ const content = (await import('./chunks/certificate.mjs')).createCertificate();
29
+ promises.mkdir(cacheDir, { recursive: true }).then(() => promises.writeFile(cachePath, content)).catch(() => {
30
+ });
31
+ return content;
32
+ }
33
+ }
34
+
35
+ export { viteBasicSslPlugin as default, getCertificate };
@@ -1,12 +1,12 @@
1
- import { bundleAnalysisPlugin, basePlugins } from '../plugins.mjs';
1
+ import { existsSync } from 'fs';
2
+ import { resolve } from 'path';
3
+ import { bundleAnalysisPlugin, httpsServerPlugin, basePlugins } from '../plugins.mjs';
2
4
  import { Target, ViteMode, Flag, Stage } from '../shared/types.mjs';
3
5
  import { getPlugins, when } from '../shared/utils/helpers.mjs';
4
6
  import 'minimatch';
5
7
  import '../../../shared/logger.mjs';
6
8
  import '@nx/devkit';
7
9
  import 'crypto';
8
- import { existsSync } from 'fs';
9
- import { resolve } from 'path';
10
10
  import 'chalk';
11
11
  import 'mri';
12
12
  import 'vite';
@@ -30,7 +30,7 @@ const getBaseConfig = (ctx)=>{
30
30
  const envFilePath = existsSync(resolve(path, 'src', envPath)) ? envPath : fallbackEnvPath;
31
31
  return {
32
32
  envPrefix,
33
- plugins: getPlugins(ctx, basePlugins, bundleAnalysisPlugin),
33
+ plugins: getPlugins(ctx, basePlugins, httpsServerPlugin, bundleAnalysisPlugin),
34
34
  resolve: {
35
35
  alias: {
36
36
  ...when(resolveEnvs.default && !omittedStages.includes(stage), {
@@ -17,6 +17,7 @@ import 'crypto';
17
17
  import 'fs';
18
18
  import 'chalk';
19
19
  import 'mri';
20
+ import viteBasicSslPlugin from '../../node_modules/.pnpm/@vitejs_plugin-basic-ssl@1.0.2_vite@5.0.4/node_modules/@vitejs/plugin-basic-ssl/dist/index.mjs';
20
21
 
21
22
  /**
22
23
  * Base plugins configuration for apps and libs.
@@ -83,5 +84,16 @@ import 'mri';
83
84
  ],
84
85
  condition: ({ projectConfig })=>projectConfig.shadowDom
85
86
  };
87
+ /**
88
+ * Use the plugin @vitejs/plugin-basic-ssl to create a SSL
89
+ * certificate and create https server.
90
+ */ const httpsServerPlugin = {
91
+ generate: ()=>[
92
+ viteBasicSslPlugin()
93
+ ],
94
+ condition: ({ viteCmd })=>{
95
+ return viteCmd.flags?.[Flag.HTTPS];
96
+ }
97
+ };
86
98
 
87
- export { basePlugins, bundleAnalysisPlugin, dtsPlugin, injectCssByJsPlugin, injectCssIntoShadowDomPlugin };
99
+ export { basePlugins, bundleAnalysisPlugin, dtsPlugin, httpsServerPlugin, injectCssByJsPlugin, injectCssIntoShadowDomPlugin };
@@ -22,6 +22,7 @@ var Flag;
22
22
  Flag["ANALYZE"] = "analyze";
23
23
  Flag["VERBOSE"] = "verbose";
24
24
  Flag["VERBOSE_ALIAS"] = "v";
25
+ Flag["HTTPS"] = "https";
25
26
  })(Flag || (Flag = {}));
26
27
  var Target;
27
28
  (function(Target) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/devkit",
3
- "version": "0.1.0-alpha.12",
3
+ "version": "0.1.0-alpha.14",
4
4
  "type": "module",
5
5
  "main": "dist/cjs/index.cjs",
6
6
  "module": "dist/esm/index.mjs",