Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build/jslib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,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 @@ -43,9 +43,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 @@ -96,7 +97,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 @@ -125,8 +126,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 @@ -256,7 +258,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+ Tue, 15 Jan 2013 01:33:33 GMT Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* @license r.js 2.1.2+ Wed, 16 Jan 2013 01:24:27 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+ Tue, 15 Jan 2013 01:33:33 GMT',
version = '2.1.2+ Wed, 16 Jan 2013 01:24:27 GMT',
jsSuffixRegExp = /\.js$/,
commandOption = '',
useLibLoaded = {},
Expand Down Expand Up @@ -21201,9 +21201,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 @@ -21254,7 +21255,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 @@ -21283,8 +21284,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 @@ -21414,7 +21416,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 @@ -23170,6 +23172,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