Skip to content

Commit

Permalink
Fix svg dependencies not being found when using minification (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper De Moor authored and devongovett committed Mar 20, 2018
1 parent 9858937 commit e3c90c4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
9 changes: 8 additions & 1 deletion src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const api = require('posthtml/lib/api');
const urlJoin = require('../utils/urlJoin');
const render = require('posthtml-render');
const posthtmlTransform = require('../transforms/posthtml');
const htmlnanoTransform = require('../transforms/htmlnano');
const isURL = require('../utils/is-url');

// A list of all attributes that may produce a dependency
Expand All @@ -22,7 +23,7 @@ const ATTRS = {
href: ['link', 'a', 'use'],
srcset: ['img', 'source'],
poster: ['video'],
'xlink:href': ['use'], // Deprecated since SVG 2, throws error in svgo
'xlink:href': ['use'],
content: ['meta']
};

Expand Down Expand Up @@ -136,6 +137,12 @@ class HTMLAsset extends Asset {
await posthtmlTransform(this);
}

async transform() {
if (this.options.minify) {
await htmlnanoTransform(this);
}
}

generate() {
let html = this.isAstDirty ? render(this.ast) : this.contents;
return {html};
Expand Down
21 changes: 21 additions & 0 deletions src/transforms/htmlnano.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const posthtml = require('posthtml');
const htmlnano = require('htmlnano');

module.exports = async function(asset) {
await asset.parseIfNeeded();

const htmlNanoConfig = asset.package.htmlnano ||
(await asset.getConfig(['.htmlnanorc', '.htmlnanorc.js'])) || {
collapseWhitespace: 'conservative',
minifyCss: {
safe: true
}
};

let res = await posthtml([htmlnano(htmlNanoConfig)]).process(asset.ast, {
skipParse: true
});

asset.ast = res.tree;
asset.isAstDirty = true;
};
14 changes: 0 additions & 14 deletions src/transforms/posthtml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const loadPlugins = require('../utils/loadPlugins');
const posthtml = require('posthtml');
const htmlnano = require('htmlnano');

module.exports = async function(asset) {
let config = await getConfig(asset);
Expand Down Expand Up @@ -29,19 +28,6 @@ async function getConfig(asset) {

config = config || {};
config.plugins = await loadPlugins(config.plugins, asset.name);

if (asset.options.minify) {
const htmlNanoConfig = asset.package.htmlnano ||
(await asset.getConfig(['.htmlnanorc', '.htmlnanorc.js'])) || {
collapseWhitespace: 'conservative',
minifyCss: {
safe: true
}
};

config.plugins.push(htmlnano(htmlNanoConfig));
}

config.skipParse = true;
return config;
}
15 changes: 15 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,19 @@ describe('html', function() {
]
});
});

it('should bundle svg files correctly', async function() {
let b = await bundle(__dirname + '/integration/html-svg/index.html');

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'svg',
assets: ['file.svg']
}
]
});
});
});
3 changes: 3 additions & 0 deletions test/integration/html-svg/file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/integration/html-svg/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
<use xlink:href="file.svg#all"/>
</svg>

0 comments on commit e3c90c4

Please sign in to comment.