Skip to content

Commit

Permalink
Merge starsquare pull for #350 cssPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Jan 21, 2013
2 parents a3ba0a8 + e2bb479 commit 0a65c6e
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 12 deletions.
6 changes: 6 additions & 0 deletions build/jslib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,12 @@ define(function (require) {
if (!config.out) {
throw new Error('"out" option missing.');
}
if (config.cssPrefix) {
//Make sure cssPrefix ends in a slash
config.cssPrefix = endsWithSlash(config.cssPrefix);
} else {
config.cssPrefix = '';
}
}
if (!config.cssIn && !config.baseUrl) {
//Just use the current directory as the baseUrl
Expand Down
12 changes: 7 additions & 5 deletions build/jslib/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ function (lang, logger, envOptimize, file, parse,
* @param {String} fileName the file name
* @param {String} fileContents the file contents
* @param {String} cssImportIgnore comma delimited string of files to ignore
* @param {String} cssPrefix string to be prefixed before relative URLs
* @param {Object} included an object used to track the files already imported
*/
function flattenCss(fileName, fileContents, cssImportIgnore, included) {
function flattenCss(fileName, fileContents, cssImportIgnore, cssPrefix, included) {
//Find the last slash in the name.
fileName = fileName.replace(lang.backSlashRegExp, "/");
var endIndex = fileName.lastIndexOf("/"),
Expand Down Expand Up @@ -95,7 +96,7 @@ function (lang, logger, envOptimize, file, parse,
included[fullImportFileName] = true;

//Make sure to flatten any nested imports.
flat = flattenCss(fullImportFileName, importContents, cssImportIgnore, included);
flat = flattenCss(fullImportFileName, importContents, cssImportIgnore, cssPrefix, included);
importContents = flat.fileContents;

if (flat.importList.length) {
Expand Down Expand Up @@ -124,8 +125,9 @@ function (lang, logger, envOptimize, file, parse,
//a protocol.
colonIndex = fixedUrlMatch.indexOf(":");
if (fixedUrlMatch.charAt(0) !== "/" && (colonIndex === -1 || colonIndex > fixedUrlMatch.indexOf("/"))) {
//It is a relative URL, tack on the path prefix
urlMatch = importPath + fixedUrlMatch;
//It is a relative URL, tack on the cssPrefix and path prefix
urlMatch = cssPrefix + importPath + fixedUrlMatch;

} else {
logger.trace(importFileName + "\n URL not a relative URL, skipping: " + urlMatch);
}
Expand Down Expand Up @@ -263,7 +265,7 @@ function (lang, logger, envOptimize, file, parse,

//Read in the file. Make sure we have a JS string.
var originalFileContents = file.readFile(fileName),
flat = flattenCss(fileName, originalFileContents, config.cssImportIgnore, {}),
flat = flattenCss(fileName, originalFileContents, config.cssImportIgnore, config.cssPrefix, {}),
//Do not use the flattened CSS if there was one that was skipped.
fileContents = flat.skippedList.length ? originalFileContents : flat.fileContents,
startIndex, endIndex, buildText, comment;
Expand Down
19 changes: 19 additions & 0 deletions build/tests/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,25 @@ define(['build', 'env!env/file'], function (build, file) {
);
doh.run();

//Tests https://github.com/jrburke/r.js/issues/350 CSS optimizer makes
//url() relative to cssIn option
doh.register("cssPrefix",
[
function cssPrefix(t) {
file.deleteFile("lib/cssPrefix/output/main-built.css");

build(["lib/cssPrefix/build.js"]);

t.is(nol(c("lib/cssPrefix/output/expected.css")),
nol(c("lib/cssPrefix/output/main-built.css")));

require._buildReset();
}

]
);
doh.run();

//Tests https://github.com/jrburke/r.js/issues/296 removeCombined should
//remove files that have been inlined.
doh.register("cssRemoveCombined",
Expand Down
5 changes: 5 additions & 0 deletions build/tests/lib/cssPrefix/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
cssIn: 'input/main.css',
out: 'output/main-built.css',
cssPrefix: '/test'
}
1 change: 1 addition & 0 deletions build/tests/lib/cssPrefix/input/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url(subfolder/sub.css);
3 changes: 3 additions & 0 deletions build/tests/lib/cssPrefix/input/subfolder/sub.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(test.png);
}
3 changes: 3 additions & 0 deletions build/tests/lib/cssPrefix/output/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(/test/subfolder/test.png);
}
3 changes: 3 additions & 0 deletions build/tests/lib/cssPrefix/output/main-built.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(/test/subfolder/test.png);
}
4 changes: 4 additions & 0 deletions build/tests/lib/cssRelativeUrl/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
cssIn: 'input/main.css',
out: 'output/main-built.css'
}
1 change: 1 addition & 0 deletions build/tests/lib/cssRelativeUrl/input/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url(subfolder/sub.css);
3 changes: 3 additions & 0 deletions build/tests/lib/cssRelativeUrl/input/subfolder/sub.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(test.png);
}
3 changes: 3 additions & 0 deletions build/tests/lib/cssRelativeUrl/output/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(../input/subfolder/test.png);
}
3 changes: 3 additions & 0 deletions build/tests/lib/cssRelativeUrl/output/main-built.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
background: url(../input/subfolder/test.png);
}
22 changes: 15 additions & 7 deletions dist/r.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license r.js 2.1.2+ Mon, 21 Jan 2013 06:08:20 GMT Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* @license r.js 2.1.2+ Mon, 21 Jan 2013 21:40:15 GMT Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
Expand All @@ -21,7 +21,7 @@ var requirejs, require, define;

var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire,
nodeDefine, exists, reqMain, loadedOptimizedLib, existsForNode,
version = '2.1.2+ Mon, 21 Jan 2013 06:08:20 GMT',
version = '2.1.2+ Mon, 21 Jan 2013 21:40:15 GMT',
jsSuffixRegExp = /\.js$/,
commandOption = '',
useLibLoaded = {},
Expand Down Expand Up @@ -21202,9 +21202,10 @@ function (lang, logger, envOptimize, file, parse,
* @param {String} fileName the file name
* @param {String} fileContents the file contents
* @param {String} cssImportIgnore comma delimited string of files to ignore
* @param {String} cssPrefix string to be prefixed before relative URLs
* @param {Object} included an object used to track the files already imported
*/
function flattenCss(fileName, fileContents, cssImportIgnore, included) {
function flattenCss(fileName, fileContents, cssImportIgnore, cssPrefix, included) {
//Find the last slash in the name.
fileName = fileName.replace(lang.backSlashRegExp, "/");
var endIndex = fileName.lastIndexOf("/"),
Expand Down Expand Up @@ -21255,7 +21256,7 @@ function (lang, logger, envOptimize, file, parse,
included[fullImportFileName] = true;

//Make sure to flatten any nested imports.
flat = flattenCss(fullImportFileName, importContents, cssImportIgnore, included);
flat = flattenCss(fullImportFileName, importContents, cssImportIgnore, cssPrefix, included);
importContents = flat.fileContents;

if (flat.importList.length) {
Expand Down Expand Up @@ -21284,8 +21285,9 @@ function (lang, logger, envOptimize, file, parse,
//a protocol.
colonIndex = fixedUrlMatch.indexOf(":");
if (fixedUrlMatch.charAt(0) !== "/" && (colonIndex === -1 || colonIndex > fixedUrlMatch.indexOf("/"))) {
//It is a relative URL, tack on the path prefix
urlMatch = importPath + fixedUrlMatch;
//It is a relative URL, tack on the cssPrefix and path prefix
urlMatch = cssPrefix + importPath + fixedUrlMatch;

} else {
logger.trace(importFileName + "\n URL not a relative URL, skipping: " + urlMatch);
}
Expand Down Expand Up @@ -21423,7 +21425,7 @@ function (lang, logger, envOptimize, file, parse,

//Read in the file. Make sure we have a JS string.
var originalFileContents = file.readFile(fileName),
flat = flattenCss(fileName, originalFileContents, config.cssImportIgnore, {}),
flat = flattenCss(fileName, originalFileContents, config.cssImportIgnore, config.cssPrefix, {}),
//Do not use the flattened CSS if there was one that was skipped.
fileContents = flat.skippedList.length ? originalFileContents : flat.fileContents,
startIndex, endIndex, buildText, comment;
Expand Down Expand Up @@ -23176,6 +23178,12 @@ define('build', function (require) {
if (!config.out) {
throw new Error('"out" option missing.');
}
if (config.cssPrefix) {
//Make sure cssPrefix ends in a slash
config.cssPrefix = endsWithSlash(config.cssPrefix);
} else {
config.cssPrefix = '';
}
}
if (!config.cssIn && !config.baseUrl) {
//Just use the current directory as the baseUrl
Expand Down

0 comments on commit 0a65c6e

Please sign in to comment.