Skip to content

Commit adda4cd

Browse files
authored
feat!: migrate back from Vitest repo (#5)
2 parents 1bc8749 + 91f86c2 commit adda4cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+9749
-2934
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: pnpm/action-setup@v4
18+
with:
19+
run_install: false
20+
- uses: actions/setup-node@v4
21+
with:
22+
node-version: lts/*
23+
cache: pnpm
24+
25+
- run: pnpm i -g @antfu/ni
26+
- run: nci
27+
- run: nr lint
28+
- run: nr typecheck
29+
30+
test:
31+
runs-on: ${{ matrix.os }}
32+
33+
strategy:
34+
matrix:
35+
node: [lts/*]
36+
os: [ubuntu-latest, windows-latest, macos-latest]
37+
fail-fast: false
38+
39+
steps:
40+
- uses: actions/checkout@v4
41+
- uses: pnpm/action-setup@v4
42+
with:
43+
run_install: false
44+
- name: Set node ${{ matrix.node }}
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: ${{ matrix.node }}
48+
cache: pnpm
49+
50+
- run: pnpm i -g @antfu/ni
51+
- run: nci
52+
- run: nr build
53+
- run: nr test

.github/workflows/release.yml

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@ on:
77

88
jobs:
99
release:
10-
runs-on: ubuntu-latest
11-
steps:
12-
- uses: actions/checkout@v2
13-
14-
# - name: Install pnpm
15-
# uses: pnpm/action-setup@v2.0.1
16-
# with:
17-
# version: 6.15.1
18-
19-
- name: Set node version to ${{ matrix.node_version }}
20-
uses: actions/setup-node@v2
21-
with:
22-
node-version: ${{ matrix.node_version }}
23-
# cache: "pnpm"
24-
25-
# - run: pnpm i -g @antfu/ni
26-
# - run: nci
27-
# - run: nr test --if-present
28-
# - run: npm publish --access public
29-
# env:
30-
# NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
31-
32-
- run: npx conventional-github-releaser -p angular
33-
env:
34-
CONVENTIONAL_GITHUB_RELEASER_TOKEN: ${{secrets.GITHUB_TOKEN}}
10+
uses: sxzz/workflows/.github/workflows/release.yml@v1
11+
with:
12+
publish: true
13+
permissions:
14+
contents: write
15+
id-token: write

.github/workflows/test.yml

Lines changed: 0 additions & 46 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Anthony Fu <https://github.com/antfu>
3+
Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 154 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
# Project Moved to [Vitest Monorepo](https://github.com/vitest-dev/vitest/tree/main/packages/vite-node)
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>
24

3-
<details>
5+
<h1 align="center">
6+
vite-node
7+
</h1>
8+
<p align="center">
9+
Vite as Node runtime.<br>The engine that powers <a href="https://github.com/nuxt/nuxt">Nuxt 3 Dev SSR</a> and <i><a href="https://github.com/vitest-dev/vitest/pull/8208">used to</a></i> power <a href="https://github.com/vitest-dev/vitest">Vitest</a>.
410

5-
# vite-node
11+
<p>
12+
<p align="center">
13+
<a href="https://www.npmjs.com/package/vite-node"><img src="https://img.shields.io/npm/v/vite-node?color=FCC72B&label="></a>
14+
<p>
615

7-
[![NPM version](https://img.shields.io/npm/v/vite-node?color=a1b858&label=)](https://www.npmjs.com/package/vite-node)
16+
> [!NOTE]
17+
> This project is firstly inspired by [Nuxt 3's SSR](https://antfu.me/posts/dev-ssr-on-nuxt) implementation made by [@pi0](https://github.com/pi0), as a PoC. Later, it made [Vitest](https://github.com/vitest-dev/vitest) possible by providing the same pipeline as in Vite. It served the ecosystem well for a few years and later became a more generalized built-in solution as [Vite Environment Module Runner](https://vite.dev/guide/api-environment.html). Vitest has [migrated to the new official solution](https://github.com/vitest-dev/vitest/pull/8208), which means `vite-node` has finished its mission. We will still keep it around for the ecosystem that built around it, but for new projects, please consider using the builtin Vite one instead.
818
9-
Vite as Node runtime.
19+
## Features
1020

11-
> **EXPERIMENTAL**
21+
- On-demand evaluation
22+
- Vite's pipeline, plugins, resolve, aliasing
23+
- Out-of-box ESM & TypeScript support
24+
- Respect `vite.config.ts`
25+
- Hot module replacement (HMR)
26+
- Separate server/client architecture
27+
- Top-level `await`
28+
- Shims for `__dirname` and `__filename` in ESM
29+
- Access to native node modules like `fs`, `path`, etc.
1230

31+
## CLI Usage
1332

14-
## Usage
33+
Run JS/TS file on Node.js using Vite's resolvers and transformers.
1534

1635
```bash
1736
npx vite-node index.ts
@@ -23,29 +42,141 @@ Options:
2342
npx vite-node -h
2443
```
2544

26-
## Features
45+
### Options via CLI
2746

28-
- Out-of-box ESM & TypeScript support (possible for more with plugins)
29-
- Top-level await
30-
- Vite plugins, resolve, aliasing
31-
- Respect `vite.config.ts`
32-
- Shims for `__dirname` and `__filename` in ESM
33-
- Access to native node modules like `fs`, `path`, etc.
34-
- Watch mode (like `nodemon`)
47+
[All `ViteNodeServer` options](https://github.com/antfu-collective/vite-node/blob/main/src/types.ts#L92-L111) are supported by the CLI. They may be defined through the dot syntax, as shown below:
3548

36-
## When NOT to Use
49+
```bash
50+
npx vite-node --options.deps.inline="module-name" --options.deps.external="/module-regexp/" index.ts
51+
```
52+
53+
Note that for options supporting RegExps, strings passed to the CLI must start _and_ end with a `/`;
3754

38-
- Production, yet - in very early stage, check it later
39-
- Most of the time, when other tools can do that job
40-
- We need to start a Vite server upon each execution, which inevitably introduces some overhead. Only use it when you want the same behavior as Vite or the powerful plugins system (for example, testing components with a Vite-specific setup).
55+
### Hashbang
4156

42-
## Why?
57+
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).
4358

44-
It runs Vite's id resolving, module transforming, and most importantly, the powerful plugins system!
59+
Simply add `#!/usr/bin/env vite-node --script` at the top of your file:
4560

46-
## How?
61+
_file.ts_
62+
63+
```ts
64+
#!/usr/bin/env vite-node --script
65+
66+
console.log('argv:', process.argv.slice(2))
67+
```
4768

48-
It fires up a Vite dev server, transforms the requests, and runs them in Node.
69+
And make the file executable:
70+
71+
```sh
72+
chmod +x ./file.ts
73+
```
74+
75+
Now, you can run the file without passing it into Vite Node:
76+
77+
```sh
78+
$ ./file.ts hello
79+
argv: [ 'hello' ]
80+
```
81+
82+
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.
83+
84+
## Programmatic Usage
85+
86+
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
87+
88+
```ts
89+
import { createServer, version as viteVersion } from 'vite'
90+
import { ViteNodeRunner } from 'vite-node/client'
91+
import { ViteNodeServer } from 'vite-node/server'
92+
import { installSourcemapsSupport } from 'vite-node/source-map'
93+
94+
// create vite server
95+
const server = await createServer({
96+
optimizeDeps: {
97+
// It's recommended to disable deps optimization
98+
noDiscovery: true,
99+
include: undefined,
100+
},
101+
})
102+
103+
// For old Vite, this is needed to initialize the plugins.
104+
if (Number(viteVersion.split('.')[0]) < 6) {
105+
await server.pluginContainer.buildStart({})
106+
}
107+
108+
// create vite-node server
109+
const node = new ViteNodeServer(server)
110+
111+
// fixes stacktraces in Errors
112+
installSourcemapsSupport({
113+
getSourceMap: source => node.getSourceMap(source),
114+
})
115+
116+
// create vite-node runner
117+
const runner = new ViteNodeRunner({
118+
root: server.config.root,
119+
base: server.config.base,
120+
// when having the server and runner in a different context,
121+
// you will need to handle the communication between them
122+
// and pass to this function
123+
fetchModule(id) {
124+
return node.fetchModule(id)
125+
},
126+
resolveId(id, importer) {
127+
return node.resolveId(id, importer)
128+
},
129+
})
130+
131+
// execute the file
132+
await runner.executeFile('./example.ts')
133+
134+
// close the vite server
135+
await server.close()
136+
```
137+
138+
## Debugging
139+
140+
### Debug Transformation
141+
142+
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`.
143+
144+
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 dumped files and use them for executing.
145+
146+
```bash
147+
VITE_NODE_DEBUG_DUMP=load vite-node example.ts
148+
```
149+
150+
Or programmatically:
151+
152+
```js
153+
import { ViteNodeServer } from 'vite-node/server'
154+
155+
const server = new ViteNodeServer(viteServer, {
156+
debug: {
157+
dumpModules: true,
158+
loadDumppedModules: true,
159+
},
160+
})
161+
```
162+
163+
### Debug Execution
164+
165+
If the process gets stuck, it might be because there are unresolvable circular dependencies. You can set `VITE_NODE_DEBUG_RUNNER=true` for vite-node to warn about this.
166+
167+
```bash
168+
VITE_NODE_DEBUG_RUNNER=true vite-node example.ts
169+
```
170+
171+
Or programmatically:
172+
173+
```js
174+
import { ViteNodeRunner } from 'vite-node/client'
175+
176+
const runner = new ViteNodeRunner({
177+
debug: true,
178+
})
179+
```
49180

50181
## Credits
51182

@@ -64,5 +195,3 @@ Thanks [@brillout](https://github.com/brillout) for kindly sharing this package
64195
## License
65196

66197
[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)
67-
68-
</details>

assets/vite-node.svg

Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)