markdalgleish / static-site-generator-webpack-plugin

Minimal, unopinionated static site generator powered by webpack
MIT License
1.61k stars 97 forks source link

HTML minification #40

Open pmendelski opened 8 years ago

pmendelski commented 8 years ago

Hi. First of all thanks for a great plugin.

Is there a way to minify the prerendered html using html-minifier?

P.S. html-minifier depends on other modules that don't play well with webpack compilation.

Minification attempt:

minify(ReactDOMServer.renderToString(<RoutingContext {...renderProps} />), locals.minify)

...results in:

ERROR in ../~/uglify-js/tools/node.js
Module not found: Error: Cannot resolve module 'fs' in $PROJECT_DIR/node_modules/uglify-js/tools
 @ ../~/uglify-js/tools/node.js 2:9-22

ERROR in ../~/clean-css/lib/clean.js
Module not found: Error: Cannot resolve module 'fs' in $PROJECT_DIR/node_modules/clean-css/lib
 @ ../~/clean-css/lib/clean.js 29:9-22

ERROR in ../~/clean-css/lib/imports/inliner.js
Module not found: Error: Cannot resolve module 'fs' in $PROJECT_DIR/node_modules/clean-css/lib/imports
 @ ../~/clean-css/lib/imports/inliner.js 1:9-22

ERROR in ../~/clean-css/lib/utils/input-source-map-tracker.js
Module not found: Error: Cannot resolve module 'fs' in $PROJECT_DIR/node_modules/clean-css/lib/utils
 @ ../~/clean-css/lib/utils/input-source-map-tracker.js 3:9-22
roelkok commented 7 years ago

I solved this by setting target: 'node' in my webpack config. This solves the 'fs' errors. However in the case of html-minifier you'll get a new error because of the UglifyJS dependency. Uglify has some funky require going on which breaks when the script is run by the static site generator. Since I didn't need this functionality, added a hacky solution to my webpack config: I added an alias for 'uglify-js' which would resolve to some dummy file.