Last active
December 13, 2015 21:18
-
-
Save 2no/4976528 to your computer and use it in GitHub Desktop.
某サイトのマネ。Resource.get をテンプレートに用意して、更新日時を src の最後に追加してキャッシュ対策。
$ npm install grunt-contrib grunt-templater ejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function(grunt) { | |
| 'use strict'; | |
| var | |
| /** | |
| * ルートパス | |
| * | |
| * @constant | |
| * @type {string} | |
| */ | |
| ROOT_PATH = '.', | |
| /** | |
| * テンプレートパス | |
| * | |
| * @constant | |
| * @type {string} | |
| */ | |
| TEMPLATE_PATH = ROOT_PATH + '/templates', | |
| /** | |
| * テンプレート変数 | |
| * | |
| * @type {object} | |
| */ | |
| variables = { | |
| Resource: { | |
| get: function(src) { | |
| var stat, | |
| filename = process.cwd() + '/' + src, | |
| fs = require('fs'); | |
| try { | |
| stat = fs.statSync(filename); | |
| src += '?' + new Date(stat.mtime).getTime(); | |
| } | |
| catch (e) { | |
| console.log(e.message); | |
| } | |
| return src; | |
| } | |
| } | |
| }; | |
| grunt.file.setBase(ROOT_PATH); | |
| grunt.initConfig({ | |
| watch: { | |
| templates: { | |
| files: [TEMPLATE_PATH + '/**/*.ejs'], | |
| tasks: 'template', | |
| }, | |
| }, | |
| template: { | |
| test: { | |
| src: TEMPLATE_PATH + '/test.ejs', | |
| dest: ROOT_PATH + '/test.html', | |
| variables: merge(variables, { | |
| // テンプレート毎に変数が必要であれば、 | |
| // '変数名': 代入する値 | |
| }), | |
| }, | |
| } | |
| }); | |
| grunt.loadNpmTasks('grunt-templater'); | |
| grunt.registerTask('default', 'template'); | |
| function merge() | |
| { | |
| var result = {}, i, iz, key; | |
| for (i = 0, iz = arguments.length; i < iz; i++) { | |
| if (arguments[i] instanceof Object) { | |
| for (key in arguments[i]) { | |
| if (arguments[i].hasOwnProperty(key)) { | |
| result[key] = arguments[i][key]; | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- templates/test.ejs --> | |
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title></title> | |
| </head> | |
| <body> | |
| <img src="<%= Resource.get('img/test.png') %>"> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- 出来上がり --> | |
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title></title> | |
| </head> | |
| <body> | |
| <img src="img/test.png?1361182310000"> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment