@@ -5,15 +5,14 @@ const http = require('http');
55const WebSocket = require ( 'ws' ) ;
66const _ = require ( 'lodash' ) ;
77const express = require ( 'express' ) ;
8- const ejs = require ( 'ejs' ) ;
98const { bold} = require ( 'chalk' ) ;
109
1110const Logger = require ( './Logger' ) ;
1211const analyzer = require ( './analyzer' ) ;
1312const { open} = require ( './utils' ) ;
13+ const { renderViewer} = require ( './template' ) ;
1414
1515const projectRoot = path . resolve ( __dirname , '..' ) ;
16- const assetsRoot = path . join ( projectRoot , 'public' ) ;
1716
1817function resolveTitle ( reportTitle ) {
1918 if ( typeof reportTitle === 'function' ) {
@@ -50,24 +49,18 @@ async function startServer(bundleStats, opts) {
5049 if ( ! chartData ) return ;
5150
5251 const app = express ( ) ;
53-
54- // Explicitly using our `ejs` dependency to render templates
55- // Fixes #17
56- app . engine ( 'ejs' , require ( 'ejs' ) . renderFile ) ;
57- app . set ( 'view engine' , 'ejs' ) ;
58- app . set ( 'views' , `${ projectRoot } /views` ) ;
5952 app . use ( express . static ( `${ projectRoot } /public` ) ) ;
6053
61- app . use ( '/' , ( req , res ) => {
62- res . render ( 'viewer' , {
54+ app . get ( '/' , ( req , res ) => {
55+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
56+ const html = renderViewer ( {
6357 mode : 'server' ,
6458 title : resolveTitle ( reportTitle ) ,
65- get chartData ( ) { return chartData } ,
59+ chartData,
6660 defaultSizes,
67- enableWebSocket : true ,
68- // Helpers
69- escapeJson
61+ enableWebSocket : true
7062 } ) ;
63+ return res . end ( html ) ;
7164 } ) ;
7265
7366 const server = http . createServer ( app ) ;
@@ -140,42 +133,28 @@ async function generateReport(bundleStats, opts) {
140133 if ( ! chartData ) return ;
141134
142135 await new Promise ( ( resolve , reject ) => {
143- ejs . renderFile (
144- `${ projectRoot } /views/viewer.ejs` ,
145- {
136+ try {
137+ const reportHtml = renderViewer ( {
146138 mode : 'static' ,
147139 title : resolveTitle ( reportTitle ) ,
148140 chartData,
149141 defaultSizes,
150- enableWebSocket : false ,
151- // Helpers
152- assetContent : getAssetContent ,
153- escapeJson
154- } ,
155- ( err , reportHtml ) => {
156- try {
157- if ( err ) {
158- logger . error ( err ) ;
159- reject ( err ) ;
160- return ;
161- }
162-
163- const reportFilepath = path . resolve ( bundleDir || process . cwd ( ) , reportFilename ) ;
164-
165- fs . mkdirSync ( path . dirname ( reportFilepath ) , { recursive : true } ) ;
166- fs . writeFileSync ( reportFilepath , reportHtml ) ;
167-
168- logger . info ( `${ bold ( 'Webpack Bundle Analyzer' ) } saved report to ${ bold ( reportFilepath ) } ` ) ;
169-
170- if ( openBrowser ) {
171- open ( `file://${ reportFilepath } ` , logger ) ;
172- }
173- resolve ( ) ;
174- } catch ( e ) {
175- reject ( e ) ;
176- }
142+ enableWebSocket : false
143+ } ) ;
144+ const reportFilepath = path . resolve ( bundleDir || process . cwd ( ) , reportFilename ) ;
145+
146+ fs . mkdirSync ( path . dirname ( reportFilepath ) , { recursive : true } ) ;
147+ fs . writeFileSync ( reportFilepath , reportHtml ) ;
148+
149+ logger . info ( `${ bold ( 'Webpack Bundle Analyzer' ) } saved report to ${ bold ( reportFilepath ) } ` ) ;
150+
151+ if ( openBrowser ) {
152+ open ( `file://${ reportFilepath } ` , logger ) ;
177153 }
178- ) ;
154+ resolve ( ) ;
155+ } catch ( e ) {
156+ reject ( e ) ;
157+ }
179158 } ) ;
180159}
181160
@@ -192,23 +171,6 @@ async function generateJSONReport(bundleStats, opts) {
192171 logger . info ( `${ bold ( 'Webpack Bundle Analyzer' ) } saved JSON report to ${ bold ( reportFilename ) } ` ) ;
193172}
194173
195- function getAssetContent ( filename ) {
196- const assetPath = path . join ( assetsRoot , filename ) ;
197-
198- if ( ! assetPath . startsWith ( assetsRoot ) ) {
199- throw new Error ( `"${ filename } " is outside of the assets root` ) ;
200- }
201-
202- return fs . readFileSync ( assetPath , 'utf8' ) ;
203- }
204-
205- /**
206- * Escapes `<` characters in JSON to safely use it in `<script>` tag.
207- */
208- function escapeJson ( json ) {
209- return JSON . stringify ( json ) . replace ( / < / gu, '\\u003c' ) ;
210- }
211-
212174function getChartData ( analyzerOpts , ...args ) {
213175 let chartData ;
214176 const { logger} = analyzerOpts ;
0 commit comments