vite-node 2.1.3 → 2.1.5

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/README.md CHANGED
@@ -83,8 +83,8 @@ In Vite Node, the server and runner (client) are separated, so you can integrate
83
83
 
84
84
  ```ts
85
85
  import { createServer } from 'vite'
86
- import { ViteNodeServer } from 'vite-node/server'
87
86
  import { ViteNodeRunner } from 'vite-node/client'
87
+ import { ViteNodeServer } from 'vite-node/server'
88
88
  import { installSourcemapsSupport } from 'vite-node/source-map'
89
89
 
90
90
  // create vite server
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var node_events = require('node:events');
4
- var browser = require('./chunk-browser.cjs');
5
4
  var createDebug = require('debug');
5
+ var browser = require('./chunk-browser.cjs');
6
6
  var utils = require('./utils.cjs');
7
7
 
8
8
  function createHmrEmitter() {
@@ -127,7 +127,7 @@ async function fetchUpdate(runner, { path, acceptedPath }) {
127
127
  };
128
128
  }
129
129
  function warnFailedFetch(err, path) {
130
- if (!err.message.match("fetch")) {
130
+ if (!(err instanceof Error) || !err.message.match("fetch")) {
131
131
  console.error(err);
132
132
  }
133
133
  console.error(
@@ -1,6 +1,6 @@
1
1
  import { EventEmitter } from 'node:events';
2
- import { f } from './chunk-browser.mjs';
3
2
  import createDebug from 'debug';
3
+ import { f } from './chunk-browser.mjs';
4
4
  import { normalizeRequestId } from './utils.mjs';
5
5
 
6
6
  function createHmrEmitter() {
@@ -125,7 +125,7 @@ async function fetchUpdate(runner, { path, acceptedPath }) {
125
125
  };
126
126
  }
127
127
  function warnFailedFetch(err, path) {
128
- if (!err.message.match("fetch")) {
128
+ if (!(err instanceof Error) || !err.message.match("fetch")) {
129
129
  console.error(err);
130
130
  }
131
131
  console.error(
package/dist/cli.cjs CHANGED
@@ -4,23 +4,24 @@ var path = require('node:path');
4
4
  var cac = require('cac');
5
5
  var browser = require('./chunk-browser.cjs');
6
6
  var vite = require('vite');
7
- var server = require('./server.cjs');
8
7
  var client = require('./client.cjs');
9
- var utils = require('./utils.cjs');
8
+ var server = require('./server.cjs');
10
9
  var sourceMap = require('./source-map.cjs');
10
+ var utils = require('./utils.cjs');
11
11
  var hmr = require('./chunk-hmr.cjs');
12
- require('node:perf_hooks');
13
- require('node:fs');
14
- require('node:assert');
12
+ require('node:module');
15
13
  require('node:url');
16
- require('pathe');
14
+ require('node:vm');
17
15
  require('debug');
16
+ require('pathe');
17
+ require('node:fs');
18
+ require('node:assert');
19
+ require('node:perf_hooks');
20
+ require('es-module-lexer');
18
21
  require('./constants.cjs');
19
- require('node:module');
20
- require('node:vm');
21
22
  require('node:events');
22
23
 
23
- var version = "2.1.3";
24
+ var version = "2.1.5";
24
25
 
25
26
  const cli = cac("vite-node");
26
27
  cli.option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").option("-v, --version", "Output the version number").option("-h, --help", "Display help for command");
package/dist/cli.mjs CHANGED
@@ -2,23 +2,24 @@ import { resolve } from 'node:path';
2
2
  import cac from 'cac';
3
3
  import { f } from './chunk-browser.mjs';
4
4
  import { createServer, loadEnv } from 'vite';
5
- import { ViteNodeServer } from './server.mjs';
6
5
  import { ViteNodeRunner } from './client.mjs';
7
- import { toArray } from './utils.mjs';
6
+ import { ViteNodeServer } from './server.mjs';
8
7
  import { installSourcemapsSupport } from './source-map.mjs';
8
+ import { toArray } from './utils.mjs';
9
9
  import { v as viteNodeHmrPlugin, a as createHotContext, h as handleMessage } from './chunk-hmr.mjs';
10
- import 'node:perf_hooks';
11
- import 'node:fs';
12
- import 'node:assert';
10
+ import 'node:module';
13
11
  import 'node:url';
14
- import 'pathe';
12
+ import 'node:vm';
15
13
  import 'debug';
14
+ import 'pathe';
15
+ import 'node:fs';
16
+ import 'node:assert';
17
+ import 'node:perf_hooks';
18
+ import 'es-module-lexer';
16
19
  import './constants.mjs';
17
- import 'node:module';
18
- import 'node:vm';
19
20
  import 'node:events';
20
21
 
21
- var version = "2.1.3";
22
+ var version = "2.1.5";
22
23
 
23
24
  const cli = cac("vite-node");
24
25
  cli.option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").option("-v, --version", "Output the version number").option("-h, --help", "Display help for command");
package/dist/client.cjs CHANGED
@@ -4,10 +4,10 @@ var node_module = require('node:module');
4
4
  var path = require('node:path');
5
5
  var node_url = require('node:url');
6
6
  var vm = require('node:vm');
7
- var pathe = require('pathe');
8
7
  var createDebug = require('debug');
9
- var utils = require('./utils.cjs');
10
8
  var sourceMap = require('./source-map.cjs');
9
+ var utils = require('./utils.cjs');
10
+ require('pathe');
11
11
  require('node:fs');
12
12
 
13
13
  const { setTimeout, clearTimeout } = globalThis;
@@ -167,7 +167,7 @@ class ViteNodeRunner {
167
167
  */
168
168
  moduleCache;
169
169
  async executeFile(file) {
170
- const url = `/@fs/${utils.slash(pathe.resolve(file))}`;
170
+ const url = `/@fs/${utils.slash(path.resolve(file))}`;
171
171
  return await this.cachedRequest(url, url, []);
172
172
  }
173
173
  async executeId(rawId) {
package/dist/client.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { e as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, a as ViteNodeRunner } from './index-z0R8hVRu.js';
2
1
  import './trace-mapping.d-DLVdEqOp.js';
2
+ export { e as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, a as ViteNodeRunner } from './index-z0R8hVRu.js';
package/dist/client.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import { createRequire } from 'node:module';
2
- import { dirname } from 'node:path';
2
+ import { resolve, dirname } from 'node:path';
3
3
  import { pathToFileURL, fileURLToPath } from 'node:url';
4
4
  import vm from 'node:vm';
5
- import { resolve } from 'pathe';
6
5
  import createDebug from 'debug';
7
- import { createImportMetaEnvProxy, normalizeModuleId, slash, isInternalRequest, isNodeBuiltin, normalizeRequestId, toFilePath, cleanUrl, isPrimitive } from './utils.mjs';
8
6
  import { extractSourceMap } from './source-map.mjs';
7
+ import { createImportMetaEnvProxy, normalizeModuleId, slash, isInternalRequest, isNodeBuiltin, normalizeRequestId, toFilePath, cleanUrl, isPrimitive } from './utils.mjs';
8
+ import 'pathe';
9
9
  import 'node:fs';
10
10
 
11
11
  const { setTimeout, clearTimeout } = globalThis;
package/dist/hmr.cjs CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  var hmr = require('./chunk-hmr.cjs');
4
4
  require('node:events');
5
- require('./chunk-browser.cjs');
6
5
  require('debug');
6
+ require('./chunk-browser.cjs');
7
7
  require('./utils.cjs');
8
- require('node:url');
9
- require('node:module');
10
8
  require('node:fs');
9
+ require('node:module');
10
+ require('node:url');
11
11
  require('pathe');
12
12
 
13
13
 
package/dist/hmr.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { EventEmitter } from 'node:events';
2
1
  import { HMRPayload, Plugin } from 'vite';
2
+ import { EventEmitter } from 'node:events';
3
3
  import { C as CustomEventMap, a as ViteNodeRunner, H as HMRPayload$1, b as HotContext } from './index-z0R8hVRu.js';
4
4
  import './trace-mapping.d-DLVdEqOp.js';
5
5
 
package/dist/hmr.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  export { c as createHmrEmitter, a as createHotContext, g as getCache, h as handleMessage, r as reload, s as sendMessageBuffer, v as viteNodeHmrPlugin } from './chunk-hmr.mjs';
2
2
  import 'node:events';
3
- import './chunk-browser.mjs';
4
3
  import 'debug';
4
+ import './chunk-browser.mjs';
5
5
  import './utils.mjs';
6
- import 'node:url';
7
- import 'node:module';
8
6
  import 'node:fs';
7
+ import 'node:module';
8
+ import 'node:url';
9
9
  import 'pathe';
package/dist/server.cjs CHANGED
@@ -1,23 +1,138 @@
1
1
  'use strict';
2
2
 
3
- var node_perf_hooks = require('node:perf_hooks');
4
- var fs = require('node:fs');
5
3
  var assert = require('node:assert');
4
+ var fs = require('node:fs');
5
+ var node_perf_hooks = require('node:perf_hooks');
6
6
  var node_url = require('node:url');
7
- var pathe = require('pathe');
8
7
  var createDebug = require('debug');
9
- var utils = require('./utils.cjs');
10
- var constants = require('./constants.cjs');
8
+ var pathe = require('pathe');
11
9
  var browser = require('./chunk-browser.cjs');
10
+ var esModuleLexer = require('es-module-lexer');
11
+ var constants = require('./constants.cjs');
12
+ var utils = require('./utils.cjs');
12
13
  var sourceMap = require('./source-map.cjs');
13
14
  require('node:module');
14
15
  require('node:path');
15
16
 
17
+ function _interopNamespaceDefault(e) {
18
+ var n = Object.create(null);
19
+ if (e) {
20
+ Object.keys(e).forEach(function (k) {
21
+ if (k !== 'default') {
22
+ var d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: function () { return e[k]; }
26
+ });
27
+ }
28
+ });
29
+ }
30
+ n.default = e;
31
+ return Object.freeze(n);
32
+ }
33
+
34
+ var esModuleLexer__namespace = /*#__PURE__*/_interopNamespaceDefault(esModuleLexer);
35
+
36
+ function hashCode(s) {
37
+ return s.split("").reduce((a, b) => {
38
+ a = (a << 5) - a + b.charCodeAt(0);
39
+ return a & a;
40
+ }, 0);
41
+ }
42
+ class Debugger {
43
+ constructor(root, options) {
44
+ this.options = options;
45
+ if (options.dumpModules) {
46
+ this.dumpDir = pathe.resolve(
47
+ root,
48
+ options.dumpModules === true ? ".vite-node/dump" : options.dumpModules
49
+ );
50
+ }
51
+ if (this.dumpDir) {
52
+ if (options.loadDumppedModules) {
53
+ console.info(
54
+ browser.f.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`)
55
+ );
56
+ } else {
57
+ console.info(
58
+ browser.f.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`)
59
+ );
60
+ }
61
+ }
62
+ this.initPromise = this.clearDump();
63
+ }
64
+ dumpDir;
65
+ initPromise;
66
+ externalizeMap = /* @__PURE__ */ new Map();
67
+ async clearDump() {
68
+ if (!this.dumpDir) {
69
+ return;
70
+ }
71
+ if (!this.options.loadDumppedModules && fs.existsSync(this.dumpDir)) {
72
+ await fs.promises.rm(this.dumpDir, { recursive: true, force: true });
73
+ }
74
+ await fs.promises.mkdir(this.dumpDir, { recursive: true });
75
+ }
76
+ encodeId(id) {
77
+ return `${id.replace(/[^\w@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
78
+ id
79
+ )}.js`;
80
+ }
81
+ async recordExternalize(id, path) {
82
+ if (!this.dumpDir) {
83
+ return;
84
+ }
85
+ this.externalizeMap.set(id, path);
86
+ await this.writeInfo();
87
+ }
88
+ async dumpFile(id, result) {
89
+ if (!result || !this.dumpDir) {
90
+ return;
91
+ }
92
+ await this.initPromise;
93
+ const name = this.encodeId(id);
94
+ return await fs.promises.writeFile(
95
+ pathe.join(this.dumpDir, name),
96
+ `// ${id.replace(/\0/g, "\\0")}
97
+ ${result.code}`,
98
+ "utf-8"
99
+ );
100
+ }
101
+ async loadDump(id) {
102
+ if (!this.dumpDir) {
103
+ return null;
104
+ }
105
+ await this.initPromise;
106
+ const name = this.encodeId(id);
107
+ const path = pathe.join(this.dumpDir, name);
108
+ if (!fs.existsSync(path)) {
109
+ return null;
110
+ }
111
+ const code = await fs.promises.readFile(path, "utf-8");
112
+ return {
113
+ code: code.replace(/^\/\/.*\n/, ""),
114
+ map: void 0
115
+ };
116
+ }
117
+ async writeInfo() {
118
+ if (!this.dumpDir) {
119
+ return;
120
+ }
121
+ const info = JSON.stringify(
122
+ {
123
+ time: (/* @__PURE__ */ new Date()).toLocaleString(),
124
+ externalize: Object.fromEntries(this.externalizeMap.entries())
125
+ },
126
+ null,
127
+ 2
128
+ );
129
+ return fs.promises.writeFile(pathe.join(this.dumpDir, "info.json"), info, "utf-8");
130
+ }
131
+ }
132
+
16
133
  const BUILTIN_EXTENSIONS = /* @__PURE__ */ new Set([".mjs", ".cjs", ".node", ".wasm"]);
17
- const ESM_SYNTAX_RE = /(?:[\s;]|^)(?:import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
18
134
  const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
19
135
  const ESM_FOLDER_RE = /\/(es|esm)\/(.*\.js)$/;
20
- const COMMENT_RE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm;
21
136
  const defaultInline = [
22
137
  /virtual:/,
23
138
  /\.[mc]?ts$/,
@@ -72,8 +187,14 @@ async function isValidNodeImport(id) {
72
187
  if (/\.(?:\w+-)?esm?(?:-\w+)?\.js$|\/esm?\//.test(id)) {
73
188
  return false;
74
189
  }
75
- const code = await fs.promises.readFile(id, "utf8").catch(() => "");
76
- return !ESM_SYNTAX_RE.test(code.replace(COMMENT_RE, ""));
190
+ try {
191
+ await esModuleLexer__namespace.init;
192
+ const code = await fs.promises.readFile(id, "utf8");
193
+ const [, , , hasModuleSyntax] = esModuleLexer__namespace.parse(code);
194
+ return !hasModuleSyntax;
195
+ } catch {
196
+ return false;
197
+ }
77
198
  }
78
199
  const _defaultExternalizeCache = /* @__PURE__ */ new Map();
79
200
  async function shouldExternalize(id, options, cache = _defaultExternalizeCache) {
@@ -147,103 +268,6 @@ function patchWindowsImportPath(path) {
147
268
  }
148
269
  }
149
270
 
150
- function hashCode(s) {
151
- return s.split("").reduce((a, b) => {
152
- a = (a << 5) - a + b.charCodeAt(0);
153
- return a & a;
154
- }, 0);
155
- }
156
- class Debugger {
157
- constructor(root, options) {
158
- this.options = options;
159
- if (options.dumpModules) {
160
- this.dumpDir = pathe.resolve(
161
- root,
162
- options.dumpModules === true ? ".vite-node/dump" : options.dumpModules
163
- );
164
- }
165
- if (this.dumpDir) {
166
- if (options.loadDumppedModules) {
167
- console.info(
168
- browser.f.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`)
169
- );
170
- } else {
171
- console.info(
172
- browser.f.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`)
173
- );
174
- }
175
- }
176
- this.initPromise = this.clearDump();
177
- }
178
- dumpDir;
179
- initPromise;
180
- externalizeMap = /* @__PURE__ */ new Map();
181
- async clearDump() {
182
- if (!this.dumpDir) {
183
- return;
184
- }
185
- if (!this.options.loadDumppedModules && fs.existsSync(this.dumpDir)) {
186
- await fs.promises.rm(this.dumpDir, { recursive: true, force: true });
187
- }
188
- await fs.promises.mkdir(this.dumpDir, { recursive: true });
189
- }
190
- encodeId(id) {
191
- return `${id.replace(/[^\w@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
192
- id
193
- )}.js`;
194
- }
195
- async recordExternalize(id, path) {
196
- if (!this.dumpDir) {
197
- return;
198
- }
199
- this.externalizeMap.set(id, path);
200
- await this.writeInfo();
201
- }
202
- async dumpFile(id, result) {
203
- if (!result || !this.dumpDir) {
204
- return;
205
- }
206
- await this.initPromise;
207
- const name = this.encodeId(id);
208
- return await fs.promises.writeFile(
209
- pathe.join(this.dumpDir, name),
210
- `// ${id.replace(/\0/g, "\\0")}
211
- ${result.code}`,
212
- "utf-8"
213
- );
214
- }
215
- async loadDump(id) {
216
- if (!this.dumpDir) {
217
- return null;
218
- }
219
- await this.initPromise;
220
- const name = this.encodeId(id);
221
- const path = pathe.join(this.dumpDir, name);
222
- if (!fs.existsSync(path)) {
223
- return null;
224
- }
225
- const code = await fs.promises.readFile(path, "utf-8");
226
- return {
227
- code: code.replace(/^\/\/.*\n/, ""),
228
- map: void 0
229
- };
230
- }
231
- async writeInfo() {
232
- if (!this.dumpDir) {
233
- return;
234
- }
235
- const info = JSON.stringify(
236
- {
237
- time: (/* @__PURE__ */ new Date()).toLocaleString(),
238
- externalize: Object.fromEntries(this.externalizeMap.entries())
239
- },
240
- null,
241
- 2
242
- );
243
- return fs.promises.writeFile(pathe.join(this.dumpDir, "info.json"), info, "utf-8");
244
- }
245
- }
246
-
247
271
  const debugRequest = createDebug("vite-node:server:request");
248
272
  class ViteNodeServer {
249
273
  constructor(server, options = {}) {
package/dist/server.mjs CHANGED
@@ -1,21 +1,117 @@
1
- import { performance } from 'node:perf_hooks';
2
- import { existsSync, promises } from 'node:fs';
3
1
  import assert from 'node:assert';
2
+ import { existsSync, promises } from 'node:fs';
3
+ import { performance } from 'node:perf_hooks';
4
4
  import { pathToFileURL } from 'node:url';
5
- import { join, extname, dirname, resolve, relative, normalize } from 'pathe';
6
5
  import createDebug from 'debug';
7
- import { isNodeBuiltin, slash, findNearestPackageData, toArray, withTrailingSlash, normalizeModuleId, toFilePath } from './utils.mjs';
8
- import { KNOWN_ASSET_RE } from './constants.mjs';
6
+ import { resolve, join, extname, dirname, relative, normalize } from 'pathe';
9
7
  import { f } from './chunk-browser.mjs';
8
+ import * as esModuleLexer from 'es-module-lexer';
9
+ import { KNOWN_ASSET_RE } from './constants.mjs';
10
+ import { isNodeBuiltin, slash, findNearestPackageData, toArray, withTrailingSlash, normalizeModuleId, toFilePath } from './utils.mjs';
10
11
  import { withInlineSourcemap } from './source-map.mjs';
11
12
  import 'node:module';
12
13
  import 'node:path';
13
14
 
15
+ function hashCode(s) {
16
+ return s.split("").reduce((a, b) => {
17
+ a = (a << 5) - a + b.charCodeAt(0);
18
+ return a & a;
19
+ }, 0);
20
+ }
21
+ class Debugger {
22
+ constructor(root, options) {
23
+ this.options = options;
24
+ if (options.dumpModules) {
25
+ this.dumpDir = resolve(
26
+ root,
27
+ options.dumpModules === true ? ".vite-node/dump" : options.dumpModules
28
+ );
29
+ }
30
+ if (this.dumpDir) {
31
+ if (options.loadDumppedModules) {
32
+ console.info(
33
+ f.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`)
34
+ );
35
+ } else {
36
+ console.info(
37
+ f.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`)
38
+ );
39
+ }
40
+ }
41
+ this.initPromise = this.clearDump();
42
+ }
43
+ dumpDir;
44
+ initPromise;
45
+ externalizeMap = /* @__PURE__ */ new Map();
46
+ async clearDump() {
47
+ if (!this.dumpDir) {
48
+ return;
49
+ }
50
+ if (!this.options.loadDumppedModules && existsSync(this.dumpDir)) {
51
+ await promises.rm(this.dumpDir, { recursive: true, force: true });
52
+ }
53
+ await promises.mkdir(this.dumpDir, { recursive: true });
54
+ }
55
+ encodeId(id) {
56
+ return `${id.replace(/[^\w@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
57
+ id
58
+ )}.js`;
59
+ }
60
+ async recordExternalize(id, path) {
61
+ if (!this.dumpDir) {
62
+ return;
63
+ }
64
+ this.externalizeMap.set(id, path);
65
+ await this.writeInfo();
66
+ }
67
+ async dumpFile(id, result) {
68
+ if (!result || !this.dumpDir) {
69
+ return;
70
+ }
71
+ await this.initPromise;
72
+ const name = this.encodeId(id);
73
+ return await promises.writeFile(
74
+ join(this.dumpDir, name),
75
+ `// ${id.replace(/\0/g, "\\0")}
76
+ ${result.code}`,
77
+ "utf-8"
78
+ );
79
+ }
80
+ async loadDump(id) {
81
+ if (!this.dumpDir) {
82
+ return null;
83
+ }
84
+ await this.initPromise;
85
+ const name = this.encodeId(id);
86
+ const path = join(this.dumpDir, name);
87
+ if (!existsSync(path)) {
88
+ return null;
89
+ }
90
+ const code = await promises.readFile(path, "utf-8");
91
+ return {
92
+ code: code.replace(/^\/\/.*\n/, ""),
93
+ map: void 0
94
+ };
95
+ }
96
+ async writeInfo() {
97
+ if (!this.dumpDir) {
98
+ return;
99
+ }
100
+ const info = JSON.stringify(
101
+ {
102
+ time: (/* @__PURE__ */ new Date()).toLocaleString(),
103
+ externalize: Object.fromEntries(this.externalizeMap.entries())
104
+ },
105
+ null,
106
+ 2
107
+ );
108
+ return promises.writeFile(join(this.dumpDir, "info.json"), info, "utf-8");
109
+ }
110
+ }
111
+
14
112
  const BUILTIN_EXTENSIONS = /* @__PURE__ */ new Set([".mjs", ".cjs", ".node", ".wasm"]);
15
- const ESM_SYNTAX_RE = /(?:[\s;]|^)(?:import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
16
113
  const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
17
114
  const ESM_FOLDER_RE = /\/(es|esm)\/(.*\.js)$/;
18
- const COMMENT_RE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm;
19
115
  const defaultInline = [
20
116
  /virtual:/,
21
117
  /\.[mc]?ts$/,
@@ -70,8 +166,14 @@ async function isValidNodeImport(id) {
70
166
  if (/\.(?:\w+-)?esm?(?:-\w+)?\.js$|\/esm?\//.test(id)) {
71
167
  return false;
72
168
  }
73
- const code = await promises.readFile(id, "utf8").catch(() => "");
74
- return !ESM_SYNTAX_RE.test(code.replace(COMMENT_RE, ""));
169
+ try {
170
+ await esModuleLexer.init;
171
+ const code = await promises.readFile(id, "utf8");
172
+ const [, , , hasModuleSyntax] = esModuleLexer.parse(code);
173
+ return !hasModuleSyntax;
174
+ } catch {
175
+ return false;
176
+ }
75
177
  }
76
178
  const _defaultExternalizeCache = /* @__PURE__ */ new Map();
77
179
  async function shouldExternalize(id, options, cache = _defaultExternalizeCache) {
@@ -145,103 +247,6 @@ function patchWindowsImportPath(path) {
145
247
  }
146
248
  }
147
249
 
148
- function hashCode(s) {
149
- return s.split("").reduce((a, b) => {
150
- a = (a << 5) - a + b.charCodeAt(0);
151
- return a & a;
152
- }, 0);
153
- }
154
- class Debugger {
155
- constructor(root, options) {
156
- this.options = options;
157
- if (options.dumpModules) {
158
- this.dumpDir = resolve(
159
- root,
160
- options.dumpModules === true ? ".vite-node/dump" : options.dumpModules
161
- );
162
- }
163
- if (this.dumpDir) {
164
- if (options.loadDumppedModules) {
165
- console.info(
166
- f.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`)
167
- );
168
- } else {
169
- console.info(
170
- f.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`)
171
- );
172
- }
173
- }
174
- this.initPromise = this.clearDump();
175
- }
176
- dumpDir;
177
- initPromise;
178
- externalizeMap = /* @__PURE__ */ new Map();
179
- async clearDump() {
180
- if (!this.dumpDir) {
181
- return;
182
- }
183
- if (!this.options.loadDumppedModules && existsSync(this.dumpDir)) {
184
- await promises.rm(this.dumpDir, { recursive: true, force: true });
185
- }
186
- await promises.mkdir(this.dumpDir, { recursive: true });
187
- }
188
- encodeId(id) {
189
- return `${id.replace(/[^\w@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
190
- id
191
- )}.js`;
192
- }
193
- async recordExternalize(id, path) {
194
- if (!this.dumpDir) {
195
- return;
196
- }
197
- this.externalizeMap.set(id, path);
198
- await this.writeInfo();
199
- }
200
- async dumpFile(id, result) {
201
- if (!result || !this.dumpDir) {
202
- return;
203
- }
204
- await this.initPromise;
205
- const name = this.encodeId(id);
206
- return await promises.writeFile(
207
- join(this.dumpDir, name),
208
- `// ${id.replace(/\0/g, "\\0")}
209
- ${result.code}`,
210
- "utf-8"
211
- );
212
- }
213
- async loadDump(id) {
214
- if (!this.dumpDir) {
215
- return null;
216
- }
217
- await this.initPromise;
218
- const name = this.encodeId(id);
219
- const path = join(this.dumpDir, name);
220
- if (!existsSync(path)) {
221
- return null;
222
- }
223
- const code = await promises.readFile(path, "utf-8");
224
- return {
225
- code: code.replace(/^\/\/.*\n/, ""),
226
- map: void 0
227
- };
228
- }
229
- async writeInfo() {
230
- if (!this.dumpDir) {
231
- return;
232
- }
233
- const info = JSON.stringify(
234
- {
235
- time: (/* @__PURE__ */ new Date()).toLocaleString(),
236
- externalize: Object.fromEntries(this.externalizeMap.entries())
237
- },
238
- null,
239
- 2
240
- );
241
- return promises.writeFile(join(this.dumpDir, "info.json"), info, "utf-8");
242
- }
243
- }
244
-
245
250
  const debugRequest = createDebug("vite-node:server:request");
246
251
  class ViteNodeServer {
247
252
  constructor(server, options = {}) {
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var pathe = require('pathe');
4
- var utils = require('./utils.cjs');
5
- var path = require('node:path');
6
4
  var fs = require('node:fs');
7
- require('node:url');
5
+ var path = require('node:path');
6
+ var utils = require('./utils.cjs');
8
7
  require('node:module');
8
+ require('node:url');
9
9
 
10
10
  const comma = ','.charCodeAt(0);
11
11
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -1,9 +1,9 @@
1
1
  import { isAbsolute, resolve as resolve$2, relative, dirname } from 'pathe';
2
- import { withTrailingSlash } from './utils.mjs';
3
- import path from 'node:path';
4
2
  import fs from 'node:fs';
5
- import 'node:url';
3
+ import path from 'node:path';
4
+ import { withTrailingSlash } from './utils.mjs';
6
5
  import 'node:module';
6
+ import 'node:url';
7
7
 
8
8
  const comma = ','.charCodeAt(0);
9
9
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
package/dist/types.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { A as Arrayable, f as Awaitable, i as CreateHotContextFunction, D as DebuggerOptions, c as DepsHandlingOptions, g as FetchFunction, F as FetchResult, b as HotContext, j as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, h as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, l as ViteNodeResolveModule, k as ViteNodeRunnerOptions, V as ViteNodeServerOptions } from './index-z0R8hVRu.js';
2
1
  export { D as DecodedSourceMap, E as EncodedSourceMap, S as SourceMapInput } from './trace-mapping.d-DLVdEqOp.js';
2
+ export { A as Arrayable, f as Awaitable, i as CreateHotContextFunction, D as DebuggerOptions, c as DepsHandlingOptions, g as FetchFunction, F as FetchResult, b as HotContext, j as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, h as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, l as ViteNodeResolveModule, k as ViteNodeRunnerOptions, V as ViteNodeServerOptions } from './index-z0R8hVRu.js';
package/dist/utils.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var node_url = require('node:url');
4
- var node_module = require('node:module');
5
3
  var fs = require('node:fs');
4
+ var node_module = require('node:module');
5
+ var node_url = require('node:url');
6
6
  var pathe = require('pathe');
7
7
 
8
8
  const isWindows = process.platform === "win32";
package/dist/utils.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { fileURLToPath, pathToFileURL } from 'node:url';
2
- import { builtinModules } from 'node:module';
3
1
  import { existsSync, promises } from 'node:fs';
2
+ import { builtinModules } from 'node:module';
3
+ import { fileURLToPath, pathToFileURL } from 'node:url';
4
4
  import { resolve, dirname, join } from 'pathe';
5
5
 
6
6
  const isWindows = process.platform === "win32";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-node",
3
3
  "type": "module",
4
- "version": "2.1.3",
4
+ "version": "2.1.5",
5
5
  "description": "Vite as Node.js runtime",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -78,7 +78,8 @@
78
78
  },
79
79
  "dependencies": {
80
80
  "cac": "^6.7.14",
81
- "debug": "^4.3.6",
81
+ "debug": "^4.3.7",
82
+ "es-module-lexer": "^1.5.4",
82
83
  "pathe": "^1.1.2",
83
84
  "vite": "^5.0.0"
84
85
  },