1- const { createSSRApp } = require ( 'vue' ) ;
1+ const { createSSRApp, h } = require ( 'vue' ) ;
22const cheerio = require ( 'cheerio' ) ;
33const { renderToString } = require ( 'vue/server-renderer' ) ;
44const fs = require ( 'fs' ) ;
55
66const fixtureDir = __dirname + '/fixture' ;
7+ const componentDir = fixtureDir + '/components' ;
78const generatedComment = '<!-- generated by `npm run-script populate-fixtures` -->' ;
89
910const defaultMethods = {
@@ -13,6 +14,20 @@ const defaultMethods = {
1314 }
1415} ;
1516
17+ const components = { } ;
18+
19+ readComponentDir ( ) . then ( function ( files ) {
20+ files . forEach ( function ( fileName ) {
21+ var filePath = componentDir + '/' + fileName ;
22+ readFile ( filePath )
23+ . then ( extractComponentTemplate )
24+ . then ( function ( componentData ) {
25+ console . log ( 'loaded component ' + fileName + ' : ' + componentData . componentName ) ;
26+ components [ componentData . componentName ] = componentData . template ;
27+ } )
28+ } ) ;
29+ } ) ;
30+
1631readFixtureDir ( ) . then ( function ( files ) {
1732 files . forEach ( function ( fileName ) {
1833 var filePath = fixtureDir + '/' + fileName ;
@@ -27,10 +42,24 @@ readFixtureDir().then( function ( files ) {
2742 } ) ;
2843} ) ;
2944
45+ function getTemplateHtml ( templateElement ) {
46+ return cheerio . load ( templateElement . html ( ) ) . html ( { decodeEntities : false } ) . trim ( ) ;
47+ }
48+
49+ function extractComponentTemplate ( html ) {
50+ const $ = cheerio . load ( html ) ;
51+ const templateRoot = $ ( '#template' ) ;
52+
53+ const componentName = templateRoot . attr ( 'component-name' ) ;
54+ console . log ( "Found component with name " + componentName ) ;
55+ const template = getTemplateHtml ( templateRoot ) ;
56+ return { componentName : componentName , template } ;
57+ }
58+
3059function extractDataFromFixture ( html ) {
3160 const $ = cheerio . load ( html ) ;
3261
33- const template = cheerio . load ( $ ( '#template' ) . html ( ) ) . html ( { decodeEntities : false } ) . trim ( ) ;
62+ const template = getTemplateHtml ( $ ( '#template' ) ) ;
3463 const data = JSON . parse ( $ ( '#data' ) . html ( ) ) ;
3564
3665 return { template : template , data : data } ;
@@ -42,6 +71,9 @@ function renderTemplate( fixtureData ) {
4271 data : ( ) => fixtureData . data ,
4372 methods : defaultMethods
4473 } ) ;
74+ for ( [ componentName , template ] of Object . entries ( components ) ) {
75+ app . component ( componentName , { template } ) ;
76+ } ;
4577
4678 return renderToString ( app ) ;
4779}
@@ -80,18 +112,26 @@ function saveFile( filePath, contents ) {
80112 } ) ;
81113}
82114
83- function readFixtureDir ( ) {
115+ function readDirFiles ( dirPath ) {
84116 return new Promise ( function ( resolve , reject ) {
85- fs . readdir ( fixtureDir , function ( err , items ) {
117+ fs . readdir ( dirPath , { withFileTypes : true } , function ( err , items ) {
86118 if ( err ) {
87119 reject ( err )
88120 } else {
89- resolve ( items ) ;
121+ resolve ( items . filter ( ( item ) => item . isFile ( ) ) . map ( ( item ) => item . name ) ) ;
90122 }
91123 } ) ;
92124 } ) ;
93125}
94126
127+ function readComponentDir ( ) {
128+ return readDirFiles ( componentDir ) ;
129+ }
130+
131+ function readFixtureDir ( ) {
132+ return readDirFiles ( fixtureDir ) ;
133+ }
134+
95135function saveResultToFile ( filePath , renderResult ) {
96136 readFile ( filePath ) . then ( function ( html ) {
97137 const $ = cheerio . load ( html ) ;
0 commit comments