11const { resolve, join } = require ( 'path' ) ;
22const os = require ( 'os' ) ;
33const { existsSync, readFileSync, writeFileSync, mkdirSync } = require ( 'fs' ) ;
4- const HtmlWebpackExcludeAssetsPlugin = require ( 'html-webpack-exclude-assets-plugin' ) ;
4+ const {
5+ HtmlWebpackSkipAssetsPlugin,
6+ } = require ( 'html-webpack-skip-assets-plugin' ) ;
57const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
68const prerender = require ( './prerender' ) ;
79const createLoadManifest = require ( './create-load-manifest' ) ;
@@ -17,8 +19,8 @@ function read(path) {
1719 return readFileSync ( resolve ( __dirname , path ) , 'utf-8' ) ;
1820}
1921
20- module . exports = async function ( config ) {
21- const { cwd, dest, isProd , src } = config ;
22+ module . exports = async function renderHTMLPlugin ( config ) {
23+ const { cwd, dest, src } = config ;
2224 const inProjectTemplatePath = resolve ( src , 'template.html' ) ;
2325 let template = defaultTemplate ;
2426 if ( existsSync ( inProjectTemplatePath ) ) {
@@ -38,10 +40,7 @@ module.exports = async function (config) {
3840 const headEnd = read ( '../../resources/head-end.ejs' ) ;
3941 const bodyEnd = read ( '../../resources/body-end.ejs' ) ;
4042 content = content
41- . replace (
42- / < % [ = ] ? \s + p r e a c t \. t i t l e \s + % > / ,
43- '<%= htmlWebpackPlugin.options.title %>'
44- )
43+ . replace ( / < % [ = ] ? \s + p r e a c t \. t i t l e \s + % > / , '<%= cli.title %>' )
4544 . replace ( / < % \s + p r e a c t \. h e a d E n d \s + % > / , headEnd )
4645 . replace ( / < % \s + p r e a c t \. b o d y E n d \s + % > / , bodyEnd ) ;
4746
@@ -56,53 +55,71 @@ module.exports = async function (config) {
5655 }
5756
5857 const htmlWebpackConfig = values => {
59- const { url, title, ...routeData } = values ;
58+ let { url, title, ...routeData } = values ;
59+
60+ title =
61+ title ||
62+ config . title ||
63+ config . manifest . name ||
64+ config . manifest . short_name ||
65+ ( config . pkg . name || '' ) . replace ( / ^ @ [ a - z ] \/ / , '' ) ||
66+ 'Preact App' ;
67+
6068 // Do not create a folder if the url is for a specific file.
6169 const filename = url . endsWith ( '.html' )
6270 ? resolve ( dest , url . substring ( 1 ) )
6371 : resolve ( dest , url . substring ( 1 ) , 'index.html' ) ;
64- return Object . assign ( values , {
72+
73+ return {
74+ title,
6575 filename,
6676 template : `!!${ require . resolve ( 'ejs-loader' ) } ?esModule=false!${ template } ` ,
67- minify : isProd && {
68- collapseWhitespace : true ,
69- removeScriptTypeAttributes : true ,
70- removeRedundantAttributes : true ,
71- removeStyleLinkTypeAttributes : true ,
72- removeComments : true ,
77+ templateParameters : ( compilation , assets , assetTags , options ) => {
78+ let entrypoints = { } ;
79+ compilation . entrypoints . forEach ( ( entrypoint , name ) => {
80+ let entryFiles = entrypoint . getFiles ( ) ;
81+ entrypoints [ name ] =
82+ assets . publicPath +
83+ entryFiles . find ( file => / \. ( m ? j s ) ( \? | $ ) / . test ( file ) ) ;
84+ } ) ;
85+
86+ let loadManifest = compilation . assets [ 'push-manifest.json' ]
87+ ? JSON . parse ( compilation . assets [ 'push-manifest.json' ] . source ( ) )
88+ : createLoadManifest (
89+ compilation . assets ,
90+ config . esm ,
91+ compilation . namedChunkGroups
92+ ) ;
93+
94+ return {
95+ cli : {
96+ title,
97+ url,
98+ manifest : config . manifest ,
99+ inlineCss : config [ 'inline-css' ] ,
100+ preload : config . preload ,
101+ config,
102+ preRenderData : values ,
103+ CLI_DATA : { preRenderData : { url, ...routeData } } ,
104+ ssr : config . prerender ? prerender ( { cwd, dest, src } , values ) : '' ,
105+ loadManifest,
106+ entrypoints,
107+ } ,
108+ htmlWebpackPlugin : {
109+ tags : assetTags ,
110+ files : assets ,
111+ options : options ,
112+ } ,
113+ } ;
73114 } ,
115+ inject : true ,
116+ scriptLoading : 'defer' ,
74117 favicon : existsSync ( resolve ( src , 'assets/favicon.ico' ) )
75118 ? 'assets/favicon.ico'
76119 : '' ,
77- inject : true ,
78- compile : true ,
79- inlineCss : config [ 'inline-css' ] ,
80- preload : config . preload ,
81- manifest : config . manifest ,
82- title :
83- title ||
84- config . title ||
85- config . manifest . name ||
86- config . manifest . short_name ||
87- ( config . pkg . name || '' ) . replace ( / ^ @ [ a - z ] \/ / , '' ) ||
88- 'Preact App' ,
89120 excludeAssets : [ / ( b u n d l e | p o l y f i l l s ) ( \. .* ) ? \. j s $ / ] ,
90- createLoadManifest : ( assets , namedChunkGroups ) => {
91- if ( assets [ 'push-manifest.json' ] ) {
92- return JSON . parse ( assets [ 'push-manifest.json' ] . source ( ) ) ;
93- }
94- return createLoadManifest ( assets , config . esm , namedChunkGroups ) ;
95- } ,
96- config,
97- url,
98- ssr ( ) {
99- return config . prerender && url !== PREACT_FALLBACK_URL
100- ? prerender ( { cwd, dest, src } , values )
101- : '' ;
102- } ,
103- scriptLoading : 'defer' ,
104- CLI_DATA : { preRenderData : { url, ...routeData } } ,
105- } ) ;
121+ // excludeChunks: ['bundle', 'polyfills']
122+ } ;
106123 } ;
107124
108125 let pages = [ { url : '/' } ] ;
@@ -158,7 +175,7 @@ module.exports = async function (config) {
158175 const resultPages = pages
159176 . map ( htmlWebpackConfig )
160177 . map ( conf => new HtmlWebpackPlugin ( conf ) )
161- . concat ( [ new HtmlWebpackExcludeAssetsPlugin ( ) ] ) ;
178+ . concat ( [ new HtmlWebpackSkipAssetsPlugin ( ) ] ) ;
162179
163180 return config . prerender
164181 ? resultPages . concat ( [
@@ -170,10 +187,9 @@ module.exports = async function (config) {
170187// Adds a preact_prerender_data in every folder so that the data could be fetched separately.
171188class PrerenderDataExtractPlugin {
172189 constructor ( page ) {
173- const cliData = page . CLI_DATA || { } ;
174- const { url } = cliData . preRenderData || { } ;
190+ const url = page . url ;
175191 this . location_ = url . endsWith ( '/' ) ? url : url + '/' ;
176- this . data_ = JSON . stringify ( cliData . preRenderData || { } ) ;
192+ this . data_ = JSON . stringify ( page || { } ) ;
177193 }
178194 apply ( compiler ) {
179195 compiler . hooks . emit . tap ( 'PrerenderDataExtractPlugin' , compilation => {
0 commit comments