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

Add support for collectting html inline-style dependencies(#541) #947

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/assets/HTMLAsset.js
Expand Up @@ -58,6 +58,7 @@ const META = {
'downloadUrl'
]
};
const URL_RE = /url\s*\("?(?![a-z]+:)/;

class HTMLAsset extends Asset {
constructor(name, pkg, options) {
Expand Down Expand Up @@ -92,6 +93,24 @@ class HTMLAsset extends Asset {
return newSources.join(',');
}

collectInlineStyleDependencies(inlineStyle) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is very hard to read. Could you try to add comments to make it easier to understand?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just add it in the newest commit.

const styles = inlineStyle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we parse the css using a real css parser like postcss to process this rather than implementing our own? Check out how we collect dependencies in the CSSAsset.

.split(/;/)
.filter(style => !/^[\n\s]*$/.test(style));

styles.forEach((style, index) => {
if (URL_RE.test(style)) {
let matchArr = /\((.*?)\)/.exec(style);
let path = matchArr.length > 1 ? matchArr[1].replace(/'|"/g, '') : null;
if (path) {
let assetPath = this.processSingleDependency(path);
styles[index] = style.replace(path, assetPath);
}
}
});
return styles.join(';');
}

collectDependencies() {
this.ast.walk(node => {
if (node.attrs) {
Expand All @@ -107,6 +126,13 @@ class HTMLAsset extends Asset {
}

for (let attr in node.attrs) {
if (attr === 'style' && URL_RE.test(node.attrs[attr])) {
node.attrs[attr] = this.collectInlineStyleDependencies(
node.attrs[attr]
);
this.isAstDirty = true;
}

if (node.tag === 'img' && attr === 'srcset') {
node.attrs[attr] = this.collectSrcSetDependencies(node.attrs[attr]);
this.isAstDirty = true;
Expand Down
18 changes: 18 additions & 0 deletions test/html.js
Expand Up @@ -470,6 +470,24 @@ describe('html', function() {
});
});

it('should collect inline-style dependencies', async function() {
let b = await bundle(
__dirname + '/integration/html-inline-style/index.html'
);

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'png',
assets: ['100x100.png'],
childBundles: []
}
]
});
});

it('should support webmanifest', async function() {
let b = await bundle(__dirname + '/integration/webmanifest/index.html');

Expand Down
Binary file added test/integration/html-inline-style/100x100.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/integration/html-inline-style/index.html
@@ -0,0 +1,13 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>inline-style-dependencies</title>
</head>

<body>
<div style="background: url('100x100.png') 0 0 no-repeat;width: 97px;"></div>
</body>

</html>