From 8fd32c3399bb3bcdd3581d531f18cf752d5d852d Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sun, 7 Sep 2014 20:31:47 +0200 Subject: [PATCH] use only grunt to insert cdnurl --- Gruntfile.js | 42 ++++++++++++++++++++++++------------ _config.yml | 2 -- _src/_plugins/picture_tag.rb | 12 ++++------- package.json | 2 +- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index b7cceca6..3a5f7a25 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -6,13 +6,13 @@ module.exports = function(grunt){ src: '_src', site: '_site', build: '_build', + cdnurl: 'https://d2jlreog722xe2.cloudfront.net', assets: { stylus: 'assets/styl', css: 'assets/css', js: 'assets/js', img: 'assets/img', - fonts: 'assets/fonts', - cdnurl: 'https://d2jlreog722xe2.cloudfront.net/assets/' + fonts: 'assets/fonts' } }; @@ -51,8 +51,7 @@ module.exports = function(grunt){ }, production: { options: { - lsi: true, - raw: 'enable_cdnurl: true\n' + lsi: true } }, development: { @@ -206,16 +205,31 @@ module.exports = function(grunt){ assetsDirs: ['<%= config.build %>', '<%= config.build %>/assets/{css,js,img,fonts}'] } }, - - // CDN some assets - cdn: { - options: { - cdn: '<%= config.assets.cdnurl %>', - flatten: true + + // insert CDN url by replacing text strings + replace: { + html: { + src: ['<%= config.build %>/**/*.html'], + overwrite: true, + replacements: [ + { + from: '/assets/js/', + to: '<%= config.cdnurl %>/assets/js/' + }, + { + from: '/media/', + to: '<%= config.cdnurl %>/media/' + } + ] }, - dist: { - src: [ - '<%= config.build %>/assets/**/*.css' // CDN all assets called from css files + css: { + src: ['<%= config.build %>/<%= config.assets.css %>/*.css'], + overwrite: true, + replacements: [ + { + from: '../', + to: '<%= config.cdnurl %>/assets/' + } ] } }, @@ -303,7 +317,7 @@ module.exports = function(grunt){ 'rsync:copy_build', 'rev', 'usemin', - 'cdn' + 'replace' ]); // Optimze media diff --git a/_config.yml b/_config.yml index dfdc0ab2..23899d6c 100644 --- a/_config.yml +++ b/_config.yml @@ -15,8 +15,6 @@ description: 'Blog of designer & developer Matthias Kretschmann' url: https://kremalicious.com author: Matthias Kretschmann email: m@kretschmann.io -cdnurl: https://d2jlreog722xe2.cloudfront.net -enable_cdnurl: false # Urls diff --git a/_src/_plugins/picture_tag.rb b/_src/_plugins/picture_tag.rb index d60a38bc..31c64a99 100755 --- a/_src/_plugins/picture_tag.rb +++ b/_src/_plugins/picture_tag.rb @@ -142,7 +142,7 @@ module Jekyll # Generate resized images instance.each { |key, source| - instance[key][:generated_src] = generate_image(source, site.source, site.dest, settings['source'], settings['output'], site.config['cdnurl'], site.config["baseurl"], site.config['enable_cdnurl']) + instance[key][:generated_src] = generate_image(source, site.source, site.dest, settings['source'], settings['output'], site.config["baseurl"]) } # Construct and return tag @@ -169,7 +169,7 @@ module Jekyll picture_tag end - def generate_image(instance, site_source, site_dest, image_source, image_dest, cdnurl, baseurl, enable_cdnurl) + def generate_image(instance, site_source, site_dest, image_source, image_dest, baseurl) image = MiniMagick::Image.open(File.join(site_source, image_source, instance[:src])) digest = Digest::MD5.hexdigest(image.to_blob).slice!(0..5) @@ -230,12 +230,8 @@ module Jekyll end # Return path relative to the site root for html - if enable_cdnurl == true - Pathname.new(File.join(cdnurl, baseurl, image_dest, image_dir, gen_name)) - else - Pathname.new(File.join(baseurl, image_dest, image_dir, gen_name)).cleanpath - end - + Pathname.new(File.join(baseurl, image_dest, image_dir, gen_name)).cleanpath + end end end diff --git a/package.json b/package.json index 996e02f3..e6ab2676 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "dependencies": {}, "devDependencies": { "grunt": ">=0.4.5", - "grunt-cdn": ">=0.5.6", "grunt-combine-media-queries": ">=1.0.8", "grunt-contrib-clean": ">=0.5.0", "grunt-contrib-connect": ">=0.5.0", @@ -22,6 +21,7 @@ "grunt-jekyll": ">=0.4.0", "grunt-rev": ">=0.1.0", "grunt-rsync": ">=0.2.1", + "grunt-text-replace": ">=0.3.12", "grunt-usemin": ">=2.0.2", "nib": ">=1.0.3", "stylus": ">=0.45.0"