Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/src/dynamic-import-with-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function loadModule() {
const module = await import('./example.data')
// eslint-disable-next-line no-console
console.log('Import succeeded from', module.id)
}

loadModule()
1 change: 1 addition & 0 deletions test/src/example.data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!
6 changes: 6 additions & 0 deletions test/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@
const result = await runViteNodeCli('--root', root, resolve(root, 'main.ts'))
await result.viteNode.waitForStdout('[ok]')
})

it('dynamic import with a loading plugin', async () => {
const cli = await runViteNodeCli(resolve(import.meta.dirname, '../src/dynamic-import-with-loader.js'))
expect(cli.stderr).toBe('')

Check failure on line 90 in test/test/cli.test.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/test/cli.test.ts > dynamic import with a loading plugin

AssertionError: expected 'file:///home/runner/work/vite-node/vi…' to be '' // Object.is equality - Expected + Received + file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168 + const err$2 = /* @__PURE__ */ new Error("The server is being restarted or closed. Request is outdated"); + ^ + + Error: The server is being restarted or closed. Request is outdated + at throwClosedServerError (file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168:32) + at EnvironmentPluginContainer.resolveId (file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29300:65) + at EnvironmentModuleGraph._resolveUrl (file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35276:39) + at EnvironmentModuleGraph.getModuleByUrl (file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35084:19) + at doTransform (file:///home/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:23191:17) { + code: 'ERR_CLOSED_SERVER' + } + + Node.js v24.11.0 + ❯ test/test/cli.test.ts:90:22

Check failure on line 90 in test/test/cli.test.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/test/cli.test.ts > dynamic import with a loading plugin

AssertionError: expected 'file:///Users/runner/work/vite-node/v…' to be '' // Object.is equality - Expected + Received + file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168 + const err$2 = /* @__PURE__ */ new Error("The server is being restarted or closed. Request is outdated"); + ^ + + Error: The server is being restarted or closed. Request is outdated + at throwClosedServerError (file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168:32) + at EnvironmentPluginContainer.resolveId (file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29300:65) + at EnvironmentModuleGraph._resolveUrl (file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35276:39) + at EnvironmentModuleGraph.getModuleByUrl (file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35084:19) + at doTransform (file:///Users/runner/work/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:23191:17) { + code: 'ERR_CLOSED_SERVER' + } + + Node.js v24.11.0 + ❯ test/test/cli.test.ts:90:22

Check failure on line 90 in test/test/cli.test.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/test/cli.test.ts > dynamic import with a loading plugin

AssertionError: expected 'file:///D:/a/vite-node/vite-node/node…' to be '' // Object.is equality - Expected + Received + file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168 + const err$2 = /* @__PURE__ */ new Error("The server is being restarted or closed. Request is outdated"); + ^ + + Error: The server is being restarted or closed. Request is outdated + at throwClosedServerError (file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29168:32) + at EnvironmentPluginContainer.resolveId (file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:29300:65) + at EnvironmentModuleGraph._resolveUrl (file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35276:39) + at EnvironmentModuleGraph.getModuleByUrl (file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:35084:19) + at doTransform (file:///D:/a/vite-node/vite-node/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/chunks/config.js:23191:17) { + code: 'ERR_CLOSED_SERVER' + } + + Node.js v24.11.0 + ❯ test/test/cli.test.ts:90:22
expect(cli.stdout).toMatch(/Import succeeded from \S*\/test\/src\/example.data\n/)
})
21 changes: 21 additions & 0 deletions test/test/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { PluginOption } from 'vite'
import fs from 'node:fs/promises'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [dataLoader()],
})

// Plugin that transforms .data files into string exports
function dataLoader(): PluginOption {
return {
name: 'data-loader',
async load(id) {
if (id.endsWith('.data')) {
const text = await fs.readFile(id, 'utf8')
return `export const id = ${JSON.stringify(id)};\n`
+ `export const text = ${JSON.stringify(text)};`
}
},
}
}
Loading