├── .gitignore ├── .travis.yml ├── test.js ├── .jshintrc ├── Gruntfile.js ├── package.json ├── tasks └── cdnify.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | before_install: 5 | - npm install -g grunt-cli 6 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.cdnify = { 4 | loads: function (test) { 5 | var cdnify = require('./tasks/cdnify'); 6 | test.ok(cdnify !== undefined); 7 | 8 | test.done(); 9 | } 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "browser": true, 4 | "esnext": true, 5 | "bitwise": true, 6 | "camelcase": true, 7 | "curly": true, 8 | "eqeqeq": true, 9 | "immed": true, 10 | "indent": 2, 11 | "latedef": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "quotmark": "single", 15 | "regexp": true, 16 | "undef": true, 17 | "unused": true, 18 | "strict": true 19 | } 20 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (grunt) { 3 | grunt.initConfig({ 4 | jshint: { 5 | options: { 6 | jshintrc: '.jshintrc' 7 | }, 8 | all: [ 9 | '*.js', 10 | 'tasks/*.js', 11 | ] 12 | }, 13 | nodeunit: { 14 | tests: ['test.js'] 15 | } 16 | }); 17 | 18 | grunt.loadTasks('tasks'); 19 | 20 | grunt.loadNpmTasks('grunt-contrib-jshint'); 21 | grunt.loadNpmTasks('grunt-contrib-nodeunit'); 22 | 23 | grunt.registerTask('test', ['nodeunit']); 24 | grunt.registerTask('default', ['jshint', 'test']); 25 | }; 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-google-cdn", 3 | "version": "0.4.3", 4 | "description": "Grunt task for replacing refs to resources on the Google CDN", 5 | "scripts": { 6 | "test": "grunt test" 7 | }, 8 | "devDependencies": { 9 | "each-async": "^1.1.1", 10 | "grunt": "~0.4.0", 11 | "grunt-contrib-jshint": "^0.11.2", 12 | "grunt-contrib-nodeunit": "^0.4.1", 13 | "proxyquire": "^1.3.0" 14 | }, 15 | "dependencies": { 16 | "bower": ">=1.0.0", 17 | "chalk": "^1.0.0", 18 | "google-cdn": "^1.0.0" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git://github.com/btford/grunt-google-cdn" 23 | }, 24 | "engines": { 25 | "node": ">=0.10.0", 26 | "npm": ">=1.4.3" 27 | }, 28 | "keywords": [ 29 | "grunt", 30 | "google", 31 | "cdn" 32 | ], 33 | "files": [ 34 | "tasks/cdnify.js" 35 | ], 36 | "author": "Brian Ford", 37 | "license": "BSD" 38 | } 39 | -------------------------------------------------------------------------------- /tasks/cdnify.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var eachAsync = require('each-async'); 5 | var googlecdn = require('google-cdn'); 6 | var bowerConfig = require('bower').config; 7 | var chalk = require('chalk'); 8 | 9 | module.exports = function (grunt) { 10 | 11 | grunt.registerMultiTask('cdnify', 'Replace scripts with refs to the Google CDN', function () { 12 | // collect files 13 | var files = grunt.file.expand({ filter: 'isFile' }, this.data.html); 14 | var compJson = grunt.file.readJSON('bower.json'); 15 | var options = this.options({ 16 | cdn: 'google' 17 | }); 18 | 19 | // Strip the leading path segment off, e.g. `app/bower_components` -> 20 | // `bower_components` 21 | var bowerDirBits = bowerConfig.directory.split(path.sep); 22 | bowerDirBits.shift(); 23 | var componentsPath = bowerDirBits.join(path.sep); 24 | 25 | grunt.log 26 | .writeln('Going through ' + grunt.log.wordlist(files) + ' to update script refs'); 27 | 28 | files = files.map(function (filepath) { 29 | return { 30 | path: filepath, 31 | body: grunt.file.read(filepath) 32 | }; 33 | }); 34 | 35 | eachAsync(files, function (file, index, cbInner) { 36 | var content = file.body; 37 | 38 | content = googlecdn(content, compJson, { 39 | componentsPath: componentsPath, 40 | cdn: options.cdn 41 | }, function (err, content, replacements) { 42 | if (err) { 43 | return cbInner(err); 44 | } 45 | 46 | if (replacements.length > 0) { 47 | replacements.forEach(function (replacement) { 48 | grunt.log.writeln(chalk.green('✔ ') + replacement.from + chalk.gray(' changed to ') + replacement.to); 49 | }); 50 | } 51 | 52 | grunt.file.write(file.path, content); 53 | cbInner(); 54 | }); 55 | }, this.async()); 56 | }); 57 | }; 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-google-cdn [![Build Status](https://travis-ci.org/btford/grunt-google-cdn.png)](https://travis-ci.org/btford/grunt-google-cdn) 2 | Grunt task for replacing refs to resources on the [Google CDN](https://developers.google.com/speed/libraries/devguide) 3 | 4 | ## Getting Started 5 | This plugin requires Grunt `~0.4.0` 6 | 7 | If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: 8 | 9 | ```shell 10 | npm install grunt-google-cdn 11 | ``` 12 | 13 | Install CDN data module: `npm install google-cdn-data` (see list of more [data modules](#cdn-data-modules) below) 14 | 15 | It manages dependencies using [Bower](http://bower.io/), be sure to have it installed, and a bower.json/component.json in your project. 16 | 17 | 18 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: 19 | 20 | ```js 21 | grunt.loadNpmTasks('grunt-google-cdn'); 22 | ``` 23 | 24 | Run this task with the `grunt cdnify` command. 25 | 26 | ### Configuration 27 | 28 | Within your Gruntfile.js file, you need to specify the html directory that contains the html referencing your CDN files. 29 | 30 | ```js 31 | cdnify: { 32 | options: { 33 | cdn: require('google-cdn-data') 34 | } 35 | dist: { 36 | html: ['app/*.html'] 37 | } 38 | } 39 | ``` 40 | You will need a valid bower.json/component.json file in your project, that has dependencies and a version listed accordingly 41 | 42 | ```json 43 | { 44 | "name": "myAwesomeApp", 45 | "version": "0.0.1", 46 | "dependencies": { 47 | "rsvp": "*", 48 | "carve": ">=0.0.1", 49 | "es5-shim": "~2.0.8", 50 | "optimist" : "0.2.x" 51 | } 52 | ``` 53 | 54 | If any updates are found, it will go through the files you specified, updating any references to those scripts. 55 | 56 | 57 | ### Options 58 | 59 | - `cdn`: defaults to `require('google-cdn-data')`. CDN you want to use. Object of the following format: 60 | 61 | ```javascript 62 | { 63 | jquery: { 64 | versions: ['2.0.3', '2.0.2', '2.0.1', '2.0.0'], 65 | url: function (version) { 66 | return '//my.own.cdn/libs/jquery/' + version + '/jquery.min.js'; 67 | } 68 | } 69 | } 70 | ``` 71 | For options consult the [google-cdn docs](https://github.com/passy/google-cdn#api). 72 | 73 | ## CDN data modules 74 | 75 | - [google-cdn-data](https://github.com/shahata/google-cdn-data) 76 | - [cdnjs-cdn-data](https://github.com/shahata/cdnjs-cdn-data) 77 | - [jsdelivr-cdn-data](https://github.com/shahata/jsdelivr-cdn-data) 78 | 79 | ## Release History 80 | 81 | * 2013-04-24   v0.1.4   removed the extra s in component.json 82 | * 2013-04-22   v0.1.3   Made 'components.json' configurable via bowerrc, added unstable AngularJS and jQuery 2.0.0 83 | * 2013-04-07   v0.1.2   update available AngularJS versions, add .jshintrc 84 | * 2013-02-24   v0.1.0   add support for versions, hositing to avoid conflicts with usemin 85 | * 2013-02-18   v0.0.1   Initial Commit 86 | 87 | --- 88 | 89 | ## License 90 | BSD 91 | --------------------------------------------------------------------------------