Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix svg dependencies not being found when using minification #1022

Merged
merged 5 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>