Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
html-template: Adjust minification settings to match upstream (#1121)
Browse files Browse the repository at this point in the history
The next release of `html-webpack-plugin` will enable minification
by default when `mode` is production, using `minify` options that
are based on those used by popular projects:
jantimon/html-webpack-plugin#1048

Those changes haven't yet been released, so in the meantime we can
emulate them via preset options, so that we can test out the new
defaults sooner (ie: in the upcoming alpha/beta), rather than making
the breaking change just before Neutrino 9 final.

Once a new version of `html-webpack-plugin` is released, we can stop
overriding `minify` entirely.
  • Loading branch information
edmorley authored Sep 20, 2018
1 parent 5680b16 commit 70865c0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
24 changes: 16 additions & 8 deletions packages/html-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ neutrino.use(template, {
meta: {
viewport: 'width=device-width, initial-scale=1'
},
minify: {
useShortDoctype: true,
keepClosingSlash: true,
// These are copied from the new html-webpack-plugin defaults (not yet released):
// https://github.com/jantimon/html-webpack-plugin/pull/1048
minify: process.env.NODE_ENV === 'production' && {
collapseWhitespace: true,
preserveLineBreaks: true
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
},
// Override pluginId to add an additional html-template plugin instance
pluginId: 'html'
Expand Down Expand Up @@ -84,11 +88,15 @@ module.exports = {
meta: {
viewport: 'width=device-width, initial-scale=1'
},
minify: {
useShortDoctype: true,
keepClosingSlash: true,
// These are copied from the new html-webpack-plugin defaults (not yet released):
// https://github.com/jantimon/html-webpack-plugin/pull/1048
minify: process.env.NODE_ENV === 'production' && {
collapseWhitespace: true,
preserveLineBreaks: true
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
},
// Override pluginId to add an additional html-template plugin instance
pluginId: 'html'
Expand Down
27 changes: 17 additions & 10 deletions packages/html-template/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
const { resolve } = require('path');

const merge = require('deepmerge');

module.exports = (neutrino, { pluginId = 'html', ...options } = {}) => {
neutrino.config
.plugin(pluginId)
.use(require.resolve('html-webpack-plugin'), [
merge({
{
// Use a custom template that has more features (appMountId, lang) than the default:
// https://github.com/jantimon/html-webpack-plugin/blob/master/default_index.ejs
template: resolve(__dirname, 'template.ejs'),
appMountId: 'root',
lang: 'en',
meta: {
viewport: 'width=device-width, initial-scale=1'
viewport: 'width=device-width, initial-scale=1',
...options.meta
},
minify: {
useShortDoctype: true,
keepClosingSlash: true,
// These are copied from the new html-webpack-plugin defaults:
// https://github.com/jantimon/html-webpack-plugin/pull/1048
// Passing options.minify will overwrite these defaults entirely
// (intentional for parity with the html-webpack-plugin implementation).
// Remove this once we're using a new release containing that change.
minify: process.env.NODE_ENV === 'production' && {
collapseWhitespace: true,
preserveLineBreaks: true
}
}, options)
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
},
...options
}
]);
};
1 change: 0 additions & 1 deletion packages/html-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"yarn": ">=1.2.1"
},
"dependencies": {
"deepmerge": "^1.5.2",
"html-webpack-plugin": "4.0.0-alpha.2"
},
"peerDependencies": {
Expand Down
31 changes: 31 additions & 0 deletions packages/html-template/test/middleware_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import Neutrino from '../../neutrino/Neutrino';

const mw = () => require('..');
const options = { title: 'Alpha Beta', appMountId: 'app' };
const originalNodeEnv = process.env.NODE_ENV;

test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
});

test('loads middleware', t => {
t.notThrows(mw);
Expand Down Expand Up @@ -35,3 +41,28 @@ test('instantiates with options', t => {

t.notThrows(() => api.config.toConfig());
});

test('minifies in production', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();
api.use(mw());

const pluginOptions = api.config.plugin('html').get('args')[0];
t.deepEqual(pluginOptions.minify, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
});
});

test('does not minify in development', t => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();
api.use(mw());

const pluginOptions = api.config.plugin('html').get('args')[0];
t.false(pluginOptions.minify);
});

0 comments on commit 70865c0

Please sign in to comment.