Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash #88

Merged
merged 14 commits into from
Jan 22, 2015
Merged

Hash #88

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"latedef": false,
"newcap": true,
"quotmark": false,
"undef": false,
"unused": false,
"undef": true,
"unused": true,
"trailing": true,
"lastsemic": true,
"asi": false,
Expand Down
60 changes: 52 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

module.exports = function(grunt) {

var style = require('./').style.init(grunt);
var css2jsParser = style.css2jsParser;
var css2js = require('./').css2js.init(grunt);
var css2jsParser = css2js.cssParser;
var jsParser = require('./').script.init(grunt).jsParser;

grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/**/*.js',
'<%= mochaTest.test.src %>',
'<%= mochaTest.test.src %>'
],
options: {
jshintrc: '.jshintrc',
},
jshintrc: '.jshintrc'
}
},

clean: {
Expand Down Expand Up @@ -93,7 +93,10 @@ module.exports = function(grunt) {
// rely on other modules
rely: {
options: {
paths: ['test/cases/assets']
paths: ['test/cases/assets'],
alias: {
foo: 'arale/class/foo'
}
},
files: [{
expand: true,
Expand Down Expand Up @@ -185,7 +188,7 @@ module.exports = function(grunt) {
'.css': [css2jsParser],
'.js': [jsParser]
},
styleBox: ["a.css"],
styleBox: ['a.css'],
idleading: 'arale/widget/1.0.0/'
},
files: [{
Expand Down Expand Up @@ -248,6 +251,47 @@ module.exports = function(grunt) {
src: '**/*.js',
dest: 'test/expected/directory'
}]
},

'hash': {
options: {
paths: ['test/cases/assets'],
alias: {
'foo': 'arale/class/foo',
'bar': 'family/bar/bar',
'$': '$'
},
idleading: 'family/name/',
hash: true,
debug: false
},
files: [{
expand: true,
cwd: 'test/cases/hash',
src: '*.js',
dest: 'test/expected/hash'
}]
},

project: {
options: {
paths: ['test/cases/project/sea-modules'],
alias: {
'list': 'alice/list/1.0.1/list.css',
'base': 'arale/base/1.1.1/base',
'confirmbox': 'arale/dialog/1.3.1/confirmbox',
'loading': 'alice/loading/1.0.0/loading.css'
},
idleading: 'family/name/',
hash: true,
debug: true
},
files: [{
expand: true,
cwd: 'test/cases/project',
src: ['*.*', '!*.expect'],
dest: 'test/expected/project'
}]
}
},

Expand All @@ -271,6 +315,6 @@ module.exports = function(grunt) {

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint']);
grunt.registerTask('test', ['clean', 'transport', 'mochaTest', 'clean']);
grunt.registerTask('test', ['clean', 'transport', 'mochaTest']);

};
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.

## Release History

**Dec 4th, 2013** `0.4.0`
**Jan 22th, 2015** `0.5.0`

Support hash

**Dec 4th, 2013** `0.4.1`

fix Windows path #58

Expand Down
3 changes: 3 additions & 0 deletions index.js
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');
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-cmd-transport",
"description": "Transport javascript into cmd.",
"version": "0.4.1",
"version": "0.5.0",
"homepage": "https://github.com/spmjs/grunt-cmd-transport",
"author": {
"name": "Hsiaoming Yang",
Expand Down Expand Up @@ -29,9 +29,12 @@
"dependencies": {
"clean-css": "~1.0.1",
"cmd-util": "~0.3.5",
"css": "~1.4.0",
"css2str": "~0.1.1",
"handlebars": "1.0.11",
"uglify-js": "~2.2.5",
"css": "~1.4.0"
"htmlclean": "~2.2.3",
"relative": "~1.2.0",
"uglify-js": "~2.2.5"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
Expand All @@ -43,4 +46,4 @@
"keywords": [
"gruntplugin"
]
}
}
81 changes: 81 additions & 0 deletions tasks/lib/common.js
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, '/');
}
61 changes: 61 additions & 0 deletions tasks/lib/css2js.js
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, '/');
}
60 changes: 60 additions & 0 deletions tasks/lib/handlebars.js
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;
};
}
Loading