Skip to content

Commit

Permalink
Merge branch 'aweary-fix-url-issue'
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 6, 2017
2 parents 9daa7dd + 45dd384 commit 92be140
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"cssnano": "^3.10.0",
"glob": "^7.1.2",
"htmlnano": "^0.1.6",
"is-url": "^1.2.2",
"js-yaml": "^3.10.0",
"micromatch": "^3.0.4",
"mkdirp": "^0.5.1",
Expand Down
5 changes: 2 additions & 3 deletions src/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const path = require('path');
const fs = require('./utils/fs');
const objectHash = require('./utils/objectHash');
const md5 = require('./utils/md5');

const URL_RE = /^(([a-z]+:)|\/|#)/;
const isURL = require('./utils/is-url');

let ASSET_ID = 1;

Expand Down Expand Up @@ -63,7 +62,7 @@ class Asset {
}

addURLDependency(url, from = this.name, opts) {
if (!url || URL_RE.test(url)) {
if (!url || isURL(url)) {
return url;
}

Expand Down
7 changes: 6 additions & 1 deletion src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const md5 = require('../utils/md5');
const render = require('posthtml-render');
const posthtmlTransform = require('../transforms/posthtml');
const isURL = require('../utils/is-url');

// A list of all attributes that should produce a dependency
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
Expand Down Expand Up @@ -35,7 +36,11 @@ class HTMLAsset extends Asset {
for (let attr in node.attrs) {
let elements = ATTRS[attr];
if (elements && elements.includes(node.tag)) {
node.attrs[attr] = path.join(this.options.publicURL, this.addURLDependency(node.attrs[attr]));
let assetPath = this.addURLDependency(node.attrs[attr]);
if (!isURL(assetPath)) {
assetPath = path.join(this.options.publicURL, assetPath);
}
node.attrs[attr] = assetPath;
this.isAstDirty = true;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/is-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isURL = require('is-url');

module.exports = function (url) {
return isURL(url) || /^#/.test(url);
};
14 changes: 14 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ describe('html', function () {
assert(css.includes('Other page'));
assert(!css.includes('\n'));
});

it('should not prepend the public path to assets with remote URLs', async function () {
let b = await bundle(__dirname + '/integration/html/index.html');

let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('<script src="https://unpkg.com/parcel-bundler"></script>'));
});

it('should not prepend the public path to hash links', async function () {
let b = await bundle(__dirname + '/integration/html/index.html');

let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('<a href="#hash_link">'));
});
});
2 changes: 2 additions & 0 deletions test/integration/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<body>
<h1>Hello world</h1>
<p>Linking to <a href="other.html">another page</a></p>
<a href="#hash_link">Hash link</a>
<script src="index.js"></script>
<script src="https://unpkg.com/parcel-bundler"></script>
</body>
</html>

0 comments on commit 92be140

Please sign in to comment.