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

Correctly build dependency URLs (for CSS) #2740

Merged
merged 2 commits into from Mar 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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