Skip to content

Commit

Permalink
Add urlPrefixes option (#234)
Browse files Browse the repository at this point in the history
* adding option cdnPath from Version 0.4.12 since updates from 1.5.0 break the desired behavior on asset-files uploaded to cdn
* renaming cdnPath to urlPrefixes and changed it to Array of Strings
* updating travis to currently maintained versions of nodejs
  • Loading branch information
lietzi authored and benhoIIand committed Jun 11, 2018
1 parent 4e3561a commit 7845750
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 12 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: node_js
sudo: false
node_js:
- "7"
- "10"
- "8"
- "6"
- "5"
- "4"
before_script:
- npm install -g grunt-cli
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ defaults. Extra details are below.
separator: '.', // The separator between the original file name and hash
queryString: false // Use a query string for cache busting instead of rewriting files
outputDir: '' // Directory where all hashed assets will be copied. Is relative to baseDir
clearOutputDir: false // Clear output directory. If outputDir was not set clear will not work
clearOutputDir: false, // Clear output directory. If outputDir was not set clear will not work
urlPrefixes: ['http://owncdn.test.com/path'] // Array of Url + Path of own CDNs where hashed files are uploaded to.
}
```

Expand Down Expand Up @@ -125,6 +126,11 @@ assets: {
}]
}
```
#### options.urlPrefixes
Type: `Array of Strings`
Default value: ``

When set, `cachebust` will try to find the assets with this prefixes - useful for e.g. asset-files uploaded to a separate cdn.

#### options.createCopies
Type: `Boolean`
Expand Down
3 changes: 2 additions & 1 deletion config/relativepath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = {
assets: ['assets/**'],
baseDir: 'tmp/relativepath',
deleteOriginals: true,
hash: '123456789'
hash: '123456789',
jsonOutput: true
},
files: [{
expand: true,
Expand Down
14 changes: 14 additions & 0 deletions config/urlPrefixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
options: {
assets: ['**/*.{css,jpg}'],
baseDir: 'tmp/urlPrefixes',
urlPrefixes: ['http://owncdn1.test.com/path', 'http://owncdn2.test.com/path'],
deleteOriginals: true,
hash: '123456789'
},
files: [{
expand: true,
cwd: 'tmp/urlPrefixes',
src: ['css/*.css', '*.html']
}]
};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"test": "grunt test"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt": "^1.0.3",
"grunt-contrib-clean": "^1.1.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-nodeunit": "^1.0.0",
"grunt-contrib-watch": "^1.0.0"
"grunt-contrib-nodeunit": "^2.0.0",
"grunt-contrib-watch": "^1.1.0"
},
"peerDependencies": {
"grunt": ">=0.4.0"
Expand All @@ -38,6 +38,6 @@
"bust assets"
],
"dependencies": {
"fs-extra": "^3.0.1"
"fs-extra": "^6.0.1"
}
}
18 changes: 15 additions & 3 deletions tasks/cachebust.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var DEFAULT_OPTIONS = {
separator: '.',
queryString: false,
outputDir: '',
clearOutputDir: false
clearOutputDir: false,
urlPrefixes: []
};

module.exports = function(grunt) {
Expand Down Expand Up @@ -61,6 +62,7 @@ module.exports = function(grunt) {
// "{file}"
// '{file}'
// ({file}) (css url(...))
// /{file} (css url(...))
// ={file}> (unquoted html attribute)
// ={file}\s (unquoted html attribute fonllowed by more attributes)
// "{file}\s (first entry of img srcset)
Expand All @@ -73,8 +75,19 @@ module.exports = function(grunt) {
['=', '>'],
['=', ' '],
['"', ' '],
[' ', ' '],
[' ', ' ']
];

// add urlPrefixes to enclosing scenarios
if (opts.urlPrefixes && Array.isArray(opts.urlPrefixes) && opts.urlPrefixes.length > 0) {
opts.urlPrefixes.forEach(function(urlPrefix) {
replaceEnclosedBy.push([urlPrefix, '"']);
replaceEnclosedBy.push([urlPrefix, "'"]);
replaceEnclosedBy.push([urlPrefix, ")"]);
replaceEnclosedBy.push([urlPrefix, ">"]);
replaceEnclosedBy.push([urlPrefix, " "]);
});
}
// don't replace references that are already cache busted
if (!isUsingQueryString(opts)) {
replaceEnclosedBy = replaceEnclosedBy.concat(replaceEnclosedBy.map(function(reb) {
Expand Down Expand Up @@ -122,7 +135,6 @@ module.exports = function(grunt) {
replace.push([dir + originalFilename, dir + hashedFilename]);
}
}

_.each(replace, function(r) {
var original = r[0];
var hashed = r[1];
Expand Down
Binary file added tests/urlPrefixes/assets/imgsrcset-big.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/urlPrefixes/assets/imgsrcset-small.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/urlPrefixes/assets/imgsrcset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/urlPrefixes/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: red;
}
11 changes: 11 additions & 0 deletions tests/urlPrefixes/urlPrefixes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Realpath example</title>
<link rel="stylesheet" type="text/css" href="/css/app.css">
<link rel="stylesheet" type="text/css" href="http://owncdn1.test.com/path/css/app.css">
</head>
<body>
<img src="http://owncdn1.test.com/path/assets/imgsrcset.jpg" srcset="http://owncdn2.test.com/path/assets/imgsrcset-small.jpg 400w, http://owncdn1.test.com/path/assets/imgsrcset-big.jpg 900w" alt="img srcset with urlPrefix example">
</body>
</html>
20 changes: 20 additions & 0 deletions tests/urlPrefixes/urlPrefixes_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var grunt = require('grunt');

module.exports = {

urlPrefixes: function(test) {
var markup = grunt.file.read('tmp/urlPrefixes/urlPrefixes.html');

test.ok(markup.match(/\/css\/app\.123456789\.css\"/), 'testing /css/app.css replaced');
test.ok(markup.match(/http:\/\/owncdn1\.test\.com\/path\/css\/app\.123456789\.css\"/), 'testing http://owncdn1.test.com/path/css/app.css replaced');

test.ok(markup.match(/http:\/\/owncdn1\.test\.com\/path\/assets\/imgsrcset\.123456789\.jpg/), 'testing srcset assets/imgsrcset.jpg');
test.ok(markup.match(/http:\/\/owncdn1\.test\.com\/path\/assets\/imgsrcset-big\.123456789\.jpg/), 'testing srcset assets/imgsrcset-big.jpg');
test.ok(markup.match(/http:\/\/owncdn2\.test\.com\/path\/assets\/imgsrcset-small\.123456789\.jpg/), 'testing srcset assets/imgsrcset-small.jpg');

test.done();
}

};

0 comments on commit 7845750

Please sign in to comment.