Skip to content

Commit

Permalink
Correctly build dependency URLs (for CSS) (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed Mar 12, 2019
1 parent 161cd27 commit 68701f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
17 changes: 8 additions & 9 deletions packages/core/integration-tests/test/css.js
Expand Up @@ -166,7 +166,7 @@ describe('css', function() {
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(/url\("test\.[0-9a-f]+\.woff2"\)/.test(css));
assert(/url\("\/test\.[0-9a-f]+\.woff2"\)/.test(css));
assert(css.includes('url("http://google.com")'));
assert(css.includes('.index'));
assert(css.includes('url("data:image/gif;base64,quotes")'));
Expand All @@ -179,7 +179,7 @@ describe('css', function() {
path.join(
__dirname,
'/dist/',
css.match(/url\("(test\.[0-9a-f]+\.woff2)"\)/)[1]
css.match(/url\("(\/test\.[0-9a-f]+\.woff2)"\)/)[1]
)
)
);
Expand Down Expand Up @@ -225,7 +225,10 @@ describe('css', function() {
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(/url\(test\.[0-9a-f]+\.woff2\)/.test(css), 'woff ext found in css');
assert(
/url\(\/test\.[0-9a-f]+\.woff2\)/.test(css),
'woff ext found in css'
);
assert(css.includes('url(http://google.com)'), 'url() found');
assert(css.includes('.index'), '.index found');
assert(css.includes('url("data:image/gif;base64,quotes")'));
Expand All @@ -238,7 +241,7 @@ describe('css', function() {
path.join(
__dirname,
'/dist/',
css.match(/url\((test\.[0-9a-f]+\.woff2)\)/)[1]
css.match(/url\((\/test\.[0-9a-f]+\.woff2)\)/)[1]
)
)
);
Expand Down Expand Up @@ -282,11 +285,7 @@ describe('css', function() {

assert(
await fs.exists(
path.join(
__dirname,
path.dirname('/dist/a/style1.css'),
css.match(/url\(([^)]*)\)/)[1]
)
path.join(__dirname, 'dist', css.match(/url\(([^)]*)\)/)[1])
),
'path specified in url() exists'
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/integration-tests/test/sass.js
Expand Up @@ -188,7 +188,7 @@ describe('sass', function() {
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(/url\("test\.[0-9a-f]+\.woff2"\)/.test(css));
assert(/url\("\/test\.[0-9a-f]+\.woff2"\)/.test(css));
assert(css.includes('url("http://google.com")'));
assert(css.includes('.index'));

Expand All @@ -197,7 +197,7 @@ describe('sass', function() {
path.join(
__dirname,
'/dist/',
css.match(/url\("(test\.[0-9a-f]+\.woff2)"\)/)[1]
css.match(/url\("(\/test\.[0-9a-f]+\.woff2)"\)/)[1]
)
)
);
Expand Down
3 changes: 1 addition & 2 deletions packages/core/parcel-bundler/src/Asset.js
Expand Up @@ -257,8 +257,7 @@ class Asset {
// Replace temporary bundle names in the output with the final content-hashed names.
let newValue = value;
for (let [name, map] of bundleNameMap) {
let mapRelative = path.relative(path.dirname(this.relativeName), map);
newValue = newValue.split(name).join(mapRelative);
newValue = newValue.split(name).join(map);
}

// Copy `this.generated` on write so we don't end up writing the final names to the cache.
Expand Down
5 changes: 5 additions & 0 deletions packages/core/parcel-bundler/src/assets/CSSAsset.js
Expand Up @@ -6,6 +6,8 @@ const CssSyntaxError = require('postcss/lib/css-syntax-error');
const SourceMap = require('../SourceMap');
const loadSourceMap = require('../utils/loadSourceMap');
const path = require('path');
const urlJoin = require('../utils/urlJoin');
const isURL = require('../utils/is-url');

const URL_RE = /url\s*\("?(?![a-z]+:)/;
const IMPORT_RE = /@import/;
Expand Down Expand Up @@ -91,6 +93,9 @@ class CSSAsset extends Asset {
let url = this.addURLDependency(node.nodes[0].value, {
loc: decl.source.start
});
if (!isURL(url)) {
url = urlJoin(this.options.publicURL, url);
}
dirty = node.nodes[0].value !== url;
node.nodes[0].value = url;
}
Expand Down

0 comments on commit 68701f6

Please sign in to comment.