Skip to content

Commit b2e170a

Browse files
committed
handles css urls
1 parent 4181d3b commit b2e170a

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

packages/synthesis-compiler/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'mwc:synthesis-compiler',
3-
version: '1.2.1',
3+
version: '1.3.0',
44
summary: 'Synthesis is meteor + polymer',
55
git: 'https://github.com/meteorwebcomponents/synthesis',
66
documentation: 'README.md',

packages/synthesis-compiler/synthesis-compiler.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ class DissectHtml {
165165
const defChild = child;
166166
const attrs = _.map(defChild.attrs, (o) => {
167167
// all src values without [[*]] and {{*}}
168-
if ((o.name === 'src' || o.name === 'src$') && o.value && !o.value.match(/({{|\[\[)\s*[\w\.]+\s*(}}|\]\])/g)) {
169-
const url = self.importableUrl(o.value);
170-
// console.log(defChild.nodeName, o, url);
171-
if (url) {
172-
o.value = path.resolve(path.dirname(`/${self.sourceName}`), o.value);
173-
}
168+
if (o.name === 'src' || o.name === 'src$') {
169+
o.value = self._changeRelUrl(o.value);
174170
}
175171
return o;
176172
});
@@ -186,7 +182,7 @@ class DissectHtml {
186182
return processedNodes.concat(pushNodes);
187183
}
188184
processStyle(css) {
189-
return polyclean.stripCss(css);
185+
return this._changeCssUrls(polyclean.stripCss(css));
190186
}
191187
processScripts(child) {
192188
const self = this;
@@ -268,13 +264,38 @@ class DissectHtml {
268264
}
269265
return null;
270266
}
267+
_changeRelUrl(inpUrl) {
268+
// avoids [[prop]] and {{prop}}
269+
if (inpUrl && !inpUrl.match(/({{|\[\[)\s*[\w\.]+\s*(}}|\]\])/g)) {
270+
// avoids absolute & remote urls
271+
const url = this.importableUrl(inpUrl);
272+
if (url) {
273+
return path.resolve(path.dirname(`/${this.sourceName}`), inpUrl);
274+
}
275+
}
276+
return inpUrl;
277+
278+
}
279+
_changeCssUrls(text) {
280+
const self = this;
281+
// to get -> property: url(filepath)
282+
283+
const processed = text.replace(/url\(.*?\)/ig, function(a) {
284+
// to get -> filepath from url(filepath), url('filepath') and url("filepath")
285+
const processedUrl = a.replace(/\(['|"]?([^)]+?)['|"]?\)/, function(_url, inpUrl) {
286+
return `(${self._changeRelUrl(inpUrl)})`;
287+
});
288+
return processedUrl;
289+
});
290+
return processed;
291+
}
271292
processCssImport(hrefAttr, child) {
272293
const url = path.resolve(this.sourceName, '../', hrefAttr.value);
273294
// checks if file exists
274295
if (fs.existsSync(url)) {
275296
const contents = fs.readFileSync(url, 'utf8');
276297
// css is inlined
277-
const minified = this.processStyle(contents);
298+
const minified = this._changeCssUrls(this.processStyle(contents));
278299
if (minified) {
279300
// link tag is replaced with style tag
280301
return _.extend(child, {

packages/synthesis-jade/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'mwc:synthesis-jade',
3-
version: '1.2.4',
3+
version: '1.3.0',
44
summary: 'Synthesis is meteor + polymer',
55
git: 'https://github.com/meteorwebcomponents/synthesis',
66
documentation: 'README.md',
@@ -25,7 +25,7 @@ Package.onTest((api) => {
2525
Package.registerBuildPlugin({
2626
name: 'synthesis-jade',
2727
use: [
28-
'mwc:synthesis-compiler@1.2.1',
28+
'mwc:synthesis-compiler@1.3.0',
2929
3030
3131
],

packages/synthesis/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'mwc:synthesis',
3-
version: '1.2.4',
3+
version: '1.3.0',
44
summary: 'Synthesis is meteor + polymer',
55
git: 'https://github.com/meteorwebcomponents/synthesis',
66
documentation: 'README.md',
@@ -25,7 +25,7 @@ Package.onTest((api) => {
2525
Package.registerBuildPlugin({
2626
name: 'synthesis',
2727
use: [
28-
'mwc:synthesis-compiler@1.2.1',
28+
'mwc:synthesis-compiler@1.3.0',
2929
3030
3131
],

0 commit comments

Comments
 (0)