Skip to content

Commit

Permalink
test: add tests for inlineImport
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 14, 2024
1 parent f9547e3 commit 14846b5
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Test {
field: true
}

export const test: Test = {
field: true,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Plugin } from 'vite'

export default function testPlugin(): Plugin {
return {
name: 'test',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import plugin from './plugin'

export default defineConfig({
root: './test',
plugins: [plugin()],
})
40 changes: 40 additions & 0 deletions packages/vite/src/node/__tests__/inlineImport.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { resolve } from 'node:path'
import { describe, expect, test } from 'vitest'
import { inlineImport } from '../ssr/inlineImport'

describe('importing files using inlined environment', () => {
const fixture = (name: string) =>
resolve(import.meta.dirname, './fixtures/inline-import', name)

test('importing a basic file works', async () => {
const { module } = await inlineImport<
typeof import('./fixtures/inline-import/basic')
>(fixture('basic'))
expect(module.test).toEqual({
field: true,
})
})

test("cannot import cjs, 'inlineImport' doesn't support CJS syntax at all", async () => {
await expect(() =>
inlineImport<typeof import('./fixtures/inline-import/basic')>(
fixture('cjs.js'),
),
).rejects.toThrow('module is not defined')
})

test('can import vite config', async () => {
const { module, dependencies } = await inlineImport<
typeof import('./fixtures/inline-import/vite.config')
>(fixture('vite.config'))
expect(module.default).toEqual({
root: './test',
plugins: [
{
name: 'test',
},
],
})
expect(dependencies).toEqual([fixture('plugin.ts')])
})
})
6 changes: 4 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import type { ResolvedSSROptions, SSROptions } from './ssr'
import { resolveSSROptions, ssrConfigDefaults } from './ssr'
import { PartialEnvironment } from './baseEnvironment'
import { createIdResolver } from './idResolver'
import { inlineImport } from './ssr/import'
import { inlineImport } from './ssr/inlineImport'

const debug = createDebugger('vite:config', { depth: 10 })
const promisifiedRealpath = promisify(fs.realpath)
Expand Down Expand Up @@ -1697,7 +1697,9 @@ export async function loadConfigFromFile(
}

async function importConfigFile(resolvedPath: string) {
const { module, dependencies } = await inlineImport(resolvedPath)
const { module, dependencies } = await inlineImport<{
default: UserConfigExport
}>(resolvedPath, { configFile: false })
return {
configExport: module.default,
dependencies,
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
DevEnvironment,
type DevEnvironmentContext,
} from './server/environment'
export { inlineImport } from './ssr/inlineImport'
export { BuildEnvironment } from './build'

export { fetchModule, type FetchModuleOptions } from './ssr/fetchModule'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import type { InlineConfig } from '../config'
import { resolveConfig } from '../config'
import { createRunnableDevEnvironment } from '../server/environments/runnableEnvironment'
import { mergeConfig } from '../utils'

interface InlineImportResult {
module: any
interface InlineImportResult<T> {
module: T
dependencies: string[]
}

export async function inlineImport(
/**
* Import any file using the default Vite environment.
* @experimental
*/
export async function inlineImport<T>(
moduleId: string,
): Promise<InlineImportResult> {
inlineConfig?: InlineConfig,
): Promise<InlineImportResult<T>> {
const environment = createRunnableDevEnvironment(
'inline',
// TODO: provide a dummy config?
await resolveConfig(
{
configFile: false,
mergeConfig(inlineConfig || {}, {
environments: {
inline: {
consumer: 'server',
Expand All @@ -26,7 +32,7 @@ export async function inlineImport(
},
},
},
},
}),
'serve',
),
{
Expand All @@ -42,16 +48,18 @@ export async function inlineImport(
try {
const module = await environment.runner.import(moduleId)
const modules = [
...environment.runner.evaluatedModules.fileToModulesMap.entries(),
...environment.runner.evaluatedModules.urlToIdModuleMap.values(),
]
const dependencies = modules
.filter(([file, modules]) => {
const isExternal = [...modules].every(
(m) => !m.meta || 'externalize' in m.meta,
)
return !isExternal && file !== moduleId
.filter((m) => {
// ignore all externalized modules
if (!m.meta || 'externalize' in m.meta) {
return false
}
// ignore the current module
return m.exports !== module
})
.map(([file]) => file)
.map((m) => m.file)
return {
module,
dependencies,
Expand Down

0 comments on commit 14846b5

Please sign in to comment.