vite-node 0.1.17 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var fs = require('fs');
6
+ var mlly = require('mlly');
7
+ var url = require('url');
8
+ var pathe = require('pathe');
9
+
10
+ const isWindows = process.platform === "win32";
11
+ function slash(str) {
12
+ return str.replace(/\\/g, "/");
13
+ }
14
+ function toFilePath(id, root) {
15
+ let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(pathe.dirname(root)) && pathe.dirname(root) !== "/" ? id : id.startsWith("/") ? slash(pathe.resolve(root, id.slice(1))) : id;
16
+ if (absolute.startsWith("//"))
17
+ absolute = absolute.slice(1);
18
+ return isWindows && absolute.startsWith("/") ? url.fileURLToPath(url.pathToFileURL(absolute.slice(1)).href) : absolute;
19
+ }
20
+
21
+ const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
22
+ const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/;
23
+ const defaultInline = [
24
+ /\/vitest\/dist\//,
25
+ /vitest-virtual-\w+\/dist/,
26
+ /virtual:/,
27
+ /\.ts$/,
28
+ ESM_EXT_RE,
29
+ ESM_FOLDER_RE
30
+ ];
31
+ const depsExternal = [
32
+ /\.cjs.js$/,
33
+ /\.mjs$/
34
+ ];
35
+ function guessCJSversion(id) {
36
+ if (id.match(ESM_EXT_RE)) {
37
+ for (const i of [
38
+ id.replace(ESM_EXT_RE, ".mjs"),
39
+ id.replace(ESM_EXT_RE, ".umd.js"),
40
+ id.replace(ESM_EXT_RE, ".cjs.js"),
41
+ id.replace(ESM_EXT_RE, ".js")
42
+ ]) {
43
+ if (fs.existsSync(i))
44
+ return i;
45
+ }
46
+ }
47
+ if (id.match(ESM_FOLDER_RE)) {
48
+ for (const i of [
49
+ id.replace(ESM_FOLDER_RE, "/umd/$1"),
50
+ id.replace(ESM_FOLDER_RE, "/cjs/$1"),
51
+ id.replace(ESM_FOLDER_RE, "/$1")
52
+ ]) {
53
+ if (fs.existsSync(i))
54
+ return i;
55
+ }
56
+ }
57
+ }
58
+ async function shouldExternalize(id, options, cache = new Map()) {
59
+ if (!cache.has(id))
60
+ cache.set(id, _shouldExternalize(id, options));
61
+ return cache.get(id);
62
+ }
63
+ async function _shouldExternalize(id, options) {
64
+ if (mlly.isNodeBuiltin(id))
65
+ return id;
66
+ id = patchWindowsImportPath(id);
67
+ if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
68
+ return false;
69
+ if (matchExternalizePattern(id, options == null ? void 0 : options.external))
70
+ return id;
71
+ const isNodeModule = id.includes("/node_modules/");
72
+ id = isNodeModule ? guessCJSversion(id) || id : id;
73
+ if (matchExternalizePattern(id, defaultInline))
74
+ return false;
75
+ if (matchExternalizePattern(id, depsExternal))
76
+ return id;
77
+ if (isNodeModule && await mlly.isValidNodeImport(id))
78
+ return id;
79
+ return false;
80
+ }
81
+ function matchExternalizePattern(id, patterns) {
82
+ if (!patterns)
83
+ return false;
84
+ for (const ex of patterns) {
85
+ if (typeof ex === "string") {
86
+ if (id.includes(`/node_modules/${ex}/`))
87
+ return true;
88
+ } else {
89
+ if (ex.test(id))
90
+ return true;
91
+ }
92
+ }
93
+ return false;
94
+ }
95
+ function patchWindowsImportPath(path) {
96
+ if (path.match(/^\w:\\/))
97
+ return `file:///${slash(path)}`;
98
+ else if (path.match(/^\w:\//))
99
+ return `file:///${path}`;
100
+ else
101
+ return path;
102
+ }
103
+
104
+ let SOURCEMAPPING_URL = "sourceMa";
105
+ SOURCEMAPPING_URL += "ppingURL";
106
+ class ViteNodeServer {
107
+ constructor(server, options = {}) {
108
+ this.server = server;
109
+ this.options = options;
110
+ this.promiseMap = new Map();
111
+ }
112
+ shouldExternalize(id) {
113
+ return shouldExternalize(id, this.options.deps);
114
+ }
115
+ async fetchModule(id) {
116
+ const externalize = await this.shouldExternalize(toFilePath(id, this.server.config.root));
117
+ if (externalize)
118
+ return { externalize };
119
+ const r = await this.transformRequest(id);
120
+ return { code: r == null ? void 0 : r.code };
121
+ }
122
+ async resolveId(id, importer) {
123
+ return this.server.pluginContainer.resolveId(id, importer, { ssr: true });
124
+ }
125
+ async transformRequest(id) {
126
+ if (!this.promiseMap.has(id)) {
127
+ this.promiseMap.set(id, this._transformRequest(id).finally(() => {
128
+ this.promiseMap.delete(id);
129
+ }));
130
+ }
131
+ return this.promiseMap.get(id);
132
+ }
133
+ getTransformMode(id) {
134
+ var _a, _b, _c, _d;
135
+ const withoutQuery = id.split("?")[0];
136
+ if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
137
+ return "web";
138
+ if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r)))
139
+ return "ssr";
140
+ if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
141
+ return "ssr";
142
+ return "web";
143
+ }
144
+ async _transformRequest(id) {
145
+ let result = null;
146
+ const mode = this.getTransformMode(id);
147
+ if (mode === "web") {
148
+ result = await this.server.transformRequest(id);
149
+ if (result)
150
+ result = await this.server.ssrTransform(result.code, result.map, id);
151
+ } else {
152
+ result = await this.server.transformRequest(id, { ssr: true });
153
+ }
154
+ if (this.options.sourcemap !== false && result && !id.includes("node_modules"))
155
+ withInlineSourcemap(result);
156
+ return result;
157
+ }
158
+ }
159
+ async function withInlineSourcemap(result) {
160
+ const { code, map } = result;
161
+ if (code.includes(`${SOURCEMAPPING_URL}=`))
162
+ return result;
163
+ if (map)
164
+ result.code = `${code}
165
+
166
+ //# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), "utf-8").toString("base64")}
167
+ `;
168
+ return result;
169
+ }
170
+
171
+ exports.ViteNodeServer = ViteNodeServer;
172
+ exports.guessCJSversion = guessCJSversion;
173
+ exports.shouldExternalize = shouldExternalize;
174
+ exports.withInlineSourcemap = withInlineSourcemap;
package/dist/server.js CHANGED
@@ -1,8 +1,18 @@
1
1
  import { existsSync } from 'fs';
2
2
  import { isNodeBuiltin, isValidNodeImport } from 'mlly';
3
- import { slash, toFilePath } from './utils.js';
4
- import 'url';
5
- import 'pathe';
3
+ import { fileURLToPath, pathToFileURL } from 'url';
4
+ import { dirname, resolve } from 'pathe';
5
+
6
+ const isWindows = process.platform === "win32";
7
+ function slash(str) {
8
+ return str.replace(/\\/g, "/");
9
+ }
10
+ function toFilePath(id, root) {
11
+ let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(dirname(root)) && dirname(root) !== "/" ? id : id.startsWith("/") ? slash(resolve(root, id.slice(1))) : id;
12
+ if (absolute.startsWith("//"))
13
+ absolute = absolute.slice(1);
14
+ return isWindows && absolute.startsWith("/") ? fileURLToPath(pathToFileURL(absolute.slice(1)).href) : absolute;
15
+ }
6
16
 
7
17
  const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
8
18
  const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/;
@@ -41,18 +51,18 @@ function guessCJSversion(id) {
41
51
  }
42
52
  }
43
53
  }
44
- async function shouldExternalize(id, config, cache = new Map()) {
54
+ async function shouldExternalize(id, options, cache = new Map()) {
45
55
  if (!cache.has(id))
46
- cache.set(id, _shouldExternalize(id, config));
56
+ cache.set(id, _shouldExternalize(id, options));
47
57
  return cache.get(id);
48
58
  }
49
- async function _shouldExternalize(id, config) {
59
+ async function _shouldExternalize(id, options) {
50
60
  if (isNodeBuiltin(id))
51
61
  return id;
52
62
  id = patchWindowsImportPath(id);
53
- if (matchExternalizePattern(id, config == null ? void 0 : config.inline))
63
+ if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
54
64
  return false;
55
- if (matchExternalizePattern(id, config == null ? void 0 : config.external))
65
+ if (matchExternalizePattern(id, options == null ? void 0 : options.external))
56
66
  return id;
57
67
  const isNodeModule = id.includes("/node_modules/");
58
68
  id = isNodeModule ? guessCJSversion(id) || id : id;
@@ -105,6 +115,9 @@ class ViteNodeServer {
105
115
  const r = await this.transformRequest(id);
106
116
  return { code: r == null ? void 0 : r.code };
107
117
  }
118
+ async resolveId(id, importer) {
119
+ return this.server.pluginContainer.resolveId(id, importer, { ssr: true });
120
+ }
108
121
  async transformRequest(id) {
109
122
  if (!this.promiseMap.has(id)) {
110
123
  this.promiseMap.set(id, this._transformRequest(id).finally(() => {
@@ -134,7 +147,7 @@ class ViteNodeServer {
134
147
  } else {
135
148
  result = await this.server.transformRequest(id, { ssr: true });
136
149
  }
137
- if (result && !id.includes("node_modules"))
150
+ if (this.options.sourcemap !== false && result && !id.includes("node_modules"))
138
151
  withInlineSourcemap(result);
139
152
  return result;
140
153
  }
package/dist/utils.cjs ADDED
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var url = require('url');
6
+ var pathe = require('pathe');
7
+
8
+ const isWindows = process.platform === "win32";
9
+ function slash(str) {
10
+ return str.replace(/\\/g, "/");
11
+ }
12
+ function normalizeId(id, base) {
13
+ if (base && id.startsWith(base))
14
+ id = `/${id.slice(base.length)}`;
15
+ return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/^node:/, "").replace(/[?&]v=\w+/, "?").replace(/\?$/, "");
16
+ }
17
+ function isPrimitive(v) {
18
+ return v !== Object(v);
19
+ }
20
+ function toFilePath(id, root) {
21
+ let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(pathe.dirname(root)) && pathe.dirname(root) !== "/" ? id : id.startsWith("/") ? slash(pathe.resolve(root, id.slice(1))) : id;
22
+ if (absolute.startsWith("//"))
23
+ absolute = absolute.slice(1);
24
+ return isWindows && absolute.startsWith("/") ? url.fileURLToPath(url.pathToFileURL(absolute.slice(1)).href) : absolute;
25
+ }
26
+
27
+ exports.isPrimitive = isPrimitive;
28
+ exports.isWindows = isWindows;
29
+ exports.normalizeId = normalizeId;
30
+ exports.slash = slash;
31
+ exports.toFilePath = toFilePath;
package/dist/utils.js CHANGED
@@ -14,7 +14,7 @@ function isPrimitive(v) {
14
14
  return v !== Object(v);
15
15
  }
16
16
  function toFilePath(id, root) {
17
- let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(dirname(root)) ? id : id.startsWith("/") ? slash(resolve(root, id.slice(1))) : id;
17
+ let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(dirname(root)) && dirname(root) !== "/" ? id : id.startsWith("/") ? slash(resolve(root, id.slice(1))) : id;
18
18
  if (absolute.startsWith("//"))
19
19
  absolute = absolute.slice(1);
20
20
  return isWindows && absolute.startsWith("/") ? fileURLToPath(pathToFileURL(absolute.slice(1)).href) : absolute;
package/index.d.ts CHANGED
@@ -1,12 +1,17 @@
1
- interface ExternalizeOptions {
1
+ interface DepsHandlingOptions {
2
2
  external?: (string | RegExp)[];
3
3
  inline?: (string | RegExp)[];
4
+ /**
5
+ * Try to guess the CJS version of a package when it's invalid ESM
6
+ * @default true
7
+ */
4
8
  fallbackCJS?: boolean;
5
9
  }
6
10
  declare type FetchFunction = (id: string) => Promise<{
7
11
  code?: string;
8
12
  externalize?: string;
9
13
  }>;
14
+ declare type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>;
10
15
  interface ModuleCache {
11
16
  promise?: Promise<any>;
12
17
  exports?: any;
@@ -14,18 +19,37 @@ interface ModuleCache {
14
19
  }
15
20
  interface ViteNodeRunnerOptions {
16
21
  fetchModule: FetchFunction;
22
+ resolveId: ResolveIdFunction;
17
23
  root: string;
18
24
  base?: string;
19
25
  moduleCache?: Map<string, ModuleCache>;
20
- interpretDefault?: boolean;
26
+ interopDefault?: boolean;
21
27
  requestStubs?: Record<string, any>;
22
28
  }
29
+ interface ViteNodeResolveId {
30
+ external?: boolean | 'absolute' | 'relative';
31
+ id: string;
32
+ meta?: Record<string, any> | null;
33
+ moduleSideEffects?: boolean | 'no-treeshake' | null;
34
+ syntheticNamedExports?: boolean | string | null;
35
+ }
23
36
  interface ViteNodeServerOptions {
24
- deps?: ExternalizeOptions;
37
+ /**
38
+ * Inject inline sourcemap to modules
39
+ * @default true
40
+ */
41
+ sourcemap?: boolean;
42
+ /**
43
+ * Deps handling
44
+ */
45
+ deps?: DepsHandlingOptions;
46
+ /**
47
+ * Tranform method for modules
48
+ */
25
49
  transformMode?: {
26
50
  ssr?: RegExp[];
27
51
  web?: RegExp[];
28
52
  };
29
53
  }
30
54
 
31
- export { ExternalizeOptions, FetchFunction, ModuleCache, ViteNodeRunnerOptions, ViteNodeServerOptions };
55
+ export { DepsHandlingOptions, FetchFunction, ModuleCache, ResolveIdFunction, ViteNodeResolveId, ViteNodeRunnerOptions, ViteNodeServerOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.1.17",
3
+ "version": "0.1.21",
4
4
  "description": "Vite as Node.js runtime",
5
5
  "homepage": "https://github.com/vitest-dev/vitest#readme",
6
6
  "bugs": {
@@ -18,18 +18,22 @@
18
18
  "exports": {
19
19
  ".": {
20
20
  "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs",
21
22
  "types": "./index.d.ts"
22
23
  },
23
24
  "./client": {
24
25
  "import": "./dist/client.js",
26
+ "require": "./dist/client.cjs",
25
27
  "types": "./client.d.ts"
26
28
  },
27
29
  "./server": {
28
30
  "import": "./dist/server.js",
31
+ "require": "./dist/server.cjs",
29
32
  "types": "./server.d.ts"
30
33
  },
31
34
  "./utils": {
32
35
  "import": "./dist/utils.js",
36
+ "require": "./dist/utils.cjs",
33
37
  "types": "./utils.d.ts"
34
38
  }
35
39
  },
package/server.d.ts CHANGED
@@ -1,12 +1,34 @@
1
1
  import { ViteDevServer, TransformResult } from 'vite';
2
2
 
3
- interface ExternalizeOptions {
3
+ interface DepsHandlingOptions {
4
4
  external?: (string | RegExp)[];
5
5
  inline?: (string | RegExp)[];
6
+ /**
7
+ * Try to guess the CJS version of a package when it's invalid ESM
8
+ * @default true
9
+ */
6
10
  fallbackCJS?: boolean;
7
11
  }
12
+ interface ViteNodeResolveId {
13
+ external?: boolean | 'absolute' | 'relative';
14
+ id: string;
15
+ meta?: Record<string, any> | null;
16
+ moduleSideEffects?: boolean | 'no-treeshake' | null;
17
+ syntheticNamedExports?: boolean | string | null;
18
+ }
8
19
  interface ViteNodeServerOptions {
9
- deps?: ExternalizeOptions;
20
+ /**
21
+ * Inject inline sourcemap to modules
22
+ * @default true
23
+ */
24
+ sourcemap?: boolean;
25
+ /**
26
+ * Deps handling
27
+ */
28
+ deps?: DepsHandlingOptions;
29
+ /**
30
+ * Tranform method for modules
31
+ */
10
32
  transformMode?: {
11
33
  ssr?: RegExp[];
12
34
  web?: RegExp[];
@@ -14,7 +36,7 @@ interface ViteNodeServerOptions {
14
36
  }
15
37
 
16
38
  declare function guessCJSversion(id: string): string | undefined;
17
- declare function shouldExternalize(id: string, config?: ExternalizeOptions, cache?: Map<string, Promise<string | false>>): Promise<string | false>;
39
+ declare function shouldExternalize(id: string, options?: DepsHandlingOptions, cache?: Map<string, Promise<string | false>>): Promise<string | false>;
18
40
 
19
41
  declare class ViteNodeServer {
20
42
  server: ViteDevServer;
@@ -29,6 +51,7 @@ declare class ViteNodeServer {
29
51
  code: string | undefined;
30
52
  externalize?: undefined;
31
53
  }>;
54
+ resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null>;
32
55
  transformRequest(id: string): Promise<TransformResult | null | undefined>;
33
56
  private getTransformMode;
34
57
  private _transformRequest;