Skip to content

Commit

Permalink
Stylistic changes to the PR meteor#4175
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava committed Apr 14, 2015
1 parent 2e8a28c commit 40dd029
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions packages/minifiers/minifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,62 +88,65 @@ CssTools = {
return path && path.charAt(0) !== '/';
};

var rewriteRules = function (rules) {
_.each(rules, function(rule, ruleIndex) {

// do we need to recurse?
if (rule.rules != null) {
rewriteRules(rule.rules);
}

var basePath = pathDirname(rule.position.source);

// Set the correct basePath based on how the linked asset will be served.
// XXX This is wrong. We are coupling the information about how files will
// be served by the web server to the information how they were stored
// originally on the filesystem in the project structure. Ideally, there
// should be some module that tells us precisely how each asset will be
// served but for now we are just assuming that everything that comes from
// a folder starting with "/packages/" is served on the same path as
// it was on the filesystem and everything else is served on root "/".
if (! basePath.match(/^\/?packages\//i))
basePath = "/";

_.each(rule.declarations, function(declaration, declarationIndex) {
var parts, resource, absolutePath, quotes, oldCssUrl, newCssUrl;
var value = declaration.value;

// Match css values containing some functional calls to `url(URI)` where
// URI is optionally quoted.
// Note that a css value can contains other elements, for instance:
// background: top center url("background.png") black;
// or even multiple url(), for instance for multiple backgrounds.
var cssUrlRegex = /url\s*\(\s*(['"]?)(.+?)\1\s*\)/gi;
while (parts = cssUrlRegex.exec(value)) {
oldCssUrl = parts[0];
quotes = parts[1];
resource = url.parse(parts[2]);

// Rewrite relative paths to absolute paths.
// We don't rewrite urls starting with a protocol definition such as
// http, https, or data.
if (isRelative(resource.path) && resource.protocol === null) {
absolutePath = pathJoin(basePath, resource.path);
newCssUrl = "url(" + quotes + absolutePath + quotes + ")";
value = value.replace(oldCssUrl, newCssUrl);
}
}

declaration.value = value;
});
});
};

rewriteRules(ast.stylesheet.rules);

}
};

var rewriteRules = function (rules) {
_.each(rules, function(rule, ruleIndex) {

// Recurse if there are sub-rules. An example:
// @media (...) {
// .rule { url(...); }
// }
if (_.has(rule, rules)) {
rewriteRules(rule.rules);
}

var basePath = pathDirname(rule.position.source);

// Set the correct basePath based on how the linked asset will be served.
// XXX This is wrong. We are coupling the information about how files will
// be served by the web server to the information how they were stored
// originally on the filesystem in the project structure. Ideally, there
// should be some module that tells us precisely how each asset will be
// served but for now we are just assuming that everything that comes from
// a folder starting with "/packages/" is served on the same path as
// it was on the filesystem and everything else is served on root "/".
if (! basePath.match(/^\/?packages\//i))
basePath = "/";

_.each(rule.declarations, function(declaration, declarationIndex) {
var parts, resource, absolutePath, quotes, oldCssUrl, newCssUrl;
var value = declaration.value;

// Match css values containing some functional calls to `url(URI)` where
// URI is optionally quoted.
// Note that a css value can contains other elements, for instance:
// background: top center url("background.png") black;
// or even multiple url(), for instance for multiple backgrounds.
var cssUrlRegex = /url\s*\(\s*(['"]?)(.+?)\1\s*\)/gi;
while (parts = cssUrlRegex.exec(value)) {
oldCssUrl = parts[0];
quotes = parts[1];
resource = url.parse(parts[2]);

// Rewrite relative paths to absolute paths.
// We don't rewrite urls starting with a protocol definition such as
// http, https, or data.
if (isRelative(resource.path) && resource.protocol === null) {
absolutePath = pathJoin(basePath, resource.path);
newCssUrl = "url(" + quotes + absolutePath + quotes + ")";
value = value.replace(oldCssUrl, newCssUrl);
}
}

declaration.value = value;
});
});
};

// These are duplicates of functions in tools/files.js, because we don't have
// a good way of exporting them into packages.
// XXX deduplicate files.js into a package at somepoint so that we can use it
Expand All @@ -169,3 +172,4 @@ var pathJoin = function (a, b) {
var pathDirname = function (p) {
return toStandardPath(path.dirname(toOSPath(p)));
};

0 comments on commit 40dd029

Please sign in to comment.