File tree 4 files changed +83
-2
lines changed 4 files changed +83
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { isBuild } from '../../../testUtils'
2
+ import { port } from './serve'
3
+
4
+ const url = `http://localhost:${ port } `
5
+
6
+ if ( isBuild ) {
7
+ test ( 'should work' , async ( ) => {
8
+ await page . goto ( url )
9
+ expect ( await page . textContent ( '#app' ) ) . toMatch ( 'Hello' )
10
+ } )
11
+
12
+ test ( 'import.meta.env.LEGACY' , async ( ) => {
13
+ // SSR build is always modern
14
+ expect ( await page . textContent ( '#env' ) ) . toMatch ( 'false' )
15
+ } )
16
+ } else {
17
+ // this test doesn't support serve mode
18
+ // must contain at least one test
19
+ test ( 'should work' , ( ) => void 0 )
20
+ }
Original file line number Diff line number Diff line change
1
+ // @ts -check
2
+ // this is automtically detected by scripts/jestPerTestSetup.ts and will replace
3
+ // the default e2e test serve behavior
4
+ const path = require ( 'path' )
5
+
6
+ const port = ( exports . port = 9527 )
7
+
8
+ /**
9
+ * @param {string } root
10
+ * @param {boolean } _isProd
11
+ */
12
+ exports . serve = async function serve ( root , _isProd ) {
13
+ const { build } = require ( 'vite' )
14
+ await build ( {
15
+ root,
16
+ logLevel : 'silent' ,
17
+ build : {
18
+ target : 'esnext' ,
19
+ ssr : 'entry-server.js' ,
20
+ outDir : 'dist/server'
21
+ }
22
+ } )
23
+
24
+ const express = require ( 'express' )
25
+ const app = express ( )
26
+
27
+ app . use ( '/' , async ( _req , res ) => {
28
+ const { render } = require ( path . resolve (
29
+ root ,
30
+ './dist/server/entry-server.js'
31
+ ) )
32
+ const html = await render ( )
33
+ res . status ( 200 ) . set ( { 'Content-Type' : 'text/html' } ) . end ( html )
34
+ } )
35
+
36
+ return new Promise ( ( resolve , reject ) => {
37
+ try {
38
+ const server = app . listen ( port , ( ) => {
39
+ resolve ( {
40
+ // for test teardown
41
+ async close ( ) {
42
+ await new Promise ( ( resolve ) => {
43
+ server . close ( resolve )
44
+ } )
45
+ }
46
+ } )
47
+ } )
48
+ } catch ( e ) {
49
+ reject ( e )
50
+ }
51
+ } )
52
+ }
Original file line number Diff line number Diff line change
1
+ // This counts as 'server-side' rendering, yes?
2
+ export async function render ( ) {
3
+ return /* html */ `
4
+ <div id="app">Hello</div>
5
+ <div id="env">${ import . meta. env . LEGACY } </div>
6
+ `
7
+ }
Original file line number Diff line number Diff line change @@ -451,12 +451,14 @@ function viteLegacyPlugin(options = {}) {
451
451
const legacyEnvPlugin = {
452
452
name : 'legacy-env' ,
453
453
454
- config ( _ , env ) {
454
+ config ( config , env ) {
455
455
if ( env ) {
456
456
return {
457
457
define : {
458
458
'import.meta.env.LEGACY' :
459
- env . command === 'serve' ? false : legacyEnvVarMarker
459
+ env . command === 'serve' || config . build . ssr
460
+ ? false
461
+ : legacyEnvVarMarker
460
462
}
461
463
}
462
464
} else {
You can’t perform that action at this time.
0 commit comments