vite-node 0.0.141 → 0.0.142

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
@@ -57,7 +57,7 @@ const runner = new ViteNodeRunner({
57
57
  })
58
58
 
59
59
  // execute the file
60
- await runner.run('./example.ts')
60
+ await runner.executeFile('./example.ts')
61
61
 
62
62
  // close the vite server
63
63
  await server.close()
package/client.d.ts CHANGED
@@ -22,7 +22,8 @@ declare class ViteNodeRunner {
22
22
  externalCache: Map<string, string | Promise<false | string>>;
23
23
  moduleCache: Map<string, ModuleCache>;
24
24
  constructor(options: ViteNodeRunnerOptions);
25
- run(file: string): Promise<any>;
25
+ executeFile(file: string): Promise<any>;
26
+ executeId(id: string): Promise<any>;
26
27
  cachedRequest(rawId: string, callstack: string[]): Promise<any>;
27
28
  directRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
28
29
  prepareContext(context: Record<string, any>): Record<string, any>;
package/dist/cli.js CHANGED
@@ -73,6 +73,6 @@ async function run(options = {}) {
73
73
  }
74
74
  });
75
75
  for (const file of files)
76
- await runner.run(file);
76
+ await runner.executeFile(file);
77
77
  await server.close();
78
78
  }
package/dist/client.js CHANGED
@@ -2,7 +2,7 @@ import { builtinModules, createRequire } from 'module';
2
2
  import { pathToFileURL, fileURLToPath } from 'url';
3
3
  import vm from 'vm';
4
4
  import { resolve, dirname } from 'pathe';
5
- import { slash, normalizeId, toFilePath } from './utils.js';
5
+ import { slash, normalizeId, toFilePath, isPrimitive } from './utils.js';
6
6
 
7
7
  class ViteNodeRunner {
8
8
  constructor(options) {
@@ -12,9 +12,12 @@ class ViteNodeRunner {
12
12
  this.externalCache = new Map();
13
13
  builtinModules.forEach((m) => this.externalCache.set(m, m));
14
14
  }
15
- async run(file) {
15
+ async executeFile(file) {
16
16
  return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
17
17
  }
18
+ async executeId(id) {
19
+ return await this.cachedRequest(id, []);
20
+ }
18
21
  async cachedRequest(rawId, callstack) {
19
22
  var _a, _b;
20
23
  const id = normalizeId(rawId, this.options.base);
@@ -98,7 +101,7 @@ function hasNestedDefault(target) {
98
101
  function proxyMethod(name, tryDefault) {
99
102
  return function(target, key, ...args) {
100
103
  const result = Reflect[name](target, key, ...args);
101
- if (typeof target.default !== "object")
104
+ if (isPrimitive(target.default))
102
105
  return result;
103
106
  if (tryDefault && key === "default" || typeof result === "undefined")
104
107
  return Reflect[name](target.default, key, ...args);
package/dist/utils.js CHANGED
@@ -10,6 +10,9 @@ function normalizeId(id, base) {
10
10
  id = `/${id.slice(base.length)}`;
11
11
  return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/^node:/, "").replace(/[?&]v=\w+/, "?").replace(/\?$/, "");
12
12
  }
13
+ function isPrimitive(v) {
14
+ return v !== Object(v);
15
+ }
13
16
  function toFilePath(id, root) {
14
17
  let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(dirname(root)) ? id : id.startsWith("/") ? slash(resolve(root, id.slice(1))) : id;
15
18
  if (absolute.startsWith("//"))
@@ -17,4 +20,4 @@ function toFilePath(id, root) {
17
20
  return isWindows && absolute.startsWith("/") ? fileURLToPath(pathToFileURL(absolute.slice(1)).href) : absolute;
18
21
  }
19
22
 
20
- export { isWindows, normalizeId, slash, toFilePath };
23
+ export { isPrimitive, isWindows, normalizeId, slash, toFilePath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.0.141",
3
+ "version": "0.0.142",
4
4
  "description": "Vite as Node.js runtime",
5
5
  "homepage": "https://github.com/vitest-dev/vitest#readme",
6
6
  "bugs": {
@@ -63,5 +63,5 @@
63
63
  "dev": "rollup -c --watch --watch.include=src/**",
64
64
  "typecheck": "tsc --noEmit"
65
65
  },
66
- "readme": "# vite-node\n\n[![NPM version](https://img.shields.io/npm/v/vite-node?color=a1b858&label=)](https://www.npmjs.com/package/vite-node)\n\nVite as Node runtime. The engine powers [Vitest](https://github.com/vitest-dev/vitest).\n\n## Features\n\n- Out-of-box ESM & TypeScript support (possible for more with plugins)\n- Top-level await\n- Vite plugins, resolve, aliasing\n- Respect `vite.config.ts`\n- Shims for `__dirname` and `__filename` in ESM\n- Access to native node modules like `fs`, `path`, etc.\n\n## CLI Usage\n\nRun JS/TS file on Node.js using Vite's resolvers and transformers.\n\n```bash\nnpx vite-node index.ts\n```\n\nOptions:\n\n```bash\nnpx vite-node -h\n```\n\n## Programmatic Usage\n\nIn Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having the server and running in the same context\n\n```ts\nimport { createServer } from 'vite'\nimport { ViteNodeServer } from 'vite-node/server'\nimport { ViteNodeRunner } from 'vite-node/client'\n\n// create vite server\nconst server = await createServer()\n// this is need to initialize the plugins\nawait server.pluginContainer.buildStart({})\n\n// create vite-node server\nconst node = new ViteNodeServer(server)\n\n// create vite-node runner\nconst runner = new ViteNodeRunner({\n root: server.config.root,\n base: server.config.base,\n // when having the server and runner in a different context,\n // you will need to handle the communication between them\n // and pass to this function\n fetchModule(id) {\n return node.fetchModule(id)\n },\n})\n\n// execute the file\nawait runner.run('./example.ts')\n\n// close the vite server\nawait server.close()\n```\n\n## Credits\n\nBased on [@pi0](https://github.com/pi0)'s brilliant idea of having a Vite server as the on-demand transforming service for [Nuxt's Vite SSR](https://github.com/nuxt/vite/pull/201).\n\nThanks [@brillout](https://github.com/brillout) for kindly sharing this package name.\n\n## Sponsors\n\n<p align=\"center\">\n <a href=\"https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg\">\n <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>\n </a>\n</p>\n\n## License\n\n[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)\n"
66
+ "readme": "# vite-node\n\n[![NPM version](https://img.shields.io/npm/v/vite-node?color=a1b858&label=)](https://www.npmjs.com/package/vite-node)\n\nVite as Node runtime. The engine powers [Vitest](https://github.com/vitest-dev/vitest).\n\n## Features\n\n- Out-of-box ESM & TypeScript support (possible for more with plugins)\n- Top-level await\n- Vite plugins, resolve, aliasing\n- Respect `vite.config.ts`\n- Shims for `__dirname` and `__filename` in ESM\n- Access to native node modules like `fs`, `path`, etc.\n\n## CLI Usage\n\nRun JS/TS file on Node.js using Vite's resolvers and transformers.\n\n```bash\nnpx vite-node index.ts\n```\n\nOptions:\n\n```bash\nnpx vite-node -h\n```\n\n## Programmatic Usage\n\nIn Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having the server and running in the same context\n\n```ts\nimport { createServer } from 'vite'\nimport { ViteNodeServer } from 'vite-node/server'\nimport { ViteNodeRunner } from 'vite-node/client'\n\n// create vite server\nconst server = await createServer()\n// this is need to initialize the plugins\nawait server.pluginContainer.buildStart({})\n\n// create vite-node server\nconst node = new ViteNodeServer(server)\n\n// create vite-node runner\nconst runner = new ViteNodeRunner({\n root: server.config.root,\n base: server.config.base,\n // when having the server and runner in a different context,\n // you will need to handle the communication between them\n // and pass to this function\n fetchModule(id) {\n return node.fetchModule(id)\n },\n})\n\n// execute the file\nawait runner.executeFile('./example.ts')\n\n// close the vite server\nawait server.close()\n```\n\n## Credits\n\nBased on [@pi0](https://github.com/pi0)'s brilliant idea of having a Vite server as the on-demand transforming service for [Nuxt's Vite SSR](https://github.com/nuxt/vite/pull/201).\n\nThanks [@brillout](https://github.com/brillout) for kindly sharing this package name.\n\n## Sponsors\n\n<p align=\"center\">\n <a href=\"https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg\">\n <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>\n </a>\n</p>\n\n## License\n\n[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)\n"
67
67
  }
package/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare const isWindows: boolean;
2
2
  declare function slash(str: string): string;
3
3
  declare function normalizeId(id: string, base?: string): string;
4
+ declare function isPrimitive(v: any): boolean;
4
5
  declare function toFilePath(id: string, root: string): string;
5
6
 
6
- export { isWindows, normalizeId, slash, toFilePath };
7
+ export { isPrimitive, isWindows, normalizeId, slash, toFilePath };