vite 6.0.0-alpha.17 → 6.0.0-alpha.19

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.
package/dist/node/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import path from 'node:path';
2
- import fs from 'node:fs';
2
+ import fs__default from 'node:fs';
3
3
  import { performance } from 'node:perf_hooks';
4
4
  import { EventEmitter } from 'events';
5
- import { I as colors, A as createLogger, r as resolveConfig } from './chunks/dep-DVlS5vGL.js';
5
+ import { I as colors, A as createLogger, r as resolveConfig } from './chunks/dep-BWEMV5Th.js';
6
6
  import { VERSION } from './constants.js';
7
7
  import 'node:fs/promises';
8
8
  import 'node:url';
@@ -12,7 +12,9 @@ import 'tty';
12
12
  import 'path';
13
13
  import 'esbuild';
14
14
  import 'fs';
15
- import 'assert';
15
+ import 'node:events';
16
+ import 'node:stream';
17
+ import 'node:string_decoder';
16
18
  import 'node:child_process';
17
19
  import 'node:http';
18
20
  import 'node:https';
@@ -32,7 +34,6 @@ import 'module';
32
34
  import 'node:assert';
33
35
  import 'node:v8';
34
36
  import 'node:worker_threads';
35
- import 'node:events';
36
37
  import 'crypto';
37
38
  import 'querystring';
38
39
  import 'node:buffer';
@@ -41,6 +42,7 @@ import 'zlib';
41
42
  import 'buffer';
42
43
  import 'https';
43
44
  import 'tls';
45
+ import 'assert';
44
46
  import 'node:zlib';
45
47
 
46
48
  function toArr(any) {
@@ -656,293 +658,281 @@ class CAC extends EventEmitter {
656
658
 
657
659
  const cac = (name = "") => new CAC(name);
658
660
 
659
- const cli = cac('vite');
661
+ const cli = cac("vite");
660
662
  let profileSession = global.__vite_profile_session;
661
663
  let profileCount = 0;
662
664
  const stopProfiler = (log) => {
663
- if (!profileSession)
664
- return;
665
- return new Promise((res, rej) => {
666
- profileSession.post('Profiler.stop', (err, { profile }) => {
667
- // Write profile to disk, upload, etc.
668
- if (!err) {
669
- const outPath = path.resolve(`./vite-profile-${profileCount++}.cpuprofile`);
670
- fs.writeFileSync(outPath, JSON.stringify(profile));
671
- log(colors.yellow(`CPU profile written to ${colors.white(colors.dim(outPath))}`));
672
- profileSession = undefined;
673
- res();
674
- }
675
- else {
676
- rej(err);
677
- }
678
- });
665
+ if (!profileSession) return;
666
+ return new Promise((res, rej) => {
667
+ profileSession.post("Profiler.stop", (err, { profile }) => {
668
+ if (!err) {
669
+ const outPath = path.resolve(
670
+ `./vite-profile-${profileCount++}.cpuprofile`
671
+ );
672
+ fs__default.writeFileSync(outPath, JSON.stringify(profile));
673
+ log(
674
+ colors.yellow(
675
+ `CPU profile written to ${colors.white(colors.dim(outPath))}`
676
+ )
677
+ );
678
+ profileSession = void 0;
679
+ res();
680
+ } else {
681
+ rej(err);
682
+ }
679
683
  });
684
+ });
680
685
  };
681
686
  const filterDuplicateOptions = (options) => {
682
- for (const [key, value] of Object.entries(options)) {
683
- if (Array.isArray(value)) {
684
- options[key] = value[value.length - 1];
685
- }
687
+ for (const [key, value] of Object.entries(options)) {
688
+ if (Array.isArray(value)) {
689
+ options[key] = value[value.length - 1];
686
690
  }
691
+ }
687
692
  };
688
- /**
689
- * removing global flags before passing as command specific sub-configs
690
- */
691
693
  function cleanGlobalCLIOptions(options) {
692
- const ret = { ...options };
693
- delete ret['--'];
694
- delete ret.c;
695
- delete ret.config;
696
- delete ret.base;
697
- delete ret.l;
698
- delete ret.logLevel;
699
- delete ret.clearScreen;
700
- delete ret.d;
701
- delete ret.debug;
702
- delete ret.f;
703
- delete ret.filter;
704
- delete ret.m;
705
- delete ret.mode;
706
- // convert the sourcemap option to a boolean if necessary
707
- if ('sourcemap' in ret) {
708
- const sourcemap = ret.sourcemap;
709
- ret.sourcemap =
710
- sourcemap === 'true'
711
- ? true
712
- : sourcemap === 'false'
713
- ? false
714
- : ret.sourcemap;
715
- }
716
- return ret;
694
+ const ret = { ...options };
695
+ delete ret["--"];
696
+ delete ret.c;
697
+ delete ret.config;
698
+ delete ret.base;
699
+ delete ret.l;
700
+ delete ret.logLevel;
701
+ delete ret.clearScreen;
702
+ delete ret.d;
703
+ delete ret.debug;
704
+ delete ret.f;
705
+ delete ret.filter;
706
+ delete ret.m;
707
+ delete ret.mode;
708
+ if ("sourcemap" in ret) {
709
+ const sourcemap = ret.sourcemap;
710
+ ret.sourcemap = sourcemap === "true" ? true : sourcemap === "false" ? false : ret.sourcemap;
711
+ }
712
+ return ret;
717
713
  }
718
- /**
719
- * removing builder flags before passing as command specific sub-configs
720
- */
721
714
  function cleanBuilderCLIOptions(options) {
722
- const ret = { ...options };
723
- delete ret.app;
724
- return ret;
715
+ const ret = { ...options };
716
+ delete ret.app;
717
+ return ret;
725
718
  }
726
- /**
727
- * host may be a number (like 0), should convert to string
728
- */
729
719
  const convertHost = (v) => {
730
- if (typeof v === 'number') {
731
- return String(v);
732
- }
733
- return v;
720
+ if (typeof v === "number") {
721
+ return String(v);
722
+ }
723
+ return v;
734
724
  };
735
- /**
736
- * base may be a number (like 0), should convert to empty string
737
- */
738
725
  const convertBase = (v) => {
739
- if (v === 0) {
740
- return '';
741
- }
742
- return v;
726
+ if (v === 0) {
727
+ return "";
728
+ }
729
+ return v;
743
730
  };
744
- cli
745
- .option('-c, --config <file>', `[string] use specified config file`)
746
- .option('--base <path>', `[string] public base path (default: /)`, {
747
- type: [convertBase],
748
- })
749
- .option('-l, --logLevel <level>', `[string] info | warn | error | silent`)
750
- .option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
751
- .option('-d, --debug [feat]', `[string | boolean] show debug logs`)
752
- .option('-f, --filter <filter>', `[string] filter debug logs`)
753
- .option('-m, --mode <mode>', `[string] set env mode`);
754
- // dev
755
- cli
756
- .command('[root]', 'start dev server') // default command
757
- .alias('serve') // the command is called 'serve' in Vite's API
758
- .alias('dev') // alias to align with the script name
759
- .option('--host [host]', `[string] specify hostname`, { type: [convertHost] })
760
- .option('--port <port>', `[number] specify port`)
761
- .option('--open [path]', `[boolean | string] open browser on startup`)
762
- .option('--cors', `[boolean] enable CORS`)
763
- .option('--strictPort', `[boolean] exit if specified port is already in use`)
764
- .option('--force', `[boolean] force the optimizer to ignore the cache and re-bundle`)
765
- .action(async (root, options) => {
766
- filterDuplicateOptions(options);
767
- // output structure is preserved even after bundling so require()
768
- // is ok here
769
- const { createServer } = await import('./chunks/dep-DVlS5vGL.js').then(function (n) { return n.M; });
770
- try {
771
- const server = await createServer({
772
- root,
773
- base: options.base,
774
- mode: options.mode,
775
- configFile: options.config,
776
- logLevel: options.logLevel,
777
- clearScreen: options.clearScreen,
778
- optimizeDeps: { force: options.force },
779
- server: cleanGlobalCLIOptions(options),
780
- });
781
- if (!server.httpServer) {
782
- throw new Error('HTTP server not available');
783
- }
784
- await server.listen();
785
- const info = server.config.logger.info;
786
- const viteStartTime = global.__vite_start_time ?? false;
787
- const startupDurationString = viteStartTime
788
- ? colors.dim(`ready in ${colors.reset(colors.bold(Math.ceil(performance.now() - viteStartTime)))} ms`)
789
- : '';
790
- const hasExistingLogs = process.stdout.bytesWritten > 0 || process.stderr.bytesWritten > 0;
791
- info(`\n ${colors.green(`${colors.bold('VITE')} v${VERSION}`)} ${startupDurationString}\n`, {
792
- clear: !hasExistingLogs,
793
- });
794
- server.printUrls();
795
- const customShortcuts = [];
796
- if (profileSession) {
797
- customShortcuts.push({
798
- key: 'p',
799
- description: 'start/stop the profiler',
800
- async action(server) {
801
- if (profileSession) {
802
- await stopProfiler(server.config.logger.info);
803
- }
804
- else {
805
- const inspector = await import('node:inspector').then((r) => r.default);
806
- await new Promise((res) => {
807
- profileSession = new inspector.Session();
808
- profileSession.connect();
809
- profileSession.post('Profiler.enable', () => {
810
- profileSession.post('Profiler.start', () => {
811
- server.config.logger.info('Profiler started');
812
- res();
813
- });
814
- });
815
- });
816
- }
817
- },
731
+ cli.option("-c, --config <file>", `[string] use specified config file`).option("--base <path>", `[string] public base path (default: /)`, {
732
+ type: [convertBase]
733
+ }).option("-l, --logLevel <level>", `[string] info | warn | error | silent`).option("--clearScreen", `[boolean] allow/disable clear screen when logging`).option("-d, --debug [feat]", `[string | boolean] show debug logs`).option("-f, --filter <filter>", `[string] filter debug logs`).option("-m, --mode <mode>", `[string] set env mode`);
734
+ cli.command("[root]", "start dev server").alias("serve").alias("dev").option("--host [host]", `[string] specify hostname`, { type: [convertHost] }).option("--port <port>", `[number] specify port`).option("--open [path]", `[boolean | string] open browser on startup`).option("--cors", `[boolean] enable CORS`).option("--strictPort", `[boolean] exit if specified port is already in use`).option(
735
+ "--force",
736
+ `[boolean] force the optimizer to ignore the cache and re-bundle`
737
+ ).action(async (root, options) => {
738
+ filterDuplicateOptions(options);
739
+ const { createServer } = await import('./chunks/dep-BWEMV5Th.js').then(function (n) { return n.M; });
740
+ try {
741
+ const server = await createServer({
742
+ root,
743
+ base: options.base,
744
+ mode: options.mode,
745
+ configFile: options.config,
746
+ logLevel: options.logLevel,
747
+ clearScreen: options.clearScreen,
748
+ optimizeDeps: { force: options.force },
749
+ server: cleanGlobalCLIOptions(options)
750
+ });
751
+ if (!server.httpServer) {
752
+ throw new Error("HTTP server not available");
753
+ }
754
+ await server.listen();
755
+ const info = server.config.logger.info;
756
+ const viteStartTime = global.__vite_start_time ?? false;
757
+ const startupDurationString = viteStartTime ? colors.dim(
758
+ `ready in ${colors.reset(
759
+ colors.bold(Math.ceil(performance.now() - viteStartTime))
760
+ )} ms`
761
+ ) : "";
762
+ const hasExistingLogs = process.stdout.bytesWritten > 0 || process.stderr.bytesWritten > 0;
763
+ info(
764
+ `
765
+ ${colors.green(
766
+ `${colors.bold("VITE")} v${VERSION}`
767
+ )} ${startupDurationString}
768
+ `,
769
+ {
770
+ clear: !hasExistingLogs
771
+ }
772
+ );
773
+ server.printUrls();
774
+ const customShortcuts = [];
775
+ if (profileSession) {
776
+ customShortcuts.push({
777
+ key: "p",
778
+ description: "start/stop the profiler",
779
+ async action(server2) {
780
+ if (profileSession) {
781
+ await stopProfiler(server2.config.logger.info);
782
+ } else {
783
+ const inspector = await import('node:inspector').then(
784
+ (r) => r.default
785
+ );
786
+ await new Promise((res) => {
787
+ profileSession = new inspector.Session();
788
+ profileSession.connect();
789
+ profileSession.post("Profiler.enable", () => {
790
+ profileSession.post("Profiler.start", () => {
791
+ server2.config.logger.info("Profiler started");
792
+ res();
793
+ });
794
+ });
818
795
  });
796
+ }
819
797
  }
820
- server.bindCLIShortcuts({ print: true, customShortcuts });
821
- }
822
- catch (e) {
823
- const logger = createLogger(options.logLevel);
824
- logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {
825
- error: e,
826
- });
827
- stopProfiler(logger.info);
828
- process.exit(1);
798
+ });
829
799
  }
800
+ server.bindCLIShortcuts({ print: true, customShortcuts });
801
+ } catch (e) {
802
+ const logger = createLogger(options.logLevel);
803
+ logger.error(colors.red(`error when starting dev server:
804
+ ${e.stack}`), {
805
+ error: e
806
+ });
807
+ stopProfiler(logger.info);
808
+ process.exit(1);
809
+ }
830
810
  });
831
- // build
832
- cli
833
- .command('build [root]', 'build for production')
834
- .option('--target <target>', `[string] transpile target (default: 'modules')`)
835
- .option('--outDir <dir>', `[string] output directory (default: dist)`)
836
- .option('--assetsDir <dir>', `[string] directory under outDir to place assets in (default: assets)`)
837
- .option('--assetsInlineLimit <number>', `[number] static asset base64 inline threshold in bytes (default: 4096)`)
838
- .option('--ssr [entry]', `[string] build specified entry for server-side rendering`)
839
- .option('--sourcemap [output]', `[boolean | "inline" | "hidden"] output source maps for build (default: false)`)
840
- .option('--minify [minifier]', `[boolean | "terser" | "esbuild"] enable/disable minification, ` +
841
- `or specify minifier to use (default: esbuild)`)
842
- .option('--manifest [name]', `[boolean | string] emit build manifest json`)
843
- .option('--ssrManifest [name]', `[boolean | string] emit ssr manifest json`)
844
- .option('--emptyOutDir', `[boolean] force empty outDir when it's outside of root`)
845
- .option('-w, --watch', `[boolean] rebuilds when modules have changed on disk`)
846
- .option('--environment [name]', `[string] build a single environment`)
847
- .option('--app', `[boolean] same as builder.entireApp`)
848
- .action(async (root, options) => {
811
+ cli.command("build [root]", "build for production").option("--target <target>", `[string] transpile target (default: 'modules')`).option("--outDir <dir>", `[string] output directory (default: dist)`).option(
812
+ "--assetsDir <dir>",
813
+ `[string] directory under outDir to place assets in (default: assets)`
814
+ ).option(
815
+ "--assetsInlineLimit <number>",
816
+ `[number] static asset base64 inline threshold in bytes (default: 4096)`
817
+ ).option(
818
+ "--ssr [entry]",
819
+ `[string] build specified entry for server-side rendering`
820
+ ).option(
821
+ "--sourcemap [output]",
822
+ `[boolean | "inline" | "hidden"] output source maps for build (default: false)`
823
+ ).option(
824
+ "--minify [minifier]",
825
+ `[boolean | "terser" | "esbuild"] enable/disable minification, or specify minifier to use (default: esbuild)`
826
+ ).option("--manifest [name]", `[boolean | string] emit build manifest json`).option("--ssrManifest [name]", `[boolean | string] emit ssr manifest json`).option(
827
+ "--emptyOutDir",
828
+ `[boolean] force empty outDir when it's outside of root`
829
+ ).option("-w, --watch", `[boolean] rebuilds when modules have changed on disk`).option("--environment [name]", `[string] build a single environment`).option("--app", `[boolean] same as builder.entireApp`).action(
830
+ async (root, options) => {
849
831
  filterDuplicateOptions(options);
850
- const { createBuilder, buildEnvironment } = await import('./chunks/dep-DVlS5vGL.js').then(function (n) { return n.N; });
851
- const buildOptions = cleanGlobalCLIOptions(cleanBuilderCLIOptions(options));
832
+ const { createBuilder, buildEnvironment } = await import('./chunks/dep-BWEMV5Th.js').then(function (n) { return n.N; });
833
+ const buildOptions = cleanGlobalCLIOptions(
834
+ cleanBuilderCLIOptions(options)
835
+ );
852
836
  const config = {
853
- root,
854
- base: options.base,
855
- mode: options.mode,
856
- configFile: options.config,
857
- logLevel: options.logLevel,
858
- clearScreen: options.clearScreen,
859
- build: buildOptions,
837
+ root,
838
+ base: options.base,
839
+ mode: options.mode,
840
+ configFile: options.config,
841
+ logLevel: options.logLevel,
842
+ clearScreen: options.clearScreen,
843
+ build: buildOptions
860
844
  };
861
845
  try {
862
- const builder = await createBuilder(config);
863
- // TODO: Backward compatibility with lib and single environment build
864
- // Ideally we would move to only building the entire app with this command
865
- if (builder.config.build.lib) {
866
- await buildEnvironment(builder.config, builder.environments.client, builder.config.build.lib);
867
- }
868
- else if (builder.config.builder.entireApp || options.app) {
869
- await builder.buildApp();
870
- }
871
- else {
872
- const ssr = !!builder.config.build.ssr;
873
- const environment = builder.environments[ssr ? 'ssr' : 'client'];
874
- await builder.build(environment);
875
- }
876
- }
877
- catch (e) {
878
- createLogger(options.logLevel).error(colors.red(`error during build:\n${e.stack}`), { error: e });
879
- process.exit(1);
880
- }
881
- finally {
882
- stopProfiler((message) => createLogger(options.logLevel).info(message));
883
- }
884
- });
885
- // optimize
886
- cli
887
- .command('optimize [root]', 'pre-bundle dependencies')
888
- .option('--force', `[boolean] force the optimizer to ignore the cache and re-bundle`)
889
- .action(async (root, options) => {
846
+ const builder = await createBuilder(config);
847
+ if (builder.config.build.lib) {
848
+ await buildEnvironment(
849
+ builder.config,
850
+ builder.environments.client,
851
+ builder.config.build.lib
852
+ );
853
+ } else if (builder.config.builder.entireApp || options.app) {
854
+ await builder.buildApp();
855
+ } else {
856
+ const ssr = !!builder.config.build.ssr;
857
+ const environment = builder.environments[ssr ? "ssr" : "client"];
858
+ await builder.build(environment);
859
+ }
860
+ } catch (e) {
861
+ createLogger(options.logLevel).error(
862
+ colors.red(`error during build:
863
+ ${e.stack}`),
864
+ { error: e }
865
+ );
866
+ process.exit(1);
867
+ } finally {
868
+ stopProfiler((message) => createLogger(options.logLevel).info(message));
869
+ }
870
+ }
871
+ );
872
+ cli.command("optimize [root]", "pre-bundle dependencies").option(
873
+ "--force",
874
+ `[boolean] force the optimizer to ignore the cache and re-bundle`
875
+ ).action(
876
+ async (root, options) => {
890
877
  filterDuplicateOptions(options);
891
- const { optimizeDeps } = await import('./chunks/dep-DVlS5vGL.js').then(function (n) { return n.L; });
878
+ const { optimizeDeps } = await import('./chunks/dep-BWEMV5Th.js').then(function (n) { return n.L; });
892
879
  try {
893
- const config = await resolveConfig({
894
- root,
895
- base: options.base,
896
- configFile: options.config,
897
- logLevel: options.logLevel,
898
- mode: options.mode,
899
- }, 'serve');
900
- await optimizeDeps(config, options.force, true);
901
- }
902
- catch (e) {
903
- createLogger(options.logLevel).error(colors.red(`error when optimizing deps:\n${e.stack}`), { error: e });
904
- process.exit(1);
905
- }
906
- });
907
- // preview
908
- cli
909
- .command('preview [root]', 'locally preview production build')
910
- .option('--host [host]', `[string] specify hostname`, { type: [convertHost] })
911
- .option('--port <port>', `[number] specify port`)
912
- .option('--strictPort', `[boolean] exit if specified port is already in use`)
913
- .option('--open [path]', `[boolean | string] open browser on startup`)
914
- .option('--outDir <dir>', `[string] output directory (default: dist)`)
915
- .action(async (root, options) => {
880
+ const config = await resolveConfig(
881
+ {
882
+ root,
883
+ base: options.base,
884
+ configFile: options.config,
885
+ logLevel: options.logLevel,
886
+ mode: options.mode
887
+ },
888
+ "serve"
889
+ );
890
+ await optimizeDeps(config, options.force, true);
891
+ } catch (e) {
892
+ createLogger(options.logLevel).error(
893
+ colors.red(`error when optimizing deps:
894
+ ${e.stack}`),
895
+ { error: e }
896
+ );
897
+ process.exit(1);
898
+ }
899
+ }
900
+ );
901
+ cli.command("preview [root]", "locally preview production build").option("--host [host]", `[string] specify hostname`, { type: [convertHost] }).option("--port <port>", `[number] specify port`).option("--strictPort", `[boolean] exit if specified port is already in use`).option("--open [path]", `[boolean | string] open browser on startup`).option("--outDir <dir>", `[string] output directory (default: dist)`).action(
902
+ async (root, options) => {
916
903
  filterDuplicateOptions(options);
917
- const { preview } = await import('./chunks/dep-DVlS5vGL.js').then(function (n) { return n.O; });
904
+ const { preview } = await import('./chunks/dep-BWEMV5Th.js').then(function (n) { return n.O; });
918
905
  try {
919
- const server = await preview({
920
- root,
921
- base: options.base,
922
- configFile: options.config,
923
- logLevel: options.logLevel,
924
- mode: options.mode,
925
- build: {
926
- outDir: options.outDir,
927
- },
928
- preview: {
929
- port: options.port,
930
- strictPort: options.strictPort,
931
- host: options.host,
932
- open: options.open,
933
- },
934
- });
935
- server.printUrls();
936
- server.bindCLIShortcuts({ print: true });
937
- }
938
- catch (e) {
939
- createLogger(options.logLevel).error(colors.red(`error when starting preview server:\n${e.stack}`), { error: e });
940
- process.exit(1);
941
- }
942
- finally {
943
- stopProfiler((message) => createLogger(options.logLevel).info(message));
944
- }
945
- });
906
+ const server = await preview({
907
+ root,
908
+ base: options.base,
909
+ configFile: options.config,
910
+ logLevel: options.logLevel,
911
+ mode: options.mode,
912
+ build: {
913
+ outDir: options.outDir
914
+ },
915
+ preview: {
916
+ port: options.port,
917
+ strictPort: options.strictPort,
918
+ host: options.host,
919
+ open: options.open
920
+ }
921
+ });
922
+ server.printUrls();
923
+ server.bindCLIShortcuts({ print: true });
924
+ } catch (e) {
925
+ createLogger(options.logLevel).error(
926
+ colors.red(`error when starting preview server:
927
+ ${e.stack}`),
928
+ { error: e }
929
+ );
930
+ process.exit(1);
931
+ } finally {
932
+ stopProfiler((message) => createLogger(options.logLevel).info(message));
933
+ }
934
+ }
935
+ );
946
936
  cli.help();
947
937
  cli.version(VERSION);
948
938
  cli.parse();