@@ -7,144 +7,70 @@ import {
7
7
beforeEach ,
8
8
afterEach ,
9
9
} from "vitest" ;
10
- import mock from "mock-fs" ;
11
10
import fs from "node:fs" ;
12
11
// Test functions
13
12
import { prepareTempEntries , cleanTempEntries } from "../src/vite-lifecycle.js" ;
14
13
import { PluginOption } from "../src/types.js" ;
15
14
16
15
const pluginOption : PluginOption = {
17
- sourceDir : "src" ,
16
+ sourceDir : "tests/fixtures/ src" ,
18
17
configName : "config.json" ,
19
- entryName : "main.js " ,
18
+ entryName : "main.jsx " ,
20
19
sharedData : { } ,
21
20
ejsOption : { } ,
22
21
} ;
23
22
24
23
const dest = "dist" ;
25
24
const entriesKV = {
26
- page1 : "" ,
27
- page2 : "" ,
28
- } ;
29
- const noTemplateDefinedProjectConstruct = {
30
- "node_modules" : mock . load (
31
- path . resolve ( __dirname , '..' , "node_modules" )
32
- ) ,
33
- "node_modules/vite-plugin-auto-mpa-html/assets/index.html" : mock . load (
34
- path . resolve ( __dirname , ".." , "assets/index.html" )
35
- ) ,
36
- src : {
37
- page1 : {
38
- "config.json" : '{"data":{"title":"Page 1"}}' ,
39
- "main.js" : "" ,
40
- } ,
41
- page2 : {
42
- "config.json" : '{"data":{"title":"Page 2"}}' ,
43
- "main.js" : "" ,
44
- } ,
45
- } ,
46
- } ;
47
- const commonProjectConstruct = {
48
- "node_modules" : mock . load (
49
- path . resolve ( __dirname , '..' , "node_modules" )
50
- ) ,
51
- "node_modules/vite-plugin-auto-mpa-html/assets/index.html" : mock . load (
52
- path . resolve ( __dirname , ".." , "assets/index.html" )
53
- ) ,
54
- src : {
55
- page1 : {
56
- assets : {
57
- "index.css" : ":root{background-color:#fff}" ,
58
- } ,
59
- "config.json" :
60
- '{"data":{"title":"Page 1"},"template":"templates/mobile.html"}' ,
61
- "main.js" : "" ,
62
- } ,
63
- page2 : {
64
- "config.json" :
65
- '{"data":{"title":"Page 2"},"template":"templates/index.html"}' ,
66
- "main.js" : "" ,
67
- } ,
68
- } ,
69
- templates : {
70
- "mobile.html" : `<!DOCTYPE html>
71
- <html lang="en">
72
- <head>
73
- <meta charset="UTF-8" />
74
- <link rel="icon" type="image/icon" href="/favicon.ico" />
75
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
76
- <title>templates/mobile.html</title>
77
- </head>
78
- <body>
79
- <div id="app"></div>
80
- </body>
81
- </html>` ,
82
- "index.html" : `<!DOCTYPE html>
83
- <html lang="en">
84
- <head>
85
- <meta charset="UTF-8" />
86
- <link rel="icon" type="image/icon" href="/favicon.ico" />
87
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
88
- <title>templates/index.html</title>
89
- </head>
90
- <body>
91
- <div id="app"></div>
92
- </body>
93
- </html>` ,
94
- } ,
95
- "vite.config.js" : `export default {{
96
- root: __dirname,
97
- }}` ,
98
- } ;
25
+ normal : "" ,
26
+ "no-template" : "" ,
27
+ "html-env-replacement" : ""
28
+ }
99
29
100
30
describe ( "Test plugin's lifecycle - buildStart" , ( ) => {
101
31
afterEach ( ( ) => {
102
- mock . restore ( ) ;
103
32
vi . restoreAllMocks ( ) ;
104
33
} ) ;
105
34
106
35
it ( "common project struct, should use user defined template" , ( ) => {
107
- mock ( commonProjectConstruct ) ;
108
36
// Do a generation
109
37
const generatedKeys = prepareTempEntries ( pluginOption , dest , entriesKV ) ;
110
38
expect ( Array . isArray ( generatedKeys ) ) . toBe ( true ) ;
111
39
expect ( generatedKeys . length ) . toBe ( Object . keys ( entriesKV ) . length ) ;
112
- generatedKeys . forEach ( ( key ) => {
113
- const filePath = path . join ( `${ key } .html` ) ;
114
- const configPath = path . join (
115
- `${ pluginOption . sourceDir } /${ key } /${ pluginOption . configName } `
116
- ) ;
117
- let configData = fs . readFileSync ( configPath , { encoding : "utf-8" } ) ;
118
- expect ( fs . existsSync ( filePath ) ) . toBe ( true ) ; // temporary generated an entry HTML for build
119
- const fileContent = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
120
- expect ( fileContent ) . toMatch ( "</html>" ) ; // correctly end html doc.
121
- expect ( fileContent ) . toMatch (
122
- `<title>${ JSON . parse ( configData ) . template } </title>`
123
- ) ; // correctly render ejs template with given data
124
- expect ( fileContent ) . toMatch (
125
- `<script type="module" src="./${ pluginOption . sourceDir } /${ key } /${ pluginOption . entryName } "></script>`
126
- ) ; // contain needed entry module
127
- } ) ;
40
+ const filePath = path . join ( `normal.html` ) ;
41
+ const configPath = path . join (
42
+ `${ pluginOption . sourceDir } /normal/${ pluginOption . configName } `
43
+ ) ;
44
+ expect ( fs . existsSync ( filePath ) ) . toBe ( true ) ; // temporary generated an entry HTML for build
45
+ const fileContent = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
46
+ expect ( fileContent ) . toMatch ( "</html>" ) ; // correctly end html doc.
47
+ expect ( fileContent ) . toMatch (
48
+ `<title>Minimal React Vite Project</title>`
49
+ ) ; // correctly render ejs template with given data
50
+ expect ( fileContent ) . toMatch (
51
+ `<script type="module" src="./${ pluginOption . sourceDir } /normal/${ pluginOption . entryName } "></script>`
52
+ ) ; // contain needed entry module
128
53
} ) ;
129
54
130
55
it ( "when template is not defined, should fallback to package preset `index.html`" , ( ) => {
131
- mock ( noTemplateDefinedProjectConstruct ) ;
132
56
// Do a generation
133
57
const generatedKeys = prepareTempEntries ( pluginOption , dest , entriesKV ) ;
134
58
expect ( Array . isArray ( generatedKeys ) ) . toBe ( true ) ;
135
59
expect ( generatedKeys . length ) . toBe ( Object . keys ( entriesKV ) . length ) ;
136
- generatedKeys . forEach ( ( key ) => {
137
- const filePath = path . join ( `${ key } .html` ) ;
138
- const configPath = path . join (
139
- `${ pluginOption . sourceDir } /${ key } /${ pluginOption . configName } `
140
- ) ;
141
- let configData = fs . readFileSync ( configPath , { encoding : "utf-8" } ) ;
142
- expect ( fs . existsSync ( filePath ) ) . toBe ( true ) ; // temporary generated an entry HTML for build
143
- const fileContent = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
144
- expect ( fileContent ) . toMatch (
145
- `<title>${ JSON . parse ( configData ) . data . title } </title>`
146
- ) ; // correctly render ejs template with given data
147
- } ) ;
60
+ const filePath = path . join ( `no-template.html` ) ;
61
+ const configPath = path . join (
62
+ `${ pluginOption . sourceDir } /no-template/${ pluginOption . configName } `
63
+ ) ;
64
+ let configData = fs . readFileSync ( configPath , { encoding : "utf-8" } ) ;
65
+ expect ( fs . existsSync ( filePath ) ) . toBe ( true ) ; // temporary generated an entry HTML for build
66
+ const fileContent = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
67
+ expect ( fileContent ) . toMatch ( "</html>" ) ; // correctly end html doc.
68
+ expect ( fileContent ) . toMatch (
69
+ `<title>${ JSON . parse ( configData ) . data . title } </title>`
70
+ ) ; // correctly render ejs template with given data
71
+ expect ( fileContent ) . toMatch (
72
+ `<script type="module" src="./${ pluginOption . sourceDir } /no-template/${ pluginOption . entryName } "></script>`
73
+ ) ; // contain needed entry module
148
74
} ) ;
149
75
150
76
// Skip EJS Option tests
@@ -153,7 +79,6 @@ describe("Test plugin's lifecycle - buildStart", () => {
153
79
describe ( "Test plugin's lifecycle - buildEnd" , ( ) => {
154
80
let generatedKeys : string [ ] ;
155
81
beforeEach ( ( ) => {
156
- mock ( commonProjectConstruct ) ;
157
82
// pre generate file struct
158
83
generatedKeys = prepareTempEntries ( pluginOption , dest , entriesKV ) ;
159
84
} ) ;
0 commit comments