vite-node 0.1.2 → 0.1.6

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/cli.mjs ADDED
@@ -0,0 +1,54 @@
1
+ /* eslint-disable no-console */
2
+ import minimist from 'minimist'
3
+ import { red, dim } from 'kolorist'
4
+ import { startAndRun } from './index.mjs'
5
+
6
+ const argv = minimist(process.argv.slice(2), {
7
+ 'alias': {
8
+ r: 'root',
9
+ c: 'config',
10
+ h: 'help',
11
+ w: 'watch',
12
+ s: 'silent',
13
+ },
14
+ '--': true,
15
+ 'string': ['root', 'config'],
16
+ 'boolean': ['help', 'vue', 'watch', 'silent'],
17
+ unknown(name) {
18
+ if (name[0] === '-') {
19
+ console.error(red(`Unknown argument: ${name}`))
20
+ help()
21
+ process.exit(1)
22
+ }
23
+ },
24
+ })
25
+
26
+ if (argv.help) {
27
+ help()
28
+ process.exit(0)
29
+ }
30
+
31
+ if (!argv._.length) {
32
+ console.error(red('No files specified.'))
33
+ help()
34
+ process.exit(1)
35
+ }
36
+
37
+ // forward argv
38
+ process.argv = [process.argv.slice(0, 2), ...argv['--']]
39
+
40
+ startAndRun(argv)
41
+
42
+ function help() {
43
+ console.log(`
44
+ Usage:
45
+ $ vite-node [options] [files]
46
+
47
+ Options:
48
+ -r, --root <path> ${dim('[string]')} use specified root directory
49
+ -c, --config <file> ${dim('[string]')} use specified config file
50
+ -w, --watch ${dim('[boolean]')} restart on file changes, similar to "nodemon"
51
+ -s, --silent ${dim('[boolean]')} do not emit errors and logs
52
+ --vue ${dim('[boolean]')} support for importing Vue component
53
+ `)
54
+ }
package/index.mjs CHANGED
@@ -5,73 +5,61 @@ import { dirname, resolve, relative } from 'path'
5
5
  import vm from 'vm'
6
6
  import { createServer } from 'vite'
7
7
  import createDebug from 'debug'
8
- import minimist from 'minimist'
9
8
  import { red, dim, yellow, green, inverse, cyan } from 'kolorist'
10
9
 
11
- const argv = minimist(process.argv.slice(2), {
12
- 'alias': {
13
- r: 'root',
14
- c: 'config',
15
- h: 'help',
16
- w: 'watch',
17
- s: 'silent',
18
- },
19
- '--': true,
20
- 'string': ['root', 'config'],
21
- 'boolean': ['help', 'vue', 'watch', 'silent'],
22
- unknown(name) {
23
- if (name[0] === '-') {
24
- console.error(red(`Unknown argument: ${name}`))
25
- help()
26
- process.exit(1)
27
- }
28
- },
29
- })
30
- const files = argv._
10
+ const debugRequest = createDebug('vite-node:request')
11
+ const debugTransform = createDebug('vite-node:transform')
31
12
 
32
- if (argv.help) {
33
- help()
34
- process.exit(0)
35
- }
13
+ let executing = false
36
14
 
37
- if (!argv._.length) {
38
- console.error(red('No files specified.'))
39
- help()
40
- process.exit(1)
41
- }
15
+ export async function startAndRun(argv) {
16
+ function log(...args) {
17
+ if (argv.silent)
18
+ return
19
+ console.log(...args)
20
+ }
42
21
 
43
- // forward argv
44
- process.argv = [process.argv.slice(0, 2), ...argv['--']]
22
+ const root = argv.root || process.cwd()
23
+ process.chdir(root)
24
+
25
+ const files = argv.files || argv._
26
+
27
+ const server = await createServer({
28
+ logLevel: 'error',
29
+ clearScreen: false,
30
+ configFile: argv.config,
31
+ root,
32
+ resolve: argv.vue
33
+ ? {
34
+ alias: {
35
+ // fix for Vue does not support mjs yet
36
+ 'vue/server-renderer': 'vue/server-renderer',
37
+ 'vue/compiler-sfc': 'vue/compiler-sfc',
38
+ '@vue/reactivity': '@vue/reactivity/dist/reactivity.cjs.js',
39
+ '@vue/shared': '@vue/shared/dist/shared.cjs.js',
40
+ 'vue-router': 'vue-router/dist/vue-router.cjs.js',
41
+ 'vue': 'vue/dist/vue.cjs.js',
42
+ },
43
+ }
44
+ : {},
45
+ })
46
+ await server.pluginContainer.buildStart({})
45
47
 
46
- const debugRequest = createDebug('vite-node:request')
47
- const debugTransform = createDebug('vite-node:transform')
48
+ await run(files, server, argv)
48
49
 
49
- const root = argv.root || process.cwd()
50
- process.chdir(root)
51
-
52
- const server = await createServer({
53
- logLevel: 'error',
54
- clearScreen: false,
55
- configFile: argv.config,
56
- root,
57
- resolve: argv.vue
58
- ? {
59
- alias: {
60
- // fix for Vue does not support mjs yet
61
- 'vue/server-renderer': 'vue/server-renderer',
62
- 'vue/compiler-sfc': 'vue/compiler-sfc',
63
- '@vue/reactivity': '@vue/reactivity/dist/reactivity.cjs.js',
64
- '@vue/shared': '@vue/shared/dist/shared.cjs.js',
65
- 'vue-router': 'vue-router/dist/vue-router.cjs.js',
66
- 'vue': 'vue/dist/vue.cjs.js',
67
- },
68
- }
69
- : {},
70
- })
71
- await server.pluginContainer.buildStart({})
72
- let executing = false
50
+ if (argv.watch) {
51
+ log(inverse(cyan(' vite node ')), cyan('watch mode enabled\n'))
73
52
 
74
- async function run() {
53
+ server.watcher.on('change', (file) => {
54
+ if (!executing) {
55
+ log(inverse(yellow(' vite node ')), yellow(`${file} changed, restarting...\n`))
56
+ run()
57
+ }
58
+ })
59
+ }
60
+ }
61
+
62
+ async function run(files, server, argv) {
75
63
  process.exitCode = 0
76
64
  executing = true
77
65
  let err
@@ -88,6 +76,12 @@ async function run() {
88
76
  executing = false
89
77
  }
90
78
 
79
+ function log(...args) {
80
+ if (argv.silent)
81
+ return
82
+ console.log(...args)
83
+ }
84
+
91
85
  if (argv.watch) {
92
86
  setTimeout(() => {
93
87
  if (err || process.exitCode)
@@ -101,21 +95,6 @@ async function run() {
101
95
  }
102
96
  }
103
97
 
104
- if (argv.watch) {
105
- log(inverse(cyan(' vite node ')), cyan('watch mode enabled\n'))
106
-
107
- server.watcher.on('change', (file) => {
108
- if (!executing) {
109
- log(inverse(yellow(' vite node ')), yellow(`${file} changed, restarting...\n`))
110
- run()
111
- }
112
- })
113
- }
114
-
115
- await run(files, server, argv)
116
-
117
- // --- CLI END ---
118
-
119
98
  function normalizeId(id) {
120
99
  // Virtual modules start with `\0`
121
100
  if (id && id.startsWith('/@id/__x00__'))
@@ -125,7 +104,7 @@ function normalizeId(id) {
125
104
  return id
126
105
  }
127
106
 
128
- function toFilePath(id) {
107
+ function toFilePath(id, server) {
129
108
  const absolute = id.startsWith('/@fs/')
130
109
  ? id.slice(4)
131
110
  : slash(resolve(server.config.root, id.slice(1)))
@@ -149,7 +128,7 @@ async function execute(files, server) {
149
128
  const request = async(dep) => {
150
129
  if (callstack.includes(dep)) {
151
130
  throw new Error(`${red('Circular dependency detected')}\nStack:\n${[...callstack, dep].reverse().map((i) => {
152
- const path = relative(server.config.root, toFilePath(normalizeId(i)))
131
+ const path = relative(server.config.root, toFilePath(normalizeId(i), server))
153
132
  return dim(' -> ') + (i === dep ? yellow(path) : path)
154
133
  }).join('\n')}\n`)
155
134
  }
@@ -157,7 +136,7 @@ async function execute(files, server) {
157
136
  }
158
137
 
159
138
  const id = normalizeId(rawId)
160
- const absolute = toFilePath(id)
139
+ const absolute = toFilePath(id, server)
161
140
 
162
141
  debugRequest(absolute)
163
142
 
@@ -225,23 +204,3 @@ async function execute(files, server) {
225
204
  function slash(path) {
226
205
  return path.replace(/\\/g, '/')
227
206
  }
228
-
229
- function help() {
230
- console.log(`
231
- Usage:
232
- $ vite-node [options] [files]
233
-
234
- Options:
235
- -r, --root <path> ${dim('[string]')} use specified root directory
236
- -c, --config <file> ${dim('[string]')} use specified config file
237
- -w, --watch ${dim('[boolean]')} restart on file changes, similar to "nodemon"
238
- -s, --silent ${dim('[boolean]')} do not emit errors and logs
239
- --vue ${dim('[boolean]')} support for importing Vue component
240
- `)
241
- }
242
-
243
- function log(...args) {
244
- if (argv.silent)
245
- return
246
- console.log(...args)
247
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.1.2",
3
+ "version": "0.1.6",
4
4
  "description": "Vite as Node runtime",
5
5
  "keywords": [
6
6
  "vite"
@@ -20,17 +20,21 @@
20
20
  "type": "module",
21
21
  "exports": {
22
22
  ".": {
23
- "import": "./index.mjs"
23
+ "import": "./index.mjs",
24
+ "types": "./index.d.ts"
25
+ },
26
+ "./cli": {
27
+ "import": "./cli.mjs"
24
28
  }
25
29
  },
26
- "main": "index.mjs",
27
- "module": "index.mjs",
30
+ "main": "./index.mjs",
31
+ "module": "./index.mjs",
32
+ "types": "./index.d.ts",
28
33
  "bin": {
29
- "vite-node": "./bin/vite-node.js"
34
+ "vite-node": "./vite-node.mjs"
30
35
  },
31
36
  "files": [
32
- "index.mjs",
33
- "bin"
37
+ "*.mjs"
34
38
  ],
35
39
  "dependencies": {
36
40
  "debug": "^4.3.3",
@@ -57,7 +61,7 @@
57
61
  "scripts": {
58
62
  "lint": "eslint \"**/*.{ts,mjs}\"",
59
63
  "release": "bumpp --commit --push --tag && pnpm publish",
60
- "start": "DEBUG=vite-node:* node index.mjs",
61
- "test": "node index.mjs -c test/vite.config.ts --vue test/index.test.mjs"
64
+ "start": "DEBUG=vite-node:* node cli.mjs",
65
+ "test": "node cli.mjs -c test/vite.config.ts --vue test/index.test.mjs"
62
66
  }
63
67
  }
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict'
3
3
 
4
- import '../index.mjs'
4
+ import './cli.mjs'