Skip to content

Commit

Permalink
Made CDN url configurable through environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
simong committed Oct 1, 2014
1 parent 6faf5dc commit 90d2c57
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
94 changes: 54 additions & 40 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,6 @@ var vm = require('vm');

module.exports = function(grunt) {

/**
* Used as a callback funciton by the `grunt-text-replace` plugin. It will
* prepend the CDN url for those static assets that are not delivered by either
* the OAE APIs or other external hosts
*
* @param {String} matchedWord The full match. For example, `src="/shared/oae/css/oae.core.123456.css`
* @param {Number} index The index where the match was found in the `fullText`
* @param {String} fullText The full original text of the file
* @param {String[]} regexMatches The matches that were made by the configured regex
* @return {String} The string that will replace `matchedWord`
* @api private
*/
var _cdnifyStaticAsset = function(matchedWord, index, fullText, regexMatches) {
// Get the name of the attribute (e.g., `src`, `data-main`, ...)
var attr = regexMatches[0];

// Do not replace anything that already points to an outside source
// e.g., do not cdnify src="//www.youtube" or href="https://foo.com/asset.jpg"
if (regexMatches[1].indexOf('/') === 0 || regexMatches[1].indexOf('api') === 0) {
return matchedWord;
}

// The URL of our CDN
var cdn = grunt.config('replace').url;

// Return the attribute with our CDN in it
return util.format('%s="%s/%s"', attr, cdn, regexMatches[1]);
};

// Project configuration.
grunt.initConfig({
'pkg': '<json:package.json>',
Expand Down Expand Up @@ -309,7 +280,7 @@ module.exports = function(grunt) {
}
},
'replace': {
'url': 'https://oaetenant-researchresearch.netdna-ssl.com',
'url': 'URL pointing to a CDN that gets set by the cdn task',
'main': {
'src': [
'target/optimized/ui/*.html',
Expand All @@ -322,19 +293,27 @@ module.exports = function(grunt) {
'replacements': [
{
'from': /(src)="\/(.+?)"/ig,
'to': _cdnifyStaticAsset
'to': function(matchedWord, index, fullText, regexMatches) {
return _cdnifyStaticAsset(grunt.config('replace').url, matchedWord, index, fullText, regexMatches);
}
},
{
'from': /(data-loadmodule)="\/(.+?)"/ig,
'to': _cdnifyStaticAsset
'to': function(matchedWord, index, fullText, regexMatches) {
return _cdnifyStaticAsset(grunt.config('replace').url, matchedWord, index, fullText, regexMatches);
}
},
{
'from': /(data-main)="\/(.+?)"/ig,
'to': _cdnifyStaticAsset
'to': function(matchedWord, index, fullText, regexMatches) {
return _cdnifyStaticAsset(grunt.config('replace').url, matchedWord, index, fullText, regexMatches);
}
},
{
'from': /(href)="\/(.+?)"/ig,
'to': _cdnifyStaticAsset
'to': function(matchedWord, index, fullText, regexMatches) {
return _cdnifyStaticAsset(grunt.config('replace').url, matchedWord, index, fullText, regexMatches);
}
},
{
// Replace the require base URL
Expand Down Expand Up @@ -634,17 +613,25 @@ module.exports = function(grunt) {
grunt.log.writeln('Boots strapped'.green);
});

// Task to update the paths so static assets are pulled from the CDN
// Task to update the paths so static assets are pulled from the CDN.
// The CDN URL can be provided as
// - an environment variable: e.g., `CDN_URL="https://cdn.example.com" grunt cdn`
// - a grunt parameter: e.g., `grunt cdn:https\://cdn.example.com`
// If no CDN is provided, this task will do nothing
grunt.registerTask('cdn', function(url) {
url = url || process.env['CDN_URL'];
if (url) {
// Pass the URL of the CDN to the replacement task
grunt.config.set('replace.url', url);
}

// Start replacing paths
grunt.task.run('replace:main');
grunt.task.run('replace:core-widgets');
grunt.task.run('replace:admin-widgets');
// Start replacing paths
grunt.task.run('replace:main');
grunt.task.run('replace:core-widgets');
grunt.task.run('replace:admin-widgets');
} else {
var msg = 'No cdn provided, not performing cdn task';
grunt.log.writeln(msg.yellow);
}
});

// A task that will copy the release files to a directory of your choosing
Expand Down Expand Up @@ -777,3 +764,30 @@ var _hashFiles = function(options) {

return _.union(globs, options.extra);
};


/**
* Prepend the CDN url for those static assets that are not delivered by either
* the OAE APIs or other external hosts
*
* @param {String} cdn The url of the cdn to prepend before static asset urls
* @param {String} matchedWord The full match. For example, `src="/shared/oae/css/oae.core.123456.css`
* @param {Number} index The index where the match was found in the `fullText`
* @param {String} fullText The full original text of the file
* @param {String[]} regexMatches The matches that were made by the configured regex
* @return {String} The string that will replace `matchedWord`
* @api private
*/
var _cdnifyStaticAsset = function(cdn, matchedWord, index, fullText, regexMatches) {
// Get the name of the attribute (e.g., `src`, `data-main`, ...)
var attr = regexMatches[0];

// Do not replace anything that already points to an outside source
// e.g., do not cdnify src="//www.youtube" or href="https://foo.com/asset.jpg"
if (regexMatches[1].indexOf('/') === 0 || regexMatches[1].indexOf('api') === 0) {
return matchedWord;
}

// Return the attribute with our CDN in it
return util.format('%s="%s/%s"', attr, cdn, regexMatches[1]);
};
9 changes: 8 additions & 1 deletion bin/package
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var argv = require('optimist')
.alias('s', 'skip-tests')
.describe('s', 'Skip the unit tests in the packaging process')

.alias('c', 'cdn')
.describe('c', 'The URL of the CDN that will be used')

.alias('u', 'upload')
.describe('u', 'Upload the package and checksum to Amazon S3')

Expand Down Expand Up @@ -62,14 +65,18 @@ if (!argv.s) {
CoreUtil.logWarn('Skipping unit tests because of -s parameter');
}

// Get the url of the CDN, if any
var cdn = argv.c || '';

// Find the 3akai-ux version. If not specified explicitly in the command with a -v parameter, it will be derived
// using a combination of the package.json version and the closest matching tag using git describe
var uiVersion = argv.v || CoreUtil.gitVersion(packageJson.version, 7);

CoreUtil.logSuccess('Resolved UI version to be '.text + uiVersion.white);

// Loudly optimize the UI into the target directory
CoreUtil.exec('node_modules/.bin/grunt', 'Error during the grunt optimization stage', 8, true);
var cmd = util.format('CDN_URL="%s" node_modules/.bin/grunt', cdn);
CoreUtil.exec(cmd, 'Error during the grunt optimization stage', 8, true);
CoreUtil.logSuccess('Successfully optimized the UI files into the '.text + 'target'.white + ' directory'.text);

// Save the system info into the 3akai-ux target directory. This will tell us at least what version we are
Expand Down
2 changes: 1 addition & 1 deletion etc/scripts/travis-upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ EXPECTED_PULL_REQUEST=false

function package_and_upload {
rm -rf target
bin/package -su --upload-bucket=oae-releases --upload-region=us-east-1
bin/package -su --upload-bucket=oae-releases --upload-region=us-east-1 --cdn https://oaetenant-researchresearch.netdna-ssl.com
}

if [[ "$TRAVIS_REPO_SLUG" == "$EXPECTED_REPOSITORY" && "$TRAVIS_BRANCH" == "$EXPECTED_BRANCH" && "$TRAVIS_PULL_REQUEST" == "$EXPECTED_PULL_REQUEST" ]]; then
Expand Down

0 comments on commit 90d2c57

Please sign in to comment.