-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use module runner to import the config (#18637)
Co-authored-by: bluwy <[email protected]> Co-authored-by: Hiroshi Ogawa <[email protected]>
- Loading branch information
1 parent
93d5443
commit b7e0e42
Showing
23 changed files
with
357 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/vite/src/node/__tests__/fixtures/runner-import/basic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
interface Test { | ||
field: true | ||
} | ||
|
||
export const test: Test = { | ||
field: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/runner-import/dynamic-import-dep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'ok' |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/runner-import/dynamic-import.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => import('./dynamic-import-dep') |
7 changes: 7 additions & 0 deletions
7
packages/vite/src/node/__tests__/fixtures/runner-import/plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/vite/src/node/__tests__/fixtures/runner-import/vite.config.outside-pkg-import.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import parent from '@vitejs/parent' | ||
|
||
export default { | ||
__injected: parent.child, | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/vite/src/node/__tests__/fixtures/runner-import/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "@vitejs/child", | ||
"type": "module", | ||
"main": "./index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-expect-error not typed | ||
import child from '@vitejs/child' | ||
|
||
export default { | ||
child, | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/vite/src/node/__tests__/packages/parent/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@vitejs/parent", | ||
"type": "module", | ||
"main": "./index.ts", | ||
"dependencies": { | ||
"@vitejs/child": "link:../child" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { resolve } from 'node:path' | ||
import { describe, expect, test } from 'vitest' | ||
import { loadConfigFromFile } from 'vite' | ||
import { runnerImport } from '../ssr/runnerImport' | ||
import { slash } from '../../shared/utils' | ||
|
||
describe('importing files using inlined environment', () => { | ||
const fixture = (name: string) => | ||
resolve(import.meta.dirname, './fixtures/runner-import', name) | ||
|
||
test('importing a basic file works', async () => { | ||
const { module } = await runnerImport< | ||
typeof import('./fixtures/runner-import/basic') | ||
>(fixture('basic')) | ||
expect(module.test).toEqual({ | ||
field: true, | ||
}) | ||
}) | ||
|
||
test("cannot import cjs, 'runnerImport' doesn't support CJS syntax at all", async () => { | ||
await expect(() => | ||
runnerImport<typeof import('./fixtures/runner-import/basic')>( | ||
fixture('cjs.js'), | ||
), | ||
).rejects.toThrow('module is not defined') | ||
}) | ||
|
||
test('can import vite config', async () => { | ||
const { module, dependencies } = await runnerImport< | ||
typeof import('./fixtures/runner-import/vite.config') | ||
>(fixture('vite.config')) | ||
expect(module.default).toEqual({ | ||
root: './test', | ||
plugins: [ | ||
{ | ||
name: 'test', | ||
}, | ||
], | ||
}) | ||
expect(dependencies).toEqual([slash(fixture('plugin.ts'))]) | ||
}) | ||
|
||
test('can import vite config that imports a TS external module', async () => { | ||
const { module, dependencies } = await runnerImport< | ||
typeof import('./fixtures/runner-import/vite.config.outside-pkg-import.mjs') | ||
>(fixture('vite.config.outside-pkg-import.mts')) | ||
|
||
expect(module.default.__injected).toBe(true) | ||
expect(dependencies).toEqual([ | ||
slash(resolve(import.meta.dirname, './packages/parent/index.ts')), | ||
]) | ||
|
||
// confirm that it fails with a bundle approach | ||
await expect(async () => { | ||
const root = resolve(import.meta.dirname, './fixtures/runner-import') | ||
await loadConfigFromFile( | ||
{ mode: 'production', command: 'serve' }, | ||
resolve(root, './vite.config.outside-pkg-import.mts'), | ||
root, | ||
'silent', | ||
) | ||
}).rejects.toThrow('Unknown file extension ".ts"') | ||
}) | ||
|
||
test('dynamic import', async () => { | ||
const { module } = await runnerImport<any>(fixture('dynamic-import.ts')) | ||
await expect(() => module.default()).rejects.toMatchInlineSnapshot( | ||
`[Error: Vite module runner has been closed.]`, | ||
) | ||
// const dep = await module.default(); | ||
// expect(dep.default).toMatchInlineSnapshot(`"ok"`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.