vite 6.0.0-alpha.8 → 6.0.0-beta.0

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