Permalink
Browse files

[ashleybrener] use -modularize to compile amd/cmd script

  • Loading branch information...
1 parent ccc299a commit 618446f591b213f8060351ed0181e2e5beb8daba @ariesjia committed Apr 28, 2015
Showing with 113 additions and 86 deletions.
  1. +12 −0 .jshintrc
  2. +14 −11 Gruntfile.js
  3. +19 −1 README.md
  4. +5 −4 package.json
  5. +18 −26 tasks/riot.js
  6. +43 −20 test/expected/concatFile.js
  7. +2 −1 test/riot_test.js
  8. +0 −23 test/tmp/concatFile.js
View
@@ -0,0 +1,12 @@
+{
+"node": true,
+"esnext": false,
+"bitwise": true,
+"curly": true,
+"immed": true,
+"newcap": true,
+"noarg": true,
+"undef": true,
+"unused": "vars",
+"strict": false
+}
View
@@ -9,8 +9,8 @@
'use strict';
module.exports = function (grunt) {
- // load all npm grunt tasks
- require('load-grunt-tasks')(grunt);
+ // load all npm grunt tasks
+ require('load-grunt-tasks')(grunt);
// Project configuration.
grunt.initConfig({
@@ -28,35 +28,38 @@ module.exports = function (grunt) {
// Before generating any new files, remove any previously-created files.
clean: {
- tests: ['tmp']
+ test: ['test/tmp/**']
},
// Configuration to be run (and then tested).
riot: {
- concatFile: {
+ compile: {
options: {
- //concat : true
- },
+ concat: true,
+ modular: [
+ 'riot',
+ {'jquery': '$'},
+ {'lib/my_module': 'MyModule'}
+ ]
+ },
src: 'test/fixtures/*.tag',
- dest: 'test/expected/concatFile.js'
+ dest: 'test/tmp/concatFile.js'
}
},
// Unit tests.
nodeunit: {
tests: ['test/*_test.js']
}
-
});
// Actually load this plugin's task(s).
grunt.loadTasks('tasks');
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
- grunt.registerTask('test', ['clean', 'riot', 'nodeunit']);
+ grunt.registerTask('test', ['clean', 'riot:compile', 'nodeunit']);
// By default, lint and run all tests.
- grunt.registerTask('default', ['jshint', 'test']);
-
+ grunt.registerTask('default', ['jshint', 'test', 'clean']);
};
View
@@ -52,6 +52,24 @@ grunt.initConfig({
})
```
+if you want concat to a single file with AMD and CommonJS support including dependencies , eg:
+
+```js
+grunt.initConfig({
+ riot: {
+ options: {
+ concat : true,
+ modular: [
+ 'riot',
+ {'jquery': '$'},
+ {'lib/my_module': 'MyModule'}
+ ]
+ },
+ src: 'script/*.tag',
+ dest: '.tmp/tag.js'
+ },
+})
+```
### Options
@@ -76,7 +94,7 @@ grunt.initConfig({
* concat: `Boolean`
* you can concat multiple tag file to a single file
* default : `false`
-* modular: `Boolean`
+* modular: `Boolean` or dependency `Array`
* AMD and CommonJS
* default : `false`
View
@@ -1,6 +1,6 @@
{
"name": "grunt-riot",
- "version": "0.0.16",
+ "version": "0.0.17",
"description": "riot custom tag compile grunt plugin.",
"repository": "https://github.com/ariesjia/grunt-riot",
"author": {
@@ -22,14 +22,15 @@
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-nodeunit": "~0.3.0",
"jshint-stylish": "~0.1.5",
- "load-grunt-tasks": "~0.3.0",
- "riot": "~2.0.13"
+ "load-grunt-tasks": "~0.3.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
"concat-stream": "^1.4.7",
- "riot": "^2.0.13"
+ "domhandler": "^2.3.0",
+ "riot": "^2.0.15",
+ "grunt-modularize": "~0.1.1"
}
}
View
@@ -8,15 +8,14 @@
'use strict';
+
module.exports = function (grunt) {
+ require('grunt-modularize/tasks/modularize')(grunt);
var riot = require('riot'),
- path = require('path'),
concat = require('concat-stream');
grunt.registerMultiTask('riot', 'riot custom tag compiler plugin', function () {
- var self = this;
-
var options = this.options({
compact : true ,
expr : true ,
@@ -43,7 +42,7 @@ module.exports = function (grunt) {
};
var compileRiot = function(code, opts){
- return riot.compile(code,opts)
+ return riot.compile(code,opts);
};
var writeFile = function (path, output) {
@@ -62,19 +61,6 @@ module.exports = function (grunt) {
};
});
- var mHeader = '';
- mHeader += '(function(tagger) {\n';
- mHeader += ' if (typeof define === \'function\' && define.amd) {\n';
- mHeader += ' define([\'riot\'], function(riot) { tagger(riot); });\n';
- mHeader += ' } else if (typeof module !== \'undefined\' && typeof module.exports !== \'undefined\') {\n';
- mHeader += ' tagger(require(\'riot\'));\n';
- mHeader += ' } else {\n';
- mHeader += ' tagger(window.riot);\n';
- mHeader += ' }\n';
- mHeader += '})(function(riot) {\n';
-
- var mFooter = '});';
-
validFiles.forEach(function (files, x) {
if(options.concat){
var strings = concat(function(out) {
@@ -85,15 +71,7 @@ module.exports = function (grunt) {
});
files.src.map(function(file, y){
var fileSource = compileRiot( grunt.file.read(file) , getOptions(file,options) );
-
- if (options.modular && x === 0 && y === 0)
- strings.write(mHeader);
-
strings.write(fileSource + '\n');
-
- if (options.modular && !validFiles[x + 1] && y === files.src.length - 1)
- strings.write(mFooter);
-
});
strings.end();
}else{
@@ -105,8 +83,22 @@ module.exports = function (grunt) {
});
}
- });
+ //console.log(grunt)
+ if (options.modular) {
+ var srcDest = {};
+ srcDest[files.dest] = files.dest;
+
+ grunt.config.set('modularize.compile', {
+ options: {
+ deps: options.modular instanceof Array ? options.modular : ['riot']
+ },
+ files: srcDest
+ });
+
+ grunt.task.run(['modularize:compile']);
+ }
+ });
});
};
@@ -1,23 +1,46 @@
-riot.tag('todo', '<h3>{opts.title}</h3><ul><li each="{items}"><label class="{completed: done}"><input type="checkbox" __checked="{done}" onclick="{parent.toggle}"> {title} </label></li></ul><form onsubmit="{add}"><input name="input" onkeyup="{edit}"><button __disabled="{!text}">Add #{items.length + 1}</button></form>', function(opts) {
- this.disabled = true
+(function(root, factory) {
+ if (typeof(define) === 'function' && define.amd) {
+ define(['riot', 'jquery', 'lib/my_module'], function(riot, $, MyModule) {
+ factory(riot, $, MyModule);
+ });
+ }
+ else if (typeof(module) !== 'undefined' && typeof module.exports !== 'undefined') {
+ var riot = require('riot');
+ var $ = require('jquery');
+ var MyModule = require('lib/my_module');
- this.items = opts.items
-
- this.edit = function(e) {
- this.text = e.target.value
- }.bind(this);
-
- this.add = function(e) {
- if (this.text) {
- this.items.push({ title: this.text })
- this.text = this.input.value = ''
- }
- }.bind(this);
-
- this.toggle = function(e) {
- var item = e.item
- item.done = !item.done
- return true
- }.bind(this);
+ factory(riot, $, MyModule);
+ }
+ else {
+ factory(root.riot, root.$, root.MyModule);
+ }
+})(this, function(riot, $, MyModule) {
+ riot.tag('test', '<h3>{this is a test file}</h3>', function(opts) {
+ console.log('test')
+
+ });
+ riot.tag('todo', '<h3>{opts.title}</h3><ul><li each="{items}"><label class="{completed: done}"><input type="checkbox" __checked="{done}" onclick="{parent.toggle}"> {title} </label></li></ul><form onsubmit="{add}"><input name="input" onkeyup="{edit}"><button __disabled="{!text}">Add #{items.length + 1}</button></form>', function(opts) {
+ this.disabled = true
+
+ this.items = opts.items
+
+ this.edit = function(e) {
+ this.text = e.target.value
+ }.bind(this);
+
+ this.add = function(e) {
+ if (this.text) {
+ this.items.push({ title: this.text })
+ this.text = this.input.value = ''
+ }
+ }.bind(this);
+
+ this.toggle = function(e) {
+ var item = e.item
+ item.done = !item.done
+ return true
+ }.bind(this);
+
+ });
});
View
@@ -27,10 +27,11 @@ exports.riot = {
// setup here if necessary
done();
},
- concat_options: function (test) {
+ compile: function (test) {
test.expect(1);
var actual = grunt.file.read('test/tmp/concatFile.js');
var expected = grunt.file.read('test/expected/concatFile.js');
+
test.equal(actual, expected);
test.done();
}
@@ -1,23 +0,0 @@
-riot.tag('todo', '<h3>{opts.title}</h3><ul><li each="{items}"><label class="{completed: done}"><input type="checkbox" __checked="{done}" onclick="{parent.toggle}"> {title} </label></li></ul><form onsubmit="{add}"><input name="input" onkeyup="{edit}"><button __disabled="{!text}">Add #{items.length + 1}</button></form>', function(opts) {
- this.disabled = true
-
- this.items = opts.items
-
- this.edit = function(e) {
- this.text = e.target.value
- }.bind(this);
-
- this.add = function(e) {
- if (this.text) {
- this.items.push({ title: this.text })
- this.text = this.input.value = ''
- }
- }.bind(this);
-
- this.toggle = function(e) {
- var item = e.item
- item.done = !item.done
- return true
- }.bind(this);
-
-});

0 comments on commit 618446f

Please sign in to comment.