From 77306cc819ded34e19dd6c2322731ba2ccd3f138 Mon Sep 17 00:00:00 2001 From: Chris Buckley Date: Tue, 15 Jan 2013 23:57:17 +0000 Subject: [PATCH 1/2] Test cases for #350 --- build/tests/builds.js | 38 +++++++++++++++++++ build/tests/lib/cssPrefix/build.js | 5 +++ build/tests/lib/cssPrefix/input/main.css | 1 + .../lib/cssPrefix/input/subfolder/sub.css | 3 ++ build/tests/lib/cssPrefix/output/expected.css | 3 ++ .../tests/lib/cssPrefix/output/main-built.css | 3 ++ build/tests/lib/cssRelativeUrl/build.js | 4 ++ build/tests/lib/cssRelativeUrl/input/main.css | 1 + .../cssRelativeUrl/input/subfolder/sub.css | 3 ++ .../lib/cssRelativeUrl/output/expected.css | 3 ++ .../lib/cssRelativeUrl/output/main-built.css | 3 ++ 11 files changed, 67 insertions(+) create mode 100644 build/tests/lib/cssPrefix/build.js create mode 100644 build/tests/lib/cssPrefix/input/main.css create mode 100644 build/tests/lib/cssPrefix/input/subfolder/sub.css create mode 100644 build/tests/lib/cssPrefix/output/expected.css create mode 100644 build/tests/lib/cssPrefix/output/main-built.css create mode 100644 build/tests/lib/cssRelativeUrl/build.js create mode 100644 build/tests/lib/cssRelativeUrl/input/main.css create mode 100644 build/tests/lib/cssRelativeUrl/input/subfolder/sub.css create mode 100644 build/tests/lib/cssRelativeUrl/output/expected.css create mode 100644 build/tests/lib/cssRelativeUrl/output/main-built.css diff --git a/build/tests/builds.js b/build/tests/builds.js index 35f50779..94acb414 100644 --- a/build/tests/builds.js +++ b/build/tests/builds.js @@ -981,6 +981,44 @@ 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/350 CSS optimizer makes + //url() relative to cssIn option + doh.register("cssRelativeUrl", + [ + function cssPrefix(t) { + file.deleteFile("lib/cssRelativeUrl/output/main-built.css"); + + build(["lib/cssRelativeUrl/build.js"]); + + t.is(nol(c("lib/cssRelativeUrl/output/expected.css")), + nol(c("lib/cssRelativeUrl/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", diff --git a/build/tests/lib/cssPrefix/build.js b/build/tests/lib/cssPrefix/build.js new file mode 100644 index 00000000..f0e66085 --- /dev/null +++ b/build/tests/lib/cssPrefix/build.js @@ -0,0 +1,5 @@ +{ + cssIn: 'input/main.css', + out: 'output/main-built.css', + cssPrefix: '/test' +} diff --git a/build/tests/lib/cssPrefix/input/main.css b/build/tests/lib/cssPrefix/input/main.css new file mode 100644 index 00000000..73222244 --- /dev/null +++ b/build/tests/lib/cssPrefix/input/main.css @@ -0,0 +1 @@ +@import url(subfolder/sub.css); diff --git a/build/tests/lib/cssPrefix/input/subfolder/sub.css b/build/tests/lib/cssPrefix/input/subfolder/sub.css new file mode 100644 index 00000000..6426c8d2 --- /dev/null +++ b/build/tests/lib/cssPrefix/input/subfolder/sub.css @@ -0,0 +1,3 @@ +.test { + background: url(test.png); +} diff --git a/build/tests/lib/cssPrefix/output/expected.css b/build/tests/lib/cssPrefix/output/expected.css new file mode 100644 index 00000000..c39cfcd3 --- /dev/null +++ b/build/tests/lib/cssPrefix/output/expected.css @@ -0,0 +1,3 @@ +.test { + background: url(/test/subfolder/test.png); +} diff --git a/build/tests/lib/cssPrefix/output/main-built.css b/build/tests/lib/cssPrefix/output/main-built.css new file mode 100644 index 00000000..c39cfcd3 --- /dev/null +++ b/build/tests/lib/cssPrefix/output/main-built.css @@ -0,0 +1,3 @@ +.test { + background: url(/test/subfolder/test.png); +} diff --git a/build/tests/lib/cssRelativeUrl/build.js b/build/tests/lib/cssRelativeUrl/build.js new file mode 100644 index 00000000..b4528b44 --- /dev/null +++ b/build/tests/lib/cssRelativeUrl/build.js @@ -0,0 +1,4 @@ +{ + cssIn: 'input/main.css', + out: 'output/main-built.css' +} diff --git a/build/tests/lib/cssRelativeUrl/input/main.css b/build/tests/lib/cssRelativeUrl/input/main.css new file mode 100644 index 00000000..73222244 --- /dev/null +++ b/build/tests/lib/cssRelativeUrl/input/main.css @@ -0,0 +1 @@ +@import url(subfolder/sub.css); diff --git a/build/tests/lib/cssRelativeUrl/input/subfolder/sub.css b/build/tests/lib/cssRelativeUrl/input/subfolder/sub.css new file mode 100644 index 00000000..6426c8d2 --- /dev/null +++ b/build/tests/lib/cssRelativeUrl/input/subfolder/sub.css @@ -0,0 +1,3 @@ +.test { + background: url(test.png); +} diff --git a/build/tests/lib/cssRelativeUrl/output/expected.css b/build/tests/lib/cssRelativeUrl/output/expected.css new file mode 100644 index 00000000..c901eb65 --- /dev/null +++ b/build/tests/lib/cssRelativeUrl/output/expected.css @@ -0,0 +1,3 @@ +.test { + background: url(../input/subfolder/test.png); +} diff --git a/build/tests/lib/cssRelativeUrl/output/main-built.css b/build/tests/lib/cssRelativeUrl/output/main-built.css new file mode 100644 index 00000000..c901eb65 --- /dev/null +++ b/build/tests/lib/cssRelativeUrl/output/main-built.css @@ -0,0 +1,3 @@ +.test { + background: url(../input/subfolder/test.png); +} From e2bb4797063390d3709e9fb0ab59b12c9a5f4f71 Mon Sep 17 00:00:00 2001 From: Chris Buckley Date: Wed, 16 Jan 2013 01:25:20 +0000 Subject: [PATCH 2/2] Remove cssRelativeUrl test case, implement cssPrefix option (Fixes #350) --- build/jslib/build.js | 6 ++++++ build/jslib/optimize.js | 12 +++++++----- build/tests/builds.js | 19 ------------------- dist/r.js | 22 +++++++++++++++------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/build/jslib/build.js b/build/jslib/build.js index 56ed5e29..5b6b9bd7 100644 --- a/build/jslib/build.js +++ b/build/jslib/build.js @@ -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 diff --git a/build/jslib/optimize.js b/build/jslib/optimize.js index 58924611..f3bf5042 100644 --- a/build/jslib/optimize.js +++ b/build/jslib/optimize.js @@ -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("/"), @@ -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) { @@ -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); } @@ -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; diff --git a/build/tests/builds.js b/build/tests/builds.js index 94acb414..05200c0c 100644 --- a/build/tests/builds.js +++ b/build/tests/builds.js @@ -1000,25 +1000,6 @@ 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("cssRelativeUrl", - [ - function cssPrefix(t) { - file.deleteFile("lib/cssRelativeUrl/output/main-built.css"); - - build(["lib/cssRelativeUrl/build.js"]); - - t.is(nol(c("lib/cssRelativeUrl/output/expected.css")), - nol(c("lib/cssRelativeUrl/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", diff --git a/dist/r.js b/dist/r.js index b13fbe8b..e18a1cf1 100644 --- a/dist/r.js +++ b/dist/r.js @@ -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 */ @@ -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 = {}, @@ -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("/"), @@ -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) { @@ -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); } @@ -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; @@ -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