Skip to content
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

test: use isPreview instead of previewBase #14878

Merged
merged 1 commit into from
Nov 6, 2023
Merged
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
9 changes: 3 additions & 6 deletions playground/assets/vite.config-relative-base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'

export default defineConfig({
export default defineConfig(({ isPreview }) => ({
...baseConfig,
base: './', // relative base to make dist portable
base: !isPreview ? './' : '/relative-base/', // relative base to make dist portable
build: {
...baseConfig.build,
outDir: 'dist/relative-base',
Expand All @@ -18,8 +18,5 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
}))
9 changes: 3 additions & 6 deletions playground/css/vite.config-relative-base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'

export default defineConfig({
export default defineConfig(({ isPreview }) => ({
...baseConfig,
base: './', // relative base to make dist portable
base: !isPreview ? './' : '/relative-base/', // relative base to make dist portable
build: {
...baseConfig.build,
outDir: 'dist/relative-base',
Expand All @@ -18,8 +18,5 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
}))
9 changes: 3 additions & 6 deletions playground/legacy/vite.config-no-polyfills-no-systemjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/no-polyfills-no-systemjs/',
plugins: [
legacy({
renderModernChunks: false,
Expand All @@ -25,7 +25,4 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/no-polyfills-no-systemjs/',
},
})
}))
9 changes: 3 additions & 6 deletions playground/legacy/vite.config-no-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/no-polyfills/',
plugins: [
legacy({
renderModernChunks: false,
Expand All @@ -24,7 +24,4 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/no-polyfills/',
},
})
}))
51 changes: 22 additions & 29 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,9 @@ beforeAll(async (s) => {
}
})

export async function startDefaultServe(): Promise<void> {
async function loadConfig(configEnv: ConfigEnv) {
let config: UserConfig | null = null

const configEnv: ConfigEnv = {
command: isBuild ? 'build' : 'serve',
mode: isBuild ? 'production' : 'development',
}

// config file named by convention as the *.spec.ts folder
const variantName = path.basename(dirname(testPath))
if (variantName !== '__tests__') {
Expand Down Expand Up @@ -231,13 +226,16 @@ export async function startDefaultServe(): Promise<void> {
},
customLogger: createInMemoryLogger(serverLogs),
}
return mergeConfig(options, config || {})
}

export async function startDefaultServe(): Promise<void> {
setupConsoleWarnCollector(serverLogs)

if (!isBuild) {
process.env.VITE_INLINE = 'inline-serve'
const testConfig = mergeConfig(options, config || {})
viteServer = server = await (await createServer(testConfig)).listen()
const config = await loadConfig({ command: 'serve', mode: 'development' })
viteServer = server = await (await createServer(config)).listen()
viteTestUrl = server.resolvedUrls.local[0]
if (server.config.base === '/') {
viteTestUrl = viteTestUrl.replace(/\/$/, '')
Expand All @@ -252,24 +250,30 @@ export async function startDefaultServe(): Promise<void> {
resolvedConfig = config
},
})
options.plugins = [resolvedPlugin()]
const testConfig = mergeConfig(options, config || {})
const rollupOutput = await build(testConfig)
const buildConfig = mergeConfig(
await loadConfig({ command: 'build', mode: 'production' }),
{
plugins: [resolvedPlugin()],
},
)
const rollupOutput = await build(buildConfig)
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
if (isWatch) {
watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(watcher)
}
if (config && config.__test__) {
config.__test__()
}
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
if (config?.testConfig?.previewBase) {
testConfig.base = config.testConfig.previewBase
if (buildConfig.__test__) {
buildConfig.__test__()
}

const previewConfig = await loadConfig({
command: 'serve',
mode: 'development',
isPreview: true,
})
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(testConfig)
const previewServer = await preview(previewConfig)
// prevent preview change NODE_ENV
process.env.NODE_ENV = _nodeEnv
viteTestUrl = previewServer.resolvedUrls.local[0]
Expand Down Expand Up @@ -348,16 +352,5 @@ declare module 'vite' {
* runs after build and before preview
*/
__test__?: () => void
/**
* special test only configs
*/
testConfig?: {
/**
* a base used for preview
*
* useful for relative base tests
*/
previewBase?: string
}
}
}
9 changes: 3 additions & 6 deletions playground/worker/vite.config-relative-base-iife.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/relative-base-iife/',
resolve: {
alias: {
'@': __dirname,
Expand Down Expand Up @@ -30,9 +30,6 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base-iife/',
},
plugins: [workerPluginTestPlugin()],
cacheDir: 'node_modules/.vite-relative-base-iife',
})
}))
9 changes: 3 additions & 6 deletions playground/worker/vite.config-relative-base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/relative-base/',
resolve: {
alias: {
'@': __dirname,
Expand Down Expand Up @@ -30,9 +30,6 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
plugins: [
workerPluginTestPlugin(),
{
Expand All @@ -48,4 +45,4 @@ export default defineConfig({
},
],
cacheDir: 'node_modules/.vite-relative-base',
})
}))