-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from spmjs/hash
Hash
- Loading branch information
Showing
148 changed files
with
7,904 additions
and
636 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
exports.style = require('./tasks/lib/style'); | ||
exports.script = require('./tasks/lib/script'); | ||
exports.css2js = require('./tasks/lib/css2js'); | ||
exports.template = require('./tasks/lib/template'); | ||
exports.text = require('./tasks/lib/text'); | ||
exports.handlebars = require('./tasks/lib/handlebars'); | ||
exports.json = require('./tasks/lib/json'); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
exports.init = function(grunt, options) { | ||
var extname = require('path').extname; | ||
var format = require('util').format; | ||
var ast = require('cmd-util').ast; | ||
var md5 = require('./util').md5; | ||
var type = options.type; | ||
var factoryParser = options.factoryParser; | ||
var depParser = options.depParser; | ||
var regType = new RegExp('.' + type + '$'); | ||
var retTypeJs = new RegExp('.' + type + '.js$'); | ||
|
||
var exports = {}; | ||
|
||
exports[type + 'Parser'] = function(fileObj, options) { | ||
var filepath, id = unixy(options.idleading + fileObj.name.replace(/\.js$/, '')); | ||
var data = fileObj.srcData || grunt.file.read(fileObj.src); | ||
var hash = md5(data); | ||
var deps = depParser ? depParser(data, options) : ''; | ||
var factory = factoryParser ? factoryParser(data, options, fileObj) : '{}'; | ||
var file = { | ||
contents: format('define("%s", [%s], %s)', id, deps, factory), | ||
dest: fileObj.dest + '.js' | ||
}; | ||
|
||
if (!options.hash) { | ||
|
||
// create .{type}.js | ||
data = ast.modify(file.contents, { | ||
id: id | ||
}).print_to_string(options.uglify); | ||
filepath = file.dest; | ||
writeFile(data, filepath); | ||
} else { | ||
|
||
// create hash file xxx-{hash}.{type}.js | ||
if (options.hash) { | ||
filepath = file.dest.replace(retTypeJs, '-' + hash + '.' + type + '.js'); | ||
data = ast.modify(file.contents, { | ||
id: id.replace(regType, '-' + hash + '.' + type) | ||
}).print_to_string(options.uglify); | ||
writeFile(data, filepath); | ||
} | ||
} | ||
|
||
// create debug file xxx-debug.{type}.js | ||
if (options.debug) { | ||
data = ast.modify(data, addDebug).print_to_string(options.uglify); | ||
filepath = filepath.replace(retTypeJs, '-debug.' + type + '.js'); | ||
writeFile(data, filepath); | ||
} | ||
|
||
// { | ||
// id: id.replace(regType, '-debug.' + type), | ||
// dependencies: function(id) { | ||
// return id + '-debug'; | ||
// }, | ||
// require: function(id) { | ||
// return id + '-debug'; | ||
// } | ||
// } | ||
function addDebug(v) { | ||
var ext = extname(v); | ||
if (ext && options.parsers[ext]) { | ||
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext); | ||
} else { | ||
return v + '-debug'; | ||
} | ||
} | ||
|
||
}; | ||
return exports; | ||
|
||
function writeFile(data, dest) { | ||
grunt.log.writeln('transport ' + dest + ' created'); | ||
grunt.file.write(dest, data + '\n'); | ||
} | ||
}; | ||
|
||
function unixy(uri) { | ||
return uri.replace(/\\/g, '/'); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var format = require('util').format; | ||
var css2str = require('css2str'); | ||
var cleancss = require('clean-css'); | ||
var commonParser = require('./common'); | ||
|
||
exports.init = function(grunt) { | ||
return commonParser.init(grunt, { | ||
type: 'css', | ||
factoryParser: css2js | ||
}); | ||
}; | ||
exports.css2js = function(code, id, options, fileObj) { | ||
var tpl = [ | ||
'define("%s", [], function() {', | ||
"seajs.importStyle('%s')", | ||
'});' | ||
].join('\n'); | ||
return format(tpl, id, css2js(code, options, fileObj)); | ||
}; | ||
|
||
function css2js(code, options, fileObj) { | ||
var addStyleBox = false; | ||
if (options.styleBox === true) { | ||
addStyleBox = true; | ||
} else if (options.styleBox && options.styleBox.length) { | ||
options.styleBox.forEach(function(file) { | ||
if (file === fileObj.name) { | ||
addStyleBox = true; | ||
} | ||
}); | ||
} | ||
|
||
// if outside css modules, fileObj would be undefined | ||
// then dont add styleBox | ||
var opt = {}; | ||
if (addStyleBox && fileObj) { | ||
// ex. arale/widget/1.0.0/ => arale-widget-1_0_0 | ||
var styleId = unixy((options || {}).idleading || '') | ||
.replace(/\/$/, '') | ||
.replace(/\//g, '-') | ||
.replace(/\./g, '_'); | ||
opt.prefix = ['.', styleId, ' '].join(''); | ||
} | ||
|
||
code = css2str(code, opt); | ||
|
||
// remove comment and format | ||
code = cleancss.process(code, { | ||
keepSpecialComments: 0, | ||
removeEmpty: true | ||
}); | ||
|
||
// transform css to js | ||
// spmjs/spm#581 | ||
var template = 'function() {seajs.importStyle(\'%s\')}'; | ||
return format(template, code); | ||
} | ||
|
||
function unixy(uri) { | ||
return uri.replace(/\\/g, '/'); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var commonParser = require('./common'); | ||
|
||
exports.init = function(grunt) { | ||
var format = require('util').format; | ||
var handlebars = require('handlebars'); | ||
|
||
return commonParser.init(grunt, { | ||
type: 'handlebars', | ||
depParser: function(data, options) { | ||
// handlebars alias | ||
return '"' + options.handlebars.id + '"'; | ||
}, | ||
factoryParser: function(data, options) { | ||
patchHandlebars(handlebars); | ||
var code = handlebars.precompile(data, options.handlebars); | ||
var template = [ | ||
'function(require, exports, module) {', | ||
'var Handlebars = require("%s");', | ||
'var template = Handlebars.template;', | ||
'module.exports = template(%s);', | ||
'}' | ||
].join('\n'); | ||
return format(template, options.handlebars.id, code); | ||
} | ||
}); | ||
}; | ||
|
||
// patch for handlebars | ||
function patchHandlebars(Handlebars) { | ||
Handlebars.JavaScriptCompiler.prototype.preamble = function() { | ||
var out = []; | ||
|
||
if (!this.isChild) { | ||
var namespace = this.namespace; | ||
// patch for handlebars | ||
var copies = [ | ||
"helpers = helpers || {};", | ||
"for (var key in " + namespace + ".helpers) {", | ||
" helpers[key] = helpers[key] || " + namespace + ".helpers[key];", | ||
"}" | ||
].join('\n'); | ||
if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } | ||
if (this.options.data) { copies = copies + " data = data || {};"; } | ||
out.push(copies); | ||
} else { | ||
out.push(''); | ||
} | ||
|
||
if (!this.environment.isSimple) { | ||
out.push(", buffer = " + this.initializeBuffer()); | ||
} else { | ||
out.push(""); | ||
} | ||
|
||
// track the last context pushed into place to allow skipping the | ||
// getContext opcode when it would be a noop | ||
this.lastContext = 0; | ||
this.source = out; | ||
}; | ||
} |
Oops, something went wrong.