版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <p align="center">
  2. <img src="https://github.com/vitest-dev/vitest/blob/main/packages/vite-node/assets/vite-node.svg?raw=true" height="120">
  3. </p>
  4. <h1 align="center">
  5. vite-node
  6. </h1>
  7. <p align="center">
  8. Vite as Node runtime.<br>The engine powers <a href="https://github.com/vitest-dev/vitest">Vitest</a> and <a href="https://github.com/nuxt/framework">Nuxt 3 Dev SSR</a>.
  9. <p>
  10. <p align="center">
  11. <a href="https://www.npmjs.com/package/vitest"><img src="https://img.shields.io/npm/v/vite-node?color=FCC72B&label="></a>
  12. <p>
  13. ## Features
  14. - On-demand evaluation
  15. - Vite's pipeline, plugins, resolve, aliasing
  16. - Out-of-box ESM & TypeScript support
  17. - Respect `vite.config.ts`
  18. - Hot module replacement (HMR)
  19. - Separate server/client architecture
  20. - Top-level `await`
  21. - Shims for `__dirname` and `__filename` in ESM
  22. - Access to native node modules like `fs`, `path`, etc.
  23. ## CLI Usage
  24. Run JS/TS file on Node.js using Vite's resolvers and transformers.
  25. ```bash
  26. npx vite-node index.ts
  27. ```
  28. Options:
  29. ```bash
  30. npx vite-node -h
  31. ```
  32. ### Options via CLI
  33. [All `ViteNodeServer` options](https://github.com/vitest-dev/vitest/blob/main/packages/vite-node/src/types.ts#L70-L89) are supported by the CLI. They may be defined through the dot syntax, as shown below:
  34. ```bash
  35. npx vite-node --options.deps.inline="module-name" --options.deps.external="/module-regexp/" index.ts
  36. ```
  37. Note that for options supporting RegExps, strings passed to the CLI must start _and_ end with a `/`;
  38. ### Hashbang
  39. If you prefer to write scripts that don't need to be passed into Vite Node, you can declare it in the [hashbang](https://bash.cyberciti.biz/guide/Shebang).
  40. Simply add `#!/usr/bin/env vite-node --script` at the top of your file:
  41. _file.ts_
  42. ```ts
  43. #!/usr/bin/env vite-node --script
  44. console.log('argv:', process.argv.slice(2))
  45. ```
  46. And make the file executable:
  47. ```sh
  48. chmod +x ./file.ts
  49. ```
  50. Now, you can run the file without passing it into Vite Node:
  51. ```sh
  52. $ ./file.ts hello
  53. argv: [ 'hello' ]
  54. ```
  55. Note that when using the `--script` option, Vite Node forwards every argument and option to the script to execute, even the one supported by Vite Node itself.
  56. ## Programmatic Usage
  57. In 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 both (server and runner) running in the same context
  58. ```ts
  59. import { createServer } from 'vite'
  60. import { ViteNodeServer } from 'vite-node/server'
  61. import { ViteNodeRunner } from 'vite-node/client'
  62. import { installSourcemapsSupport } from 'vite-node/source-map'
  63. // create vite server
  64. const server = await createServer({
  65. optimizeDeps: {
  66. // It's recommended to disable deps optimization
  67. disabled: true,
  68. },
  69. })
  70. // this is need to initialize the plugins
  71. await server.pluginContainer.buildStart({})
  72. // create vite-node server
  73. const node = new ViteNodeServer(server)
  74. // fixes stacktraces in Errors
  75. installSourcemapsSupport({
  76. getSourceMap: source => node.getSourceMap(source),
  77. })
  78. // create vite-node runner
  79. const runner = new ViteNodeRunner({
  80. root: server.config.root,
  81. base: server.config.base,
  82. // when having the server and runner in a different context,
  83. // you will need to handle the communication between them
  84. // and pass to this function
  85. fetchModule(id) {
  86. return node.fetchModule(id)
  87. },
  88. resolveId(id, importer) {
  89. return node.resolveId(id, importer)
  90. },
  91. })
  92. // execute the file
  93. await runner.executeFile('./example.ts')
  94. // close the vite server
  95. await server.close()
  96. ```
  97. ## Debugging
  98. ### Debug Transformation
  99. Sometimes you might want to inspect the transformed code to investigate issues. You can set environment variable `VITE_NODE_DEBUG_DUMP=true` to let vite-node write the transformed result of each module under `.vite-node/dump`.
  100. If you want to debug by modifying the dumped code, you can change the value of `VITE_NODE_DEBUG_DUMP` to `load` and search for the dumpped files and use them for executing.
  101. ```bash
  102. VITE_NODE_DEBUG_DUMP=load vite-node example.ts
  103. ```
  104. Or programmatically:
  105. ```js
  106. import { ViteNodeServer } from 'vite-node/server'
  107. const server = new ViteNodeServer(viteServer, {
  108. debug: {
  109. dumpModules: true,
  110. loadDumppedModules: true,
  111. }
  112. })
  113. ```
  114. ### Debug Execution
  115. If the process get stuck, it might because there is a unresolvable circular dependencies, you can set `VITE_NODE_DEBUG_RUNNER=true` to vite-node warn about it.
  116. ```bash
  117. VITE_NODE_DEBUG_RUNNER=true vite-node example.ts
  118. ```
  119. Or programmatically:
  120. ```js
  121. import { ViteNodeRunner } from 'vite-node/client'
  122. const runner = new ViteNodeRunner({
  123. debug: true
  124. })
  125. ```
  126. ## Credits
  127. Based 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).
  128. Thanks [@brillout](https://github.com/brillout) for kindly sharing this package name.
  129. ## Sponsors
  130. <p align="center">
  131. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  132. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  133. </a>
  134. </p>
  135. ## License
  136. [MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)