-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
feat: use module runner to import the config #18637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
547d1c7
2454b66
1cf3da7
df1efe3
e55e083
c9e908e
b4c41d3
7c5dcf0
aad578d
d8f8771
0d16f85
f295303
d453876
65578af
df7d928
001f7e9
1adc486
5ecd529
275a593
b383651
14571ac
d847961
07867ae
646a069
9b2179a
8669489
19c5019
e06731d
a960fd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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,5 @@ | ||
| import parent from 'parent' | ||
|
|
||
| export default { | ||
| __injected: parent.child, | ||
| } |
| 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()], | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { resolve } from 'node:path' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { loadConfigFromFile } from 'vite' | ||
| import { inlineImport } from '../ssr/inlineImport' | ||
| import { slash } from '../../shared/utils' | ||
|
|
||
| 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([slash(fixture('plugin.ts'))]) | ||
| }) | ||
|
|
||
| test('can import vite config that imports a TS external module', async () => { | ||
| const { module, dependencies } = await inlineImport< | ||
| typeof import('./fixtures/inline-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/inline-import') | ||
| await loadConfigFromFile( | ||
| { mode: 'production', command: 'serve' }, | ||
| resolve(root, './vite.config.outside-pkg-import.mts'), | ||
| root, | ||
| 'silent', | ||
| ) | ||
| }).rejects.toThrow('Unknown file extension ".ts"') | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "child", | ||
| "type": "module", | ||
| "main": "./index.js" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // @ts-expect-error not typed | ||
| import child from 'child' | ||
|
|
||
| export default { | ||
| child, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "name": "parent", | ||
| "type": "module", | ||
| "main": "./index.ts", | ||
| "dependencies": { | ||
| "child": "link:../child" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ interface GlobalCLIOptions { | |
| l?: LogLevel | ||
| logLevel?: LogLevel | ||
| clearScreen?: boolean | ||
| configLoader?: 'bundle' | 'runner' | ||
| d?: boolean | string | ||
| debug?: boolean | string | ||
| f?: string | ||
|
|
@@ -87,6 +88,7 @@ function cleanGlobalCLIOptions<Options extends GlobalCLIOptions>( | |
| delete ret.l | ||
| delete ret.logLevel | ||
| delete ret.clearScreen | ||
| delete ret.configLoader | ||
| delete ret.d | ||
| delete ret.debug | ||
| delete ret.f | ||
|
|
@@ -151,6 +153,10 @@ cli | |
| }) | ||
| .option('-l, --logLevel <level>', `[string] info | warn | error | silent`) | ||
| .option('--clearScreen', `[boolean] allow/disable clear screen when logging`) | ||
| .option( | ||
| '--configLoader <loader>', | ||
| `[string] use 'bundle' to bundle the config with esbuild or 'runner' (experimental) to process it on the fly (default: bundle)`, | ||
| ) | ||
|
Comment on lines
+156
to
+159
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to flag this experimental? I guess it's fine not to, but just to confirm.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can mark it as experimental since the environment API is experimental 😄
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marked it like this: - [string] use 'bundle' to bundle the config with esbuild or 'runner' to process it on the fly (default: bundle)
+ [string] use 'bundle' to bundle the config with esbuild or 'runner' (experimental) to process it on the fly (default: bundle) |
||
| .option('-d, --debug [feat]', `[string | boolean] show debug logs`) | ||
| .option('-f, --filter <filter>', `[string] filter debug logs`) | ||
| .option('-m, --mode <mode>', `[string] set env mode`) | ||
|
|
@@ -180,6 +186,7 @@ cli | |
| base: options.base, | ||
| mode: options.mode, | ||
| configFile: options.config, | ||
| configLoader: options.configLoader, | ||
| logLevel: options.logLevel, | ||
| clearScreen: options.clearScreen, | ||
| optimizeDeps: { force: options.force }, | ||
|
|
@@ -304,6 +311,7 @@ cli | |
| base: options.base, | ||
| mode: options.mode, | ||
| configFile: options.config, | ||
| configLoader: options.configLoader, | ||
| logLevel: options.logLevel, | ||
| clearScreen: options.clearScreen, | ||
| build: buildOptions, | ||
|
|
@@ -340,6 +348,7 @@ cli | |
| root, | ||
| base: options.base, | ||
| configFile: options.config, | ||
| configLoader: options.configLoader, | ||
| logLevel: options.logLevel, | ||
| mode: options.mode, | ||
| }, | ||
|
|
@@ -382,6 +391,7 @@ cli | |
| root, | ||
| base: options.base, | ||
| configFile: options.config, | ||
| configLoader: options.configLoader, | ||
| logLevel: options.logLevel, | ||
| mode: options.mode, | ||
| build: { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.