vite-node 0.26.2 → 0.27.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/cli.cjs CHANGED
@@ -1,14 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var events = require('events');
4
- var picocolors = require('./chunk-picocolors.cjs');
3
+ var cac = require('cac');
4
+ var c = require('picocolors');
5
5
  var vite = require('vite');
6
6
  var server = require('./server.cjs');
7
7
  var client = require('./client.cjs');
8
8
  var utils = require('./utils.cjs');
9
- var hmr = require('./chunk-hmr.cjs');
10
9
  var sourceMap = require('./source-map.cjs');
11
- require('tty');
10
+ var hmr = require('./chunk-hmr.cjs');
12
11
  require('perf_hooks');
13
12
  require('pathe');
14
13
  require('debug');
@@ -21,629 +20,21 @@ require('path');
21
20
  require('vm');
22
21
  require('node:events');
23
22
 
24
- function toArr(any) {
25
- return any == null ? [] : Array.isArray(any) ? any : [any];
26
- }
27
-
28
- function toVal(out, key, val, opts) {
29
- var x, old=out[key], nxt=(
30
- !!~opts.string.indexOf(key) ? (val == null || val === true ? '' : String(val))
31
- : typeof val === 'boolean' ? val
32
- : !!~opts.boolean.indexOf(key) ? (val === 'false' ? false : val === 'true' || (out._.push((x = +val,x * 0 === 0) ? x : val),!!val))
33
- : (x = +val,x * 0 === 0) ? x : val
34
- );
35
- out[key] = old == null ? nxt : (Array.isArray(old) ? old.concat(nxt) : [old, nxt]);
36
- }
37
-
38
- function mri2 (args, opts) {
39
- args = args || [];
40
- opts = opts || {};
41
-
42
- var k, arr, arg, name, val, out={ _:[] };
43
- var i=0, j=0, idx=0, len=args.length;
44
-
45
- const alibi = opts.alias !== void 0;
46
- const strict = opts.unknown !== void 0;
47
- const defaults = opts.default !== void 0;
48
-
49
- opts.alias = opts.alias || {};
50
- opts.string = toArr(opts.string);
51
- opts.boolean = toArr(opts.boolean);
52
-
53
- if (alibi) {
54
- for (k in opts.alias) {
55
- arr = opts.alias[k] = toArr(opts.alias[k]);
56
- for (i=0; i < arr.length; i++) {
57
- (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
58
- }
59
- }
60
- }
61
-
62
- for (i=opts.boolean.length; i-- > 0;) {
63
- arr = opts.alias[opts.boolean[i]] || [];
64
- for (j=arr.length; j-- > 0;) opts.boolean.push(arr[j]);
65
- }
66
-
67
- for (i=opts.string.length; i-- > 0;) {
68
- arr = opts.alias[opts.string[i]] || [];
69
- for (j=arr.length; j-- > 0;) opts.string.push(arr[j]);
70
- }
71
-
72
- if (defaults) {
73
- for (k in opts.default) {
74
- name = typeof opts.default[k];
75
- arr = opts.alias[k] = opts.alias[k] || [];
76
- if (opts[name] !== void 0) {
77
- opts[name].push(k);
78
- for (i=0; i < arr.length; i++) {
79
- opts[name].push(arr[i]);
80
- }
81
- }
82
- }
83
- }
84
-
85
- const keys = strict ? Object.keys(opts.alias) : [];
86
-
87
- for (i=0; i < len; i++) {
88
- arg = args[i];
89
-
90
- if (arg === '--') {
91
- out._ = out._.concat(args.slice(++i));
92
- break;
93
- }
94
-
95
- for (j=0; j < arg.length; j++) {
96
- if (arg.charCodeAt(j) !== 45) break; // "-"
97
- }
98
-
99
- if (j === 0) {
100
- out._.push(arg);
101
- } else if (arg.substring(j, j + 3) === 'no-') {
102
- name = arg.substring(j + 3);
103
- if (strict && !~keys.indexOf(name)) {
104
- return opts.unknown(arg);
105
- }
106
- out[name] = false;
107
- } else {
108
- for (idx=j+1; idx < arg.length; idx++) {
109
- if (arg.charCodeAt(idx) === 61) break; // "="
110
- }
111
-
112
- name = arg.substring(j, idx);
113
- val = arg.substring(++idx) || (i+1 === len || (''+args[i+1]).charCodeAt(0) === 45 || args[++i]);
114
- arr = (j === 2 ? [name] : name);
115
-
116
- for (idx=0; idx < arr.length; idx++) {
117
- name = arr[idx];
118
- if (strict && !~keys.indexOf(name)) return opts.unknown('-'.repeat(j) + name);
119
- toVal(out, name, (idx + 1 < arr.length) || val, opts);
120
- }
121
- }
122
- }
123
-
124
- if (defaults) {
125
- for (k in opts.default) {
126
- if (out[k] === void 0) {
127
- out[k] = opts.default[k];
128
- }
129
- }
130
- }
131
-
132
- if (alibi) {
133
- for (k in out) {
134
- arr = opts.alias[k] || [];
135
- while (arr.length > 0) {
136
- out[arr.shift()] = out[k];
137
- }
138
- }
139
- }
140
-
141
- return out;
142
- }
143
-
144
- const removeBrackets = (v) => v.replace(/[<[].+/, "").trim();
145
- const findAllBrackets = (v) => {
146
- const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
147
- const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
148
- const res = [];
149
- const parse = (match) => {
150
- let variadic = false;
151
- let value = match[1];
152
- if (value.startsWith("...")) {
153
- value = value.slice(3);
154
- variadic = true;
155
- }
156
- return {
157
- required: match[0].startsWith("<"),
158
- value,
159
- variadic
160
- };
161
- };
162
- let angledMatch;
163
- while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) {
164
- res.push(parse(angledMatch));
165
- }
166
- let squareMatch;
167
- while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) {
168
- res.push(parse(squareMatch));
169
- }
170
- return res;
171
- };
172
- const getMriOptions = (options) => {
173
- const result = {alias: {}, boolean: []};
174
- for (const [index, option] of options.entries()) {
175
- if (option.names.length > 1) {
176
- result.alias[option.names[0]] = option.names.slice(1);
177
- }
178
- if (option.isBoolean) {
179
- if (option.negated) {
180
- const hasStringTypeOption = options.some((o, i) => {
181
- return i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === "boolean";
182
- });
183
- if (!hasStringTypeOption) {
184
- result.boolean.push(option.names[0]);
185
- }
186
- } else {
187
- result.boolean.push(option.names[0]);
188
- }
189
- }
190
- }
191
- return result;
192
- };
193
- const findLongest = (arr) => {
194
- return arr.sort((a, b) => {
195
- return a.length > b.length ? -1 : 1;
196
- })[0];
197
- };
198
- const padRight = (str, length) => {
199
- return str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
200
- };
201
- const camelcase = (input) => {
202
- return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
203
- return p1 + p2.toUpperCase();
204
- });
205
- };
206
- const setDotProp = (obj, keys, val) => {
207
- let i = 0;
208
- let length = keys.length;
209
- let t = obj;
210
- let x;
211
- for (; i < length; ++i) {
212
- x = t[keys[i]];
213
- t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf(".") || !(+keys[i + 1] > -1) ? {} : [];
214
- }
215
- };
216
- const setByType = (obj, transforms) => {
217
- for (const key of Object.keys(transforms)) {
218
- const transform = transforms[key];
219
- if (transform.shouldTransform) {
220
- obj[key] = Array.prototype.concat.call([], obj[key]);
221
- if (typeof transform.transformFunction === "function") {
222
- obj[key] = obj[key].map(transform.transformFunction);
223
- }
224
- }
225
- }
226
- };
227
- const getFileName = (input) => {
228
- const m = /([^\\\/]+)$/.exec(input);
229
- return m ? m[1] : "";
230
- };
231
- const camelcaseOptionName = (name) => {
232
- return name.split(".").map((v, i) => {
233
- return i === 0 ? camelcase(v) : v;
234
- }).join(".");
235
- };
236
- class CACError extends Error {
237
- constructor(message) {
238
- super(message);
239
- this.name = this.constructor.name;
240
- if (typeof Error.captureStackTrace === "function") {
241
- Error.captureStackTrace(this, this.constructor);
242
- } else {
243
- this.stack = new Error(message).stack;
244
- }
245
- }
246
- }
247
-
248
- class Option {
249
- constructor(rawName, description, config) {
250
- this.rawName = rawName;
251
- this.description = description;
252
- this.config = Object.assign({}, config);
253
- rawName = rawName.replace(/\.\*/g, "");
254
- this.negated = false;
255
- this.names = removeBrackets(rawName).split(",").map((v) => {
256
- let name = v.trim().replace(/^-{1,2}/, "");
257
- if (name.startsWith("no-")) {
258
- this.negated = true;
259
- name = name.replace(/^no-/, "");
260
- }
261
- return camelcaseOptionName(name);
262
- }).sort((a, b) => a.length > b.length ? 1 : -1);
263
- this.name = this.names[this.names.length - 1];
264
- if (this.negated && this.config.default == null) {
265
- this.config.default = true;
266
- }
267
- if (rawName.includes("<")) {
268
- this.required = true;
269
- } else if (rawName.includes("[")) {
270
- this.required = false;
271
- } else {
272
- this.isBoolean = true;
273
- }
274
- }
275
- }
276
-
277
- const processArgs = process.argv;
278
- const platformInfo = `${process.platform}-${process.arch} node-${process.version}`;
279
-
280
- class Command {
281
- constructor(rawName, description, config = {}, cli) {
282
- this.rawName = rawName;
283
- this.description = description;
284
- this.config = config;
285
- this.cli = cli;
286
- this.options = [];
287
- this.aliasNames = [];
288
- this.name = removeBrackets(rawName);
289
- this.args = findAllBrackets(rawName);
290
- this.examples = [];
291
- }
292
- usage(text) {
293
- this.usageText = text;
294
- return this;
295
- }
296
- allowUnknownOptions() {
297
- this.config.allowUnknownOptions = true;
298
- return this;
299
- }
300
- ignoreOptionDefaultValue() {
301
- this.config.ignoreOptionDefaultValue = true;
302
- return this;
303
- }
304
- version(version, customFlags = "-v, --version") {
305
- this.versionNumber = version;
306
- this.option(customFlags, "Display version number");
307
- return this;
308
- }
309
- example(example) {
310
- this.examples.push(example);
311
- return this;
312
- }
313
- option(rawName, description, config) {
314
- const option = new Option(rawName, description, config);
315
- this.options.push(option);
316
- return this;
317
- }
318
- alias(name) {
319
- this.aliasNames.push(name);
320
- return this;
321
- }
322
- action(callback) {
323
- this.commandAction = callback;
324
- return this;
325
- }
326
- isMatched(name) {
327
- return this.name === name || this.aliasNames.includes(name);
328
- }
329
- get isDefaultCommand() {
330
- return this.name === "" || this.aliasNames.includes("!");
331
- }
332
- get isGlobalCommand() {
333
- return this instanceof GlobalCommand;
334
- }
335
- hasOption(name) {
336
- name = name.split(".")[0];
337
- return this.options.find((option) => {
338
- return option.names.includes(name);
339
- });
340
- }
341
- outputHelp() {
342
- const {name, commands} = this.cli;
343
- const {
344
- versionNumber,
345
- options: globalOptions,
346
- helpCallback
347
- } = this.cli.globalCommand;
348
- let sections = [
349
- {
350
- body: `${name}${versionNumber ? `/${versionNumber}` : ""}`
351
- }
352
- ];
353
- sections.push({
354
- title: "Usage",
355
- body: ` $ ${name} ${this.usageText || this.rawName}`
356
- });
357
- const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
358
- if (showCommands) {
359
- const longestCommandName = findLongest(commands.map((command) => command.rawName));
360
- sections.push({
361
- title: "Commands",
362
- body: commands.map((command) => {
363
- return ` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`;
364
- }).join("\n")
365
- });
366
- sections.push({
367
- title: `For more info, run any command with the \`--help\` flag`,
368
- body: commands.map((command) => ` $ ${name}${command.name === "" ? "" : ` ${command.name}`} --help`).join("\n")
369
- });
370
- }
371
- let options = this.isGlobalCommand ? globalOptions : [...this.options, ...globalOptions || []];
372
- if (!this.isGlobalCommand && !this.isDefaultCommand) {
373
- options = options.filter((option) => option.name !== "version");
374
- }
375
- if (options.length > 0) {
376
- const longestOptionName = findLongest(options.map((option) => option.rawName));
377
- sections.push({
378
- title: "Options",
379
- body: options.map((option) => {
380
- return ` ${padRight(option.rawName, longestOptionName.length)} ${option.description} ${option.config.default === void 0 ? "" : `(default: ${option.config.default})`}`;
381
- }).join("\n")
382
- });
383
- }
384
- if (this.examples.length > 0) {
385
- sections.push({
386
- title: "Examples",
387
- body: this.examples.map((example) => {
388
- if (typeof example === "function") {
389
- return example(name);
390
- }
391
- return example;
392
- }).join("\n")
393
- });
394
- }
395
- if (helpCallback) {
396
- sections = helpCallback(sections) || sections;
397
- }
398
- console.log(sections.map((section) => {
399
- return section.title ? `${section.title}:
400
- ${section.body}` : section.body;
401
- }).join("\n\n"));
402
- }
403
- outputVersion() {
404
- const {name} = this.cli;
405
- const {versionNumber} = this.cli.globalCommand;
406
- if (versionNumber) {
407
- console.log(`${name}/${versionNumber} ${platformInfo}`);
408
- }
409
- }
410
- checkRequiredArgs() {
411
- const minimalArgsCount = this.args.filter((arg) => arg.required).length;
412
- if (this.cli.args.length < minimalArgsCount) {
413
- throw new CACError(`missing required args for command \`${this.rawName}\``);
414
- }
415
- }
416
- checkUnknownOptions() {
417
- const {options, globalCommand} = this.cli;
418
- if (!this.config.allowUnknownOptions) {
419
- for (const name of Object.keys(options)) {
420
- if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) {
421
- throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
422
- }
423
- }
424
- }
425
- }
426
- checkOptionValue() {
427
- const {options: parsedOptions, globalCommand} = this.cli;
428
- const options = [...globalCommand.options, ...this.options];
429
- for (const option of options) {
430
- const value = parsedOptions[option.name.split(".")[0]];
431
- if (option.required) {
432
- const hasNegated = options.some((o) => o.negated && o.names.includes(option.name));
433
- if (value === true || value === false && !hasNegated) {
434
- throw new CACError(`option \`${option.rawName}\` value is missing`);
435
- }
436
- }
437
- }
438
- }
439
- }
440
- class GlobalCommand extends Command {
441
- constructor(cli) {
442
- super("@@global@@", "", {}, cli);
443
- }
444
- }
445
-
446
- var __assign = Object.assign;
447
- class CAC extends events.EventEmitter {
448
- constructor(name = "") {
449
- super();
450
- this.name = name;
451
- this.commands = [];
452
- this.rawArgs = [];
453
- this.args = [];
454
- this.options = {};
455
- this.globalCommand = new GlobalCommand(this);
456
- this.globalCommand.usage("<command> [options]");
457
- }
458
- usage(text) {
459
- this.globalCommand.usage(text);
460
- return this;
461
- }
462
- command(rawName, description, config) {
463
- const command = new Command(rawName, description || "", config, this);
464
- command.globalCommand = this.globalCommand;
465
- this.commands.push(command);
466
- return command;
467
- }
468
- option(rawName, description, config) {
469
- this.globalCommand.option(rawName, description, config);
470
- return this;
471
- }
472
- help(callback) {
473
- this.globalCommand.option("-h, --help", "Display this message");
474
- this.globalCommand.helpCallback = callback;
475
- this.showHelpOnExit = true;
476
- return this;
477
- }
478
- version(version, customFlags = "-v, --version") {
479
- this.globalCommand.version(version, customFlags);
480
- this.showVersionOnExit = true;
481
- return this;
482
- }
483
- example(example) {
484
- this.globalCommand.example(example);
485
- return this;
486
- }
487
- outputHelp() {
488
- if (this.matchedCommand) {
489
- this.matchedCommand.outputHelp();
490
- } else {
491
- this.globalCommand.outputHelp();
492
- }
493
- }
494
- outputVersion() {
495
- this.globalCommand.outputVersion();
496
- }
497
- setParsedInfo({args, options}, matchedCommand, matchedCommandName) {
498
- this.args = args;
499
- this.options = options;
500
- if (matchedCommand) {
501
- this.matchedCommand = matchedCommand;
502
- }
503
- if (matchedCommandName) {
504
- this.matchedCommandName = matchedCommandName;
505
- }
506
- return this;
507
- }
508
- unsetMatchedCommand() {
509
- this.matchedCommand = void 0;
510
- this.matchedCommandName = void 0;
511
- }
512
- parse(argv = processArgs, {
513
- run = true
514
- } = {}) {
515
- this.rawArgs = argv;
516
- if (!this.name) {
517
- this.name = argv[1] ? getFileName(argv[1]) : "cli";
518
- }
519
- let shouldParse = true;
520
- for (const command of this.commands) {
521
- const parsed = this.mri(argv.slice(2), command);
522
- const commandName = parsed.args[0];
523
- if (command.isMatched(commandName)) {
524
- shouldParse = false;
525
- const parsedInfo = __assign(__assign({}, parsed), {
526
- args: parsed.args.slice(1)
527
- });
528
- this.setParsedInfo(parsedInfo, command, commandName);
529
- this.emit(`command:${commandName}`, command);
530
- }
531
- }
532
- if (shouldParse) {
533
- for (const command of this.commands) {
534
- if (command.name === "") {
535
- shouldParse = false;
536
- const parsed = this.mri(argv.slice(2), command);
537
- this.setParsedInfo(parsed, command);
538
- this.emit(`command:!`, command);
539
- }
540
- }
541
- }
542
- if (shouldParse) {
543
- const parsed = this.mri(argv.slice(2));
544
- this.setParsedInfo(parsed);
545
- }
546
- if (this.options.help && this.showHelpOnExit) {
547
- this.outputHelp();
548
- run = false;
549
- this.unsetMatchedCommand();
550
- }
551
- if (this.options.version && this.showVersionOnExit && this.matchedCommandName == null) {
552
- this.outputVersion();
553
- run = false;
554
- this.unsetMatchedCommand();
555
- }
556
- const parsedArgv = {args: this.args, options: this.options};
557
- if (run) {
558
- this.runMatchedCommand();
559
- }
560
- if (!this.matchedCommand && this.args[0]) {
561
- this.emit("command:*");
562
- }
563
- return parsedArgv;
564
- }
565
- mri(argv, command) {
566
- const cliOptions = [
567
- ...this.globalCommand.options,
568
- ...command ? command.options : []
569
- ];
570
- const mriOptions = getMriOptions(cliOptions);
571
- let argsAfterDoubleDashes = [];
572
- const doubleDashesIndex = argv.indexOf("--");
573
- if (doubleDashesIndex > -1) {
574
- argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
575
- argv = argv.slice(0, doubleDashesIndex);
576
- }
577
- let parsed = mri2(argv, mriOptions);
578
- parsed = Object.keys(parsed).reduce((res, name) => {
579
- return __assign(__assign({}, res), {
580
- [camelcaseOptionName(name)]: parsed[name]
581
- });
582
- }, {_: []});
583
- const args = parsed._;
584
- const options = {
585
- "--": argsAfterDoubleDashes
586
- };
587
- const ignoreDefault = command && command.config.ignoreOptionDefaultValue ? command.config.ignoreOptionDefaultValue : this.globalCommand.config.ignoreOptionDefaultValue;
588
- let transforms = Object.create(null);
589
- for (const cliOption of cliOptions) {
590
- if (!ignoreDefault && cliOption.config.default !== void 0) {
591
- for (const name of cliOption.names) {
592
- options[name] = cliOption.config.default;
593
- }
594
- }
595
- if (Array.isArray(cliOption.config.type)) {
596
- if (transforms[cliOption.name] === void 0) {
597
- transforms[cliOption.name] = Object.create(null);
598
- transforms[cliOption.name]["shouldTransform"] = true;
599
- transforms[cliOption.name]["transformFunction"] = cliOption.config.type[0];
600
- }
601
- }
602
- }
603
- for (const key of Object.keys(parsed)) {
604
- if (key !== "_") {
605
- const keys = key.split(".");
606
- setDotProp(options, keys, parsed[key]);
607
- setByType(options, transforms);
608
- }
609
- }
610
- return {
611
- args,
612
- options
613
- };
614
- }
615
- runMatchedCommand() {
616
- const {args, options, matchedCommand: command} = this;
617
- if (!command || !command.commandAction)
618
- return;
619
- command.checkUnknownOptions();
620
- command.checkOptionValue();
621
- command.checkRequiredArgs();
622
- const actionArgs = [];
623
- command.args.forEach((arg, index) => {
624
- if (arg.variadic) {
625
- actionArgs.push(args.slice(index));
626
- } else {
627
- actionArgs.push(args[index]);
628
- }
629
- });
630
- actionArgs.push(options);
631
- return command.commandAction.apply(this, actionArgs);
632
- }
633
- }
23
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
634
24
 
635
- const cac = (name = "") => new CAC(name);
25
+ var cac__default = /*#__PURE__*/_interopDefaultLegacy(cac);
26
+ var c__default = /*#__PURE__*/_interopDefaultLegacy(c);
636
27
 
637
- var version = "0.26.2";
28
+ var version = "0.27.0";
638
29
 
639
- const cli = cac("vite-node");
30
+ const cli = cac__default["default"]("vite-node");
640
31
  cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--options <options>", "Use specified Vite server options").help();
641
32
  cli.command("[...files]").action(run);
642
33
  cli.parse();
643
34
  async function run(files, options = {}) {
644
35
  var _a;
645
36
  if (!files.length) {
646
- console.error(picocolors.picocolors.exports.red("No files specified."));
37
+ console.error(c__default["default"].red("No files specified."));
647
38
  cli.outputHelp();
648
39
  process.exit(1);
649
40
  }
@@ -685,7 +76,7 @@ async function run(files, options = {}) {
685
76
  });
686
77
  if (options.watch) {
687
78
  process.on("uncaughtException", (err) => {
688
- console.error(picocolors.picocolors.exports.red("[vite-node] Failed to execute file: \n"), err);
79
+ console.error(c__default["default"].red("[vite-node] Failed to execute file: \n"), err);
689
80
  });
690
81
  }
691
82
  }
package/dist/cli.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { e as ViteNodeServerOptions } from './types-6a15e0b9.js';
1
+ import { e as ViteNodeServerOptions } from './types-63205a44.js';
2
2
 
3
3
  interface CliOptions {
4
4
  root?: string;