From 55afef741222af9e079011eb916944ceb7b1507a Mon Sep 17 00:00:00 2001 From: Jeremy Dowell Date: Thu, 26 Sep 2013 14:17:49 -0500 Subject: [PATCH 1/4] Added angular_module_namespace and corrected singlefile compilation --- README.md | 103 +----------- lib/index.js | 430 ++++++++++++++++++++++++----------------------- lib/index.old | 262 +++++++++++++++++++++++++++++ package.json | 13 +- src/index.coffee | 37 +++- 5 files changed, 529 insertions(+), 316 deletions(-) create mode 100644 lib/index.old diff --git a/README.md b/README.md index 9172120..ddbf9ef 100644 --- a/README.md +++ b/README.md @@ -1,107 +1,12 @@ jade-angularjs-brunch [![Build Status](https://travis-ci.org/GulinSS/jade-angularjs-brunch.png?branch=master)](https://travis-ci.org/GulinSS/jade-angularjs-brunch) ===================== -Compiler Jade templates to AngularJS modules for Brunch.IO with automatic section detection based on location of index.jade's. +DEPRECATED! Need update! +======================== -## Step by step using guide +Automatic compiler Jade templates to AngularJS modules for Brunch.IO -For example you have a directory structure of your project such as: - -``` -app/ - index.jade - application.coffee - welcome/ - page.jade - page.less - controllers.coffee - directives.coffee - otherStuff.coffee - access/ - index.jade - application.coffee - register/ - page.jade - page.less - controllers.coffee - directives.coffee - otherStuff.coffee - login/ - ... - admin/ - index.jade - application.coffee - users/ - ... - records/ - ... - landing/ - index.jade - ... - -``` - -The key note of example above is location of index.jade's. Them will be compile as usual jade files into index.html's. Your public folder will have such structure: - -``` -_public/ - index.html - access/ - index.html - admin/ - index.html - landing/ - index.html - -``` - -And as addition it will group "partials" (files like page.jade in example) of this section into javascript files: - -``` -_public/ - js/ - app.templates.js # it will contains compiled content of - # app/welcome/page.jade and any jades in subdirectories - - app.access.templates.js # it will contains compiled content of - # app/access/register/page.jade and - # app/access/login/page.jade - # and any jades in subdirectories - - app.admin.templates.js # ... - ... -``` - -Any file in example above will contains declaration of Angular.js module with same name: - -``` -app.templates.js -> app.templates -app.access.templates.js -> app.access.templates -... -``` - -Modules must be registered in application.coffee's files such as: - -``` -App = angular.module('app', [ - ... - - 'app.templates' -]) -``` - -After action above you can use your template in your code like this: - -``` - $routeProvider - .when('/welcome', {templateUrl: 'app/welcome/page.jade'}) -``` - -or in directive's templateUrl. - -This magic helps you split your large application on small SPA sections for improving performance and control complexity. - -## Sample of settings (DEPRECATED) +## Sample of settings: ### Add to dependencies section in package.json of your project: diff --git a/lib/index.js b/lib/index.js index a41c7fa..84d5ca2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,251 +1,267 @@ -// Generated by CoffeeScript 1.3.3 -(function() { - var JadeAngularJsCompiler, fileWriter, fs, jade, mkdirp, sysPath, _; +// Generated by CoffeeScript 1.6.3 +var JadeAngularJsCompiler, fileWriter, fs, jade, mkdirp, sysPath, _; - jade = require('jade'); +jade = require('jade'); - sysPath = require('path'); +sysPath = require('path'); - mkdirp = require('mkdirp'); +mkdirp = require('mkdirp'); - fs = require('fs'); +fs = require('fs'); - _ = require('lodash'); +_ = require('lodash'); - fileWriter = function(newFilePath) { - return function(err, content) { - var dirname; +fileWriter = function(newFilePath) { + return function(err, content) { + var dirname; + if (err != null) { + throw err; + } + if (content == null) { + return; + } + dirname = sysPath.dirname(newFilePath); + return mkdirp(dirname, '0775', function(err) { if (err != null) { throw err; } - if (!(content != null)) { - return; - } - dirname = sysPath.dirname(newFilePath); - return mkdirp(dirname, '0775', function(err) { + return fs.writeFile(newFilePath, content, function(err) { if (err != null) { throw err; } - return fs.writeFile(newFilePath, content, function(err) { - if (err != null) { - throw err; - } - }); }); - }; + }); }; +}; - module.exports = JadeAngularJsCompiler = (function() { +module.exports = JadeAngularJsCompiler = (function() { + JadeAngularJsCompiler.prototype.brunchPlugin = true; - JadeAngularJsCompiler.prototype.brunchPlugin = true; + JadeAngularJsCompiler.prototype.type = 'template'; - JadeAngularJsCompiler.prototype.type = 'template'; + JadeAngularJsCompiler.prototype.extension = 'jade'; - JadeAngularJsCompiler.prototype.extension = 'jade'; + function JadeAngularJsCompiler(config) { + var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref16, _ref17, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; + this["public"] = ((_ref = config.paths) != null ? _ref["public"] : void 0) || "_public"; + this.pretty = !!((_ref1 = config.plugins) != null ? (_ref2 = _ref1.jade) != null ? _ref2.pretty : void 0 : void 0); + this.doctype = ((_ref3 = config.plugins) != null ? (_ref4 = _ref3.jade) != null ? _ref4.doctype : void 0 : void 0) || "5"; + this.locals = ((_ref5 = config.plugins) != null ? (_ref6 = _ref5.jade_angular) != null ? _ref6.locals : void 0 : void 0) || {}; + this.staticMask = ((_ref7 = config.plugins) != null ? (_ref8 = _ref7.jade_angular) != null ? _ref8.static_mask : void 0 : void 0) || /index.jade/; + this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe')); + this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0); + this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js"); + this.angularModuleNamespace = ((_ref14 = config.plugins) != null ? (_ref15 = _ref14.jade_angular) != null ? _ref15.angular_module_namespace : void 0 : void 0) || ""; + this.outputDirectory = ((_ref16 = config.plugins) != null ? (_ref17 = _ref16.jade_angular) != null ? _ref17.output_directory : void 0 : void 0) || 'js'; + } - function JadeAngularJsCompiler(config) { - var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; - this["public"] = ((_ref = config.paths) != null ? _ref["public"] : void 0) || "_public"; - this.pretty = !!((_ref1 = config.plugins) != null ? (_ref2 = _ref1.jade) != null ? _ref2.pretty : void 0 : void 0); - this.doctype = ((_ref3 = config.plugins) != null ? (_ref4 = _ref3.jade) != null ? _ref4.doctype : void 0 : void 0) || "5"; - this.locals = ((_ref5 = config.plugins) != null ? (_ref6 = _ref5.jade_angular) != null ? _ref6.locals : void 0 : void 0) || {}; - this.staticMask = ((_ref7 = config.plugins) != null ? (_ref8 = _ref7.jade_angular) != null ? _ref8.static_mask : void 0 : void 0) || /index.jade/; - this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe')); - this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0); - this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js"); + JadeAngularJsCompiler.prototype.compile = function(data, path, callback) { + var content, err, error; + try { + content = jade.compile(data, { + compileDebug: false, + client: false, + filename: path, + doctype: this.doctype, + pretty: this.pretty + }); + return content(this.locals); + } catch (_error) { + err = _error; + return error = err; + } finally { + callback(error, ""); } + }; - JadeAngularJsCompiler.prototype.compile = function(data, path, callback) { - var content, error; - try { - content = jade.compile(data, { - compileDebug: false, - client: false, - filename: path, - doctype: this.doctype, - pretty: this.pretty - }); - return content(this.locals); - } catch (err) { - return error = err; - } finally { - callback(error, ""); - } - }; - - JadeAngularJsCompiler.prototype.preparePairStatic = function(pair) { - pair.path.push(pair.path.pop().slice(0, -this.extension.length) + 'html'); - return pair.path.splice(0, 1, this["public"]); - }; - - JadeAngularJsCompiler.prototype.writeStatic = function(pairs) { - var _this = this; - return _.each(pairs, function(pair) { - var writer; - _this.preparePairStatic(pair); - writer = fileWriter(sysPath.join.apply(_this, pair.path)); - return writer(null, pair.result); - }); - }; + JadeAngularJsCompiler.prototype.preparePairStatic = function(pair) { + pair.path.push(pair.path.pop().slice(0, -this.extension.length) + 'html'); + return pair.path.splice(0, 1, this["public"]); + }; - JadeAngularJsCompiler.prototype.parsePairsIntoAssetsTree = function(pairs) { - var assets, root, - _this = this; - assets = _.map(pairs, function(v) { - return _this.removeFileNameFromPath(v.path); - }); - root = []; - _.each(assets, function(path) { - var node; - node = root; - return _.each(path, function(v) { - var child; - child = _.find(node, function(vv) { - return vv.name === v; - }); - if (child === void 0) { - child = { - name: v, - children: [] - }; - node.push(child); - } - return node = child.children; - }); - }); - return root; - }; + JadeAngularJsCompiler.prototype.writeStatic = function(pairs) { + var _this = this; + console.log('pairs:', pairs); + return _.each(pairs, function(pair) { + var writer; + _this.preparePairStatic(pair); + writer = fileWriter(sysPath.join.apply(_this, pair.path)); + return writer(null, pair.result); + }); + }; - JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair, assetsTree) { - var findedPath, node, path; - path = this.removeFileNameFromPath(pair.path); - if (assetsTree.length === 0) { - pair.module = "" + path[0] + ".templates"; - return; - } - findedPath = []; - node = assetsTree; - _.each(path, function(v) { + JadeAngularJsCompiler.prototype.parsePairsIntoAssetsTree = function(pairs) { + var assets, root, + _this = this; + assets = _.map(pairs, function(v) { + return _this.removeFileNameFromPath(v.path); + }); + root = []; + _.each(assets, function(path) { + var node; + node = root; + return _.each(path, function(v) { var child; child = _.find(node, function(vv) { return vv.name === v; }); if (child === void 0) { - return; + child = { + name: v, + children: [] + }; + node.push(child); } - findedPath.push(child.name); return node = child.children; }); - findedPath.push("templates"); - return pair.module = findedPath.join('.'); - }; + }); + return root; + }; - JadeAngularJsCompiler.prototype.removeFileNameFromPath = function(path) { - return path.slice(0, -1); - }; + JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair, assetsTree) { + var findedPath, node, path; + path = this.removeFileNameFromPath(pair.path); + if (this.angularModuleNamespace) { + pair.module = this.angularModuleNamespace + '.templates'; + return; + } + if (assetsTree.length === 0) { + pair.module = "" + path[0] + ".templates"; + return; + } + findedPath = []; + node = assetsTree; + _.each(path, function(v) { + var child; + child = _.find(node, function(vv) { + return vv.name === v; + }); + if (child === void 0) { + return; + } + findedPath.push(child.name); + return node = child.children; + }); + findedPath.push("templates"); + return pair.module = findedPath.join('.'); + }; - JadeAngularJsCompiler.prototype.generateModuleFileName = function(module) { - return module.filename = sysPath.join.apply(this, [this["public"], 'js', module.name + ".js"]); - }; + JadeAngularJsCompiler.prototype.removeFileNameFromPath = function(path) { + return path.slice(0, -1); + }; - JadeAngularJsCompiler.prototype.writeModules = function(modules) { - var buildModule, content, writer; - buildModule = function(module) { - var addEndOfModule, content, moduleHeader, templateRecord; - moduleHeader = function(name) { - return "angular.module('" + name + "', [])"; - }; - templateRecord = function(result, path) { - var parseStringToJSArray; - parseStringToJSArray = function(str) { - var stringArray; - stringArray = '['; - str.split('\n').map(function(e, i) { - return stringArray += "\n'" + e.replace(/'/g, "\\'") + "',"; - }); - return stringArray += "''" + '].join("\\n")'; - }; - return "\n.run(['$templateCache', function($templateCache) {\n return $templateCache.put('" + path + "', " + (parseStringToJSArray(result)) + ");\n}])"; - }; - addEndOfModule = function() { - return ";\n"; + JadeAngularJsCompiler.prototype.generateModuleFileName = function(module) { + return module.filename = sysPath.join.apply(this, [this["public"], this.outputDirectory, module.name + ".js"]); + }; + + JadeAngularJsCompiler.prototype.writeModules = function(modules) { + var buildModule, content, singleFile, writer; + buildModule = function(module) { + var addEndOfModule, content, moduleHeader, templateRecord; + moduleHeader = function(name) { + return "angular.module('" + name + "', [])"; + }; + templateRecord = function(result, path) { + var parseStringToJSArray; + parseStringToJSArray = function(str) { + var stringArray; + stringArray = '['; + str.split('\n').map(function(e, i) { + return stringArray += "\n'" + e.replace(/'/g, "\\'") + "',"; + }); + return stringArray += "''" + '].join("\\n")'; }; - content = moduleHeader(module.name); - _.each(module.templates, function(template) { - return content += templateRecord(template.result, template.path); - }); - return content += addEndOfModule(); + return "\n.run([ '$templateCache', function($templateCache) {\n return $templateCache.put('" + path + "', " + (parseStringToJSArray(result)) + ");\n}])"; }; - content = ""; - _.each(modules, function(module) { - var moduleContent, writer; - moduleContent = buildModule(module); - if (this.singleFile) { - return content += "\n" + moduleContent; - } else { - writer = fileWriter(module.filename); - return writer(null, moduleContent); - } + addEndOfModule = function() { + return ";\n"; + }; + content = moduleHeader(module.name); + _.each(module.templates, function(template) { + return content += templateRecord(template.result, template.path); }); - if (this.singleFile) { - writer = fileWriter(this.singleFileName); - return writer(null, content); - } + return content += addEndOfModule(); }; - - JadeAngularJsCompiler.prototype.prepareResult = function(compiled) { - var pathes, - _this = this; - pathes = _.find(compiled, function(v) { - return v.path === _this.compileTrigger; - }); - if (pathes === void 0) { - return []; + content = ""; + singleFile = this.singleFile; + _.each(modules, function(module) { + var moduleContent, writer; + console.log('module:', module); + moduleContent = buildModule(module); + console.log('singleFile: ', singleFile); + if (singleFile) { + console.log('adding single file content'); + return content += "\n" + moduleContent; + } else { + console.log('writing all files'); + writer = fileWriter(module.filename); + return writer(null, moduleContent); } - return pathes.sourceFiles.map(function(e, i) { - var content, data; - data = fs.readFileSync(e.path, 'utf8'); - content = jade.compile(data, { - compileDebug: false, - client: false, - filename: e.path, - doctype: _this.doctype, - pretty: _this.pretty - }); - return { - path: e.path.split(sysPath.sep), - result: content(_this.locals) - }; - }); - }; + }); + if (this.singleFile) { + console.log('writing single file'); + writer = fileWriter(this.singleFileName); + return writer(null, content); + } + }; - JadeAngularJsCompiler.prototype.onCompile = function(compiled) { - var assets, assetsTree, preResult, - _this = this; - preResult = this.prepareResult(compiled); - assets = _.filter(preResult, function(v) { - return _this.staticMask.test(v.path); + JadeAngularJsCompiler.prototype.prepareResult = function(compiled) { + var pathes, + _this = this; + pathes = _.find(compiled, function(v) { + return v.path === _this.compileTrigger; + }); + if (pathes === void 0) { + return []; + } + return pathes.sourceFiles.map(function(e, i) { + var content, data, path; + data = fs.readFileSync(e.path, 'utf8'); + content = jade.compile(data, { + compileDebug: false, + client: false, + filename: e.path, + doctype: _this.doctype, + pretty: _this.pretty }); - assetsTree = this.parsePairsIntoAssetsTree(assets); - this.writeStatic(assets); - return this.writeModules(_.chain(preResult).difference(assets).each(function(v) { - return _this.attachModuleNameToTemplate(v, assetsTree); - }).each(function(v) { - return v.path = v.path.join('/'); - }).groupBy(function(v) { - return v.module; - }).map(function(v, k) { - return { - name: k, - templates: v - }; - }).each(function(v) { - return _this.generateModuleFileName(v); - }).value()); - }; + path = e.path.split(sysPath.sep); + if (_this.angularModuleNamespace) { + path[0] = _this.angularModuleNamespace; + } + return { + path: path, + result: content(_this.locals) + }; + }); + }; - return JadeAngularJsCompiler; + JadeAngularJsCompiler.prototype.onCompile = function(compiled) { + var assets, assetsTree, preResult, + _this = this; + preResult = this.prepareResult(compiled); + assets = _.filter(preResult, function(v) { + return _this.staticMask.test(v.path); + }); + assetsTree = this.parsePairsIntoAssetsTree(assets); + this.writeStatic(assets); + return this.writeModules(_.chain(preResult).difference(assets).each(function(v) { + return _this.attachModuleNameToTemplate(v, assetsTree); + }).each(function(v) { + v.path.push(v.path.pop().slice(0, -_this.extension.length) + 'html'); + v.path = v.path.join('/'); + return console.log('v.path:', v.path); + }).groupBy(function(v) { + return v.module; + }).map(function(v, k) { + return { + name: k, + templates: v + }; + }).each(function(v) { + return _this.generateModuleFileName(v); + }).value()); + }; - })(); + return JadeAngularJsCompiler; -}).call(this); +})(); diff --git a/lib/index.old b/lib/index.old new file mode 100644 index 0000000..5b2facd --- /dev/null +++ b/lib/index.old @@ -0,0 +1,262 @@ +// Generated by CoffeeScript 1.6.3 +var JadeAngularJsCompiler, fileWriter, fs, jade, mkdirp, sysPath, _; + +jade = require('jade'); + +sysPath = require('path'); + +mkdirp = require('mkdirp'); + +fs = require('fs'); + +_ = require('lodash'); + +fileWriter = function(newFilePath) { + return function(err, content) { + var dirname; + if (err != null) { + throw err; + } + if (content == null) { + return; + } + dirname = sysPath.dirname(newFilePath); + return mkdirp(dirname, '0775', function(err) { + if (err != null) { + throw err; + } + return fs.writeFile(newFilePath, content, function(err) { + if (err != null) { + throw err; + } + }); + }); + }; +}; + +module.exports = JadeAngularJsCompiler = (function() { + JadeAngularJsCompiler.prototype.brunchPlugin = true; + + JadeAngularJsCompiler.prototype.type = 'template'; + + JadeAngularJsCompiler.prototype.extension = 'jade'; + + function JadeAngularJsCompiler(config) { + var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; + this["public"] = ((_ref = config.paths) != null ? _ref["public"] : void 0) || "_public"; + this.pretty = !!((_ref1 = config.plugins) != null ? (_ref2 = _ref1.jade) != null ? _ref2.pretty : void 0 : void 0); + this.doctype = ((_ref3 = config.plugins) != null ? (_ref4 = _ref3.jade) != null ? _ref4.doctype : void 0 : void 0) || "5"; + this.locals = ((_ref5 = config.plugins) != null ? (_ref6 = _ref5.jade_angular) != null ? _ref6.locals : void 0 : void 0) || {}; + this.staticMask = ((_ref7 = config.plugins) != null ? (_ref8 = _ref7.jade_angular) != null ? _ref8.static_mask : void 0 : void 0) || /index.jade/; + this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe')); + this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0); + this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js"); + this.angularModuleNamespace = ((_ref14 = config.plugins) != null ? (_ref15 = _ref14.jade_angular) != null ? _ref15.angular_module_namespace : void 0 : void 0) || null; + } + + JadeAngularJsCompiler.prototype.compile = function(data, path, callback) { + var content, err, error; + try { + content = jade.compile(data, { + compileDebug: false, + client: false, + filename: path, + doctype: this.doctype, + pretty: this.pretty + }); + return content(this.locals); + } catch (_error) { + err = _error; + return error = err; + } finally { + callback(error, ""); + } + }; + + JadeAngularJsCompiler.prototype.preparePairStatic = function(pair) { + pair.path.push(pair.path.pop().slice(0, -this.extension.length) + 'html'); + return pair.path.splice(0, 1, this["public"]); + }; + + JadeAngularJsCompiler.prototype.writeStatic = function(pairs) { + var _this = this; + return _.each(pairs, function(pair) { + var writer; + _this.preparePairStatic(pair); + writer = fileWriter(sysPath.join.apply(_this, pair.path)); + return writer(null, pair.result); + }); + }; + + JadeAngularJsCompiler.prototype.parsePairsIntoAssetsTree = function(pairs) { + var assets, root, + _this = this; + assets = _.map(pairs, function(v) { + return _this.removeFileNameFromPath(v.path); + }); + root = []; + _.each(assets, function(path) { + var node; + node = root; + return _.each(path, function(v) { + var child; + child = _.find(node, function(vv) { + return vv.name === v; + }); + if (child === void 0) { + child = { + name: v, + children: [] + }; + node.push(child); + } + return node = child.children; + }); + }); + return root; + }; + + JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair, assetsTree) { + var findedPath, node, path; + path = this.removeFileNameFromPath(pair.path); + console.log('path:' , path) + if (this.angularModuleNamespace) { + pair.module = this.angularModuleNamespace + '.templates'; + return; + } + + if (assetsTree.length === 0) { + pair.module = "" + path[0] + ".templates"; + console.log('returning: ' , pair.module) + return; + } + findedPath = []; + node = assetsTree; + _.each(path, function(v) { + var child; + child = _.find(node, function(vv) { + return vv.name === v; + }); + if (child === void 0) { + return; + } + findedPath.push(child.name); + return node = child.children; + }); + findedPath.push("templates"); + return pair.module = findedPath.join('.'); + }; + + JadeAngularJsCompiler.prototype.removeFileNameFromPath = function(path) { + return path.slice(0, -1); + }; + + JadeAngularJsCompiler.prototype.generateModuleFileName = function(module) { + return module.filename = sysPath.join.apply(this, [this["public"], 'js', module.name + ".js"]); + }; + + JadeAngularJsCompiler.prototype.writeModules = function(modules) { + var buildModule, content, writer; + buildModule = function(module) { + var addEndOfModule, content, moduleHeader, templateRecord; + moduleHeader = function(name) { + return "angular.module('" + name + "', [])"; + }; + templateRecord = function(result, path) { + var parseStringToJSArray; + parseStringToJSArray = function(str) { + var stringArray; + stringArray = '['; + str.split('\n').map(function(e, i) { + return stringArray += "\n'" + e.replace(/'/g, "\\'") + "',"; + }); + return stringArray += "''" + '].join("\\n")'; + }; + return "\n.run(['$templateCache', function($templateCache) {\n return $templateCache.put('" + path + "', " + (parseStringToJSArray(result)) + ");\n}])"; + }; + addEndOfModule = function() { + return ";\n"; + }; + content = moduleHeader(module.name); + _.each(module.templates, function(template) { + return content += templateRecord(template.result, template.path); + }); + return content += addEndOfModule(); + }; + content = ""; + _.each(modules, function(module) { + var moduleContent, writer; + moduleContent = buildModule(module); + if (this.singleFile) { + return content += "\n" + moduleContent; + } else { + writer = fileWriter(module.filename); + return writer(null, moduleContent); + } + }); + if (this.singleFile) { + writer = fileWriter(this.singleFileName); + return writer(null, content); + } + }; + + JadeAngularJsCompiler.prototype.prepareResult = function(compiled) { + var pathes, + _this = this; + pathes = _.find(compiled, function(v) { + return v.path === _this.compileTrigger; + }); + if (pathes === void 0) { + return []; + } + return pathes.sourceFiles.map(function(e, i) { + var content, data; + data = fs.readFileSync(e.path, 'utf8'); + content = jade.compile(data, { + compileDebug: false, + client: false, + filename: e.path, + doctype: _this.doctype, + pretty: _this.pretty + }); + + var path; + path = e.path.split(sysPath.sep) + if (_this.angularModuleNamespace) { + path[0] = _this.angularModuleNamespace; + } + return { + path: path, + result: content(_this.locals) + }; + }); + }; + + JadeAngularJsCompiler.prototype.onCompile = function(compiled) { + var assets, assetsTree, preResult, + _this = this; + preResult = this.prepareResult(compiled); + assets = _.filter(preResult, function(v) { + return _this.staticMask.test(v.path); + }); + assetsTree = this.parsePairsIntoAssetsTree(assets); + this.writeStatic(assets); + return this.writeModules(_.chain(preResult).difference(assets).each(function(v) { + return _this.attachModuleNameToTemplate(v, assetsTree); + }).each(function(v) { + return v.path = v.path.join('/'); + }).groupBy(function(v) { + return v.module; + }).map(function(v, k) { + return { + name: k, + templates: v + }; + }).each(function(v) { + return _this.generateModuleFileName(v); + }).value()); + }; + + return JadeAngularJsCompiler; + +})(); diff --git a/package.json b/package.json index 000278e..e25404c 100644 --- a/package.json +++ b/package.json @@ -30,5 +30,16 @@ "chai": "*", "sinon": "~1.7.3", "sinon-chai": "~2.4.0" - } + }, + "readme": "jade-angularjs-brunch [![Build Status](https://travis-ci.org/GulinSS/jade-angularjs-brunch.png?branch=master)](https://travis-ci.org/GulinSS/jade-angularjs-brunch)\n=====================\n\nDEPRECATED! Need update!\n========================\n\nAutomatic compiler Jade templates to AngularJS modules for Brunch.IO\n\n## Sample of settings:\n\n### Add to dependencies section in package.json of your project:\n\n`` \"jade-angularjs-brunch\" : \">= 0.0.1 < 1.5\" `` \n\n### Add to paths section in config.coffee:\n\n```coffee\njadeCompileTrigger: '.compile-jade' # Defaults to 'js/dontUseMe'.\n```\n\n### Add to templates section in config.coffee:\n\n```coffee\njoinTo: \n '.compile-jade': /^app/ # Hack for auto-compiling Jade templates.\n```\n\n### Add to plugin section in config.coffee:\n\n```coffee\nplugins:\n jade:\n pretty: yes # Adds pretty-indentation whitespaces to output (false by default).\n doctype: \"xml\" # Specify doctype (\"5\" by default).\n jade_angular:\n modules_folder: 'templates'\n locals: {}\n```\n\n* modules_folder: folder with your template\n* locals: context for jade compiler\n\n### Now you can get angular.js modules:\n\n_public/js/login.template.js:\n\n```js\nangular.module('login.templates', [])\n.run(['$templateCache', function($templateCache) {\n return $templateCache.put('/login/modal.page.html', [\n'This is content of your jade-file',''].join(\"\\n\"));\n}])\n```\n\n# Single-File Mode\n\nIf you want a single file instead of a file per module, you can use the `single_file` option in `jade_angular`.\n\n```coffee\nplugins:\n jade_angular:\n single_file: true\n # if you want to change the file name (defaults to js/templates.js and is in your public directory)\n single_file_name: 'js/angular_templates.js'\n```\n", + "readmeFilename": "README.md", + "bugs": { + "url": "https://github.com/GulinSS/jade-angularjs-brunch/issues" + }, + "_id": "jade-angularjs-brunch@1.1.1", + "_from": "jade-angularjs-brunch@1.1.1", + "dist": { + "shasum": "93d3372c6f8b4a4bf1d0a4cf87dc18bc4a187680" + }, + "_resolved": "https://registry.npmjs.org/jade-angularjs-brunch/-/jade-angularjs-brunch-1.1.1.tgz" } diff --git a/src/index.coffee b/src/index.coffee index 9f4a814..df2a27e 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -27,6 +27,8 @@ module.exports = class JadeAngularJsCompiler @compileTrigger = sysPath.normalize @public + sysPath.sep + (config.paths?.jadeCompileTrigger or 'js/dontUseMe') @singleFile = !!config?.plugins?.jade_angular?.single_file @singleFileName = sysPath.join @public, (config?.plugins?.jade_angular?.single_file_name or "js/angular_templates.js") + @angularModuleNamespace = config.plugins?.jade_angular?.angular_module_namespace or "" + @outputDirectory = config.plugins?.jade_angular?.output_directory or 'js' # Do nothing, just check possibility of Jade compilation compile: (data, path, callback) -> @@ -50,6 +52,7 @@ module.exports = class JadeAngularJsCompiler pair.path.splice 0, 1, @public writeStatic: (pairs) -> + console.log 'pairs:', pairs _.each pairs, (pair) => @preparePairStatic pair writer = fileWriter sysPath.join.apply(this, pair.path) @@ -78,8 +81,12 @@ module.exports = class JadeAngularJsCompiler attachModuleNameToTemplate: (pair, assetsTree) -> path = @removeFileNameFromPath pair.path + if @angularModuleNamespace + pair.module = @angularModuleNamespace + '.templates'; + return; + if assetsTree.length is 0 - pair.module ="#{path[0]}.templates" + pair.module = "#{path[0]}.templates" return findedPath = [] @@ -98,10 +105,9 @@ module.exports = class JadeAngularJsCompiler removeFileNameFromPath: (path) -> path[0..-2] generateModuleFileName: (module) -> - module.filename = sysPath.join.apply(this, [@public, 'js', module.name+".js"]) + module.filename = sysPath.join.apply(this, [@public, @outputDirectory, module.name+".js"]) writeModules: (modules) -> - buildModule = (module) -> moduleHeader = (name) -> """ @@ -116,7 +122,7 @@ module.exports = class JadeAngularJsCompiler stringArray += "''" + '].join("\\n")' """ - \n.run(['$templateCache', function($templateCache) { + \n.run([ '$templateCache', function($templateCache) { return $templateCache.put('#{path}', #{parseStringToJSArray(result)}); }]) """ @@ -131,20 +137,24 @@ module.exports = class JadeAngularJsCompiler content += addEndOfModule() content = "" - + singleFile = @singleFile _.each modules, (module) -> + console.log 'module:', module moduleContent = buildModule module - if @singleFile + console.log 'singleFile: ', singleFile + if singleFile + console.log 'adding single file content' content += "\n#{moduleContent}" else + console.log 'writing all files' writer = fileWriter module.filename writer null, moduleContent if @singleFile + console.log 'writing single file' writer = fileWriter @singleFileName writer null, content - prepareResult: (compiled) -> pathes = _.find compiled, (v) => v.path is @compileTrigger @@ -159,7 +169,12 @@ module.exports = class JadeAngularJsCompiler doctype: @doctype pretty: @pretty - path: e.path.split sysPath.sep + path = e.path.split sysPath.sep + + if @angularModuleNamespace + path[0] = @angularModuleNamespace; + + path: path result: content @locals onCompile: (compiled) -> @@ -173,7 +188,11 @@ module.exports = class JadeAngularJsCompiler @writeModules _.chain(preResult) .difference(assets) .each((v) => @attachModuleNameToTemplate v, assetsTree) - .each((v) -> v.path = v.path.join('/')) # concat items to virtual url + .each((v) => + v.path.push(v.path.pop()[...-@extension.length] + 'html') + v.path = v.path.join('/') + console.log 'v.path:', v.path + ) # concat items to virtual url .groupBy((v) -> v.module) .map((v, k) -> name: k, templates: v) .each((v) => @generateModuleFileName v) From 60a97b090554062aa6cb14b8c3d04e04447a6e09 Mon Sep 17 00:00:00 2001 From: Jeremy Dowell Date: Thu, 26 Sep 2013 14:32:33 -0500 Subject: [PATCH 2/4] Updated the readme --- README.md | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ddbf9ef..5baec30 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,38 @@ angular.module('login.templates', []) 'This is content of your jade-file',''].join("\n")); }]) ``` +# Specific Output Directory + +You can now specify the output directory of the compiled files using the `output_directory` property of the config +```coffee +plugins: +plugins: + jade_angular: + output_directory: 'foo' +``` + + +# Angular Module Namespacing + +You can add an angular module namespace to all templates using the `angular_module_namespace` property. + +```coffee +jade_angular: + single_file: false + angular_module_namespace: 'myLibrary' +``` +This will create template files with namespaced modules instead of using directory structure. + +```javascript +angular.module('myLibrary.templates', []) +.run([ '$templateCache', function($templateCache) { + return $templateCache.put('myLibrary/templates/hello.html', [ +'', +'
', +'

Hello, World!

', +'
',''].join("\n")); +}]); +``` # Single-File Mode @@ -58,8 +90,8 @@ If you want a single file instead of a file per module, you can use the `single_ ```coffee plugins: - jade_angular: - single_file: true + jade_angular: single_file: true # if you want to change the file name (defaults to js/templates.js and is in your public directory) single_file_name: 'js/angular_templates.js' + ``` From 19e7ee3d04756d8c0dcad909ef08002e0389366c Mon Sep 17 00:00:00 2001 From: Jeremy Dowell Date: Thu, 26 Sep 2013 14:36:48 -0500 Subject: [PATCH 3/4] Removed debug statements --- src/index.coffee | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/index.coffee b/src/index.coffee index df2a27e..263e540 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -52,7 +52,6 @@ module.exports = class JadeAngularJsCompiler pair.path.splice 0, 1, @public writeStatic: (pairs) -> - console.log 'pairs:', pairs _.each pairs, (pair) => @preparePairStatic pair writer = fileWriter sysPath.join.apply(this, pair.path) @@ -139,20 +138,17 @@ module.exports = class JadeAngularJsCompiler content = "" singleFile = @singleFile _.each modules, (module) -> - console.log 'module:', module + moduleContent = buildModule module - - console.log 'singleFile: ', singleFile + if singleFile - console.log 'adding single file content' content += "\n#{moduleContent}" + else - console.log 'writing all files' writer = fileWriter module.filename writer null, moduleContent if @singleFile - console.log 'writing single file' writer = fileWriter @singleFileName writer null, content prepareResult: (compiled) -> @@ -191,7 +187,6 @@ module.exports = class JadeAngularJsCompiler .each((v) => v.path.push(v.path.pop()[...-@extension.length] + 'html') v.path = v.path.join('/') - console.log 'v.path:', v.path ) # concat items to virtual url .groupBy((v) -> v.module) .map((v, k) -> name: k, templates: v) From 5bd2163ffe1d58015c306586edbf0fc47e93af9b Mon Sep 17 00:00:00 2001 From: Jeremy Dowell Date: Thu, 26 Sep 2013 15:45:58 -0500 Subject: [PATCH 4/4] Changed angular_module_namespace to be angular_module object with namespace and predefined options. Predefiniton determines whether to create a new angular module, or to append to another --- README.md | 30 +++++- lib/index.js | 28 +++-- lib/index.old | 262 ----------------------------------------------- src/index.coffee | 22 ++-- 4 files changed, 54 insertions(+), 288 deletions(-) delete mode 100644 lib/index.old diff --git a/README.md b/README.md index 5baec30..a6de516 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,16 @@ plugins: ``` -# Angular Module Namespacing +# Angular Module Config -You can add an angular module namespace to all templates using the `angular_module_namespace` property. +You can add an angular module namespace to all templates using the `angular_module` property. ```coffee jade_angular: single_file: false - angular_module_namespace: 'myLibrary' + angular_module + namespace: 'myLibrary' + predefined: false ``` This will create template files with namespaced modules instead of using directory structure. @@ -84,6 +86,28 @@ angular.module('myLibrary.templates', []) }]); ``` +You can also specify whether or not the angular module has been predfined using the `predefined` property + +```coffee +jade_angular: + single_file: false + angular_module + namespace: 'myLibrary' + predefined: true +``` + +```javascript +angular.module('myLibrary.templates') +.run([ '$templateCache', function($templateCache) { + return $templateCache.put('myLibrary/templates/hello.html', [ +'', +'
', +'

Hello, World!

', +'
',''].join("\n")); +}]); +``` + + # Single-File Mode If you want a single file instead of a file per module, you can use the `single_file` option in `jade_angular`. diff --git a/lib/index.js b/lib/index.js index 84d5ca2..630163f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,7 +51,7 @@ module.exports = JadeAngularJsCompiler = (function() { this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe')); this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0); this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js"); - this.angularModuleNamespace = ((_ref14 = config.plugins) != null ? (_ref15 = _ref14.jade_angular) != null ? _ref15.angular_module_namespace : void 0 : void 0) || ""; + this.angularModule = ((_ref14 = config.plugins) != null ? (_ref15 = _ref14.jade_angular) != null ? _ref15.angular_module : void 0 : void 0) || {}; this.outputDirectory = ((_ref16 = config.plugins) != null ? (_ref17 = _ref16.jade_angular) != null ? _ref17.output_directory : void 0 : void 0) || 'js'; } @@ -81,7 +81,6 @@ module.exports = JadeAngularJsCompiler = (function() { JadeAngularJsCompiler.prototype.writeStatic = function(pairs) { var _this = this; - console.log('pairs:', pairs); return _.each(pairs, function(pair) { var writer; _this.preparePairStatic(pair); @@ -121,8 +120,8 @@ module.exports = JadeAngularJsCompiler = (function() { JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair, assetsTree) { var findedPath, node, path; path = this.removeFileNameFromPath(pair.path); - if (this.angularModuleNamespace) { - pair.module = this.angularModuleNamespace + '.templates'; + if (this.angularModule.namespace) { + pair.module = this.angularModule.namespace + '.templates'; return; } if (assetsTree.length === 0) { @@ -155,11 +154,16 @@ module.exports = JadeAngularJsCompiler = (function() { }; JadeAngularJsCompiler.prototype.writeModules = function(modules) { - var buildModule, content, singleFile, writer; + var buildModule, content, predefined, singleFile, writer; + predefined = this.angularModule.predefined; buildModule = function(module) { var addEndOfModule, content, moduleHeader, templateRecord; moduleHeader = function(name) { - return "angular.module('" + name + "', [])"; + if (!predefined) { + return "angular.module('" + name + "', [])"; + } else { + return "angular.module('" + name + "')"; + } }; templateRecord = function(result, path) { var parseStringToJSArray; @@ -186,20 +190,15 @@ module.exports = JadeAngularJsCompiler = (function() { singleFile = this.singleFile; _.each(modules, function(module) { var moduleContent, writer; - console.log('module:', module); moduleContent = buildModule(module); - console.log('singleFile: ', singleFile); if (singleFile) { - console.log('adding single file content'); return content += "\n" + moduleContent; } else { - console.log('writing all files'); writer = fileWriter(module.filename); return writer(null, moduleContent); } }); if (this.singleFile) { - console.log('writing single file'); writer = fileWriter(this.singleFileName); return writer(null, content); } @@ -225,8 +224,8 @@ module.exports = JadeAngularJsCompiler = (function() { pretty: _this.pretty }); path = e.path.split(sysPath.sep); - if (_this.angularModuleNamespace) { - path[0] = _this.angularModuleNamespace; + if (_this.angularModule.namespace) { + path[0] = _this.angularModule.namespace; } return { path: path, @@ -248,8 +247,7 @@ module.exports = JadeAngularJsCompiler = (function() { return _this.attachModuleNameToTemplate(v, assetsTree); }).each(function(v) { v.path.push(v.path.pop().slice(0, -_this.extension.length) + 'html'); - v.path = v.path.join('/'); - return console.log('v.path:', v.path); + return v.path = v.path.join('/'); }).groupBy(function(v) { return v.module; }).map(function(v, k) { diff --git a/lib/index.old b/lib/index.old deleted file mode 100644 index 5b2facd..0000000 --- a/lib/index.old +++ /dev/null @@ -1,262 +0,0 @@ -// Generated by CoffeeScript 1.6.3 -var JadeAngularJsCompiler, fileWriter, fs, jade, mkdirp, sysPath, _; - -jade = require('jade'); - -sysPath = require('path'); - -mkdirp = require('mkdirp'); - -fs = require('fs'); - -_ = require('lodash'); - -fileWriter = function(newFilePath) { - return function(err, content) { - var dirname; - if (err != null) { - throw err; - } - if (content == null) { - return; - } - dirname = sysPath.dirname(newFilePath); - return mkdirp(dirname, '0775', function(err) { - if (err != null) { - throw err; - } - return fs.writeFile(newFilePath, content, function(err) { - if (err != null) { - throw err; - } - }); - }); - }; -}; - -module.exports = JadeAngularJsCompiler = (function() { - JadeAngularJsCompiler.prototype.brunchPlugin = true; - - JadeAngularJsCompiler.prototype.type = 'template'; - - JadeAngularJsCompiler.prototype.extension = 'jade'; - - function JadeAngularJsCompiler(config) { - var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; - this["public"] = ((_ref = config.paths) != null ? _ref["public"] : void 0) || "_public"; - this.pretty = !!((_ref1 = config.plugins) != null ? (_ref2 = _ref1.jade) != null ? _ref2.pretty : void 0 : void 0); - this.doctype = ((_ref3 = config.plugins) != null ? (_ref4 = _ref3.jade) != null ? _ref4.doctype : void 0 : void 0) || "5"; - this.locals = ((_ref5 = config.plugins) != null ? (_ref6 = _ref5.jade_angular) != null ? _ref6.locals : void 0 : void 0) || {}; - this.staticMask = ((_ref7 = config.plugins) != null ? (_ref8 = _ref7.jade_angular) != null ? _ref8.static_mask : void 0 : void 0) || /index.jade/; - this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe')); - this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0); - this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js"); - this.angularModuleNamespace = ((_ref14 = config.plugins) != null ? (_ref15 = _ref14.jade_angular) != null ? _ref15.angular_module_namespace : void 0 : void 0) || null; - } - - JadeAngularJsCompiler.prototype.compile = function(data, path, callback) { - var content, err, error; - try { - content = jade.compile(data, { - compileDebug: false, - client: false, - filename: path, - doctype: this.doctype, - pretty: this.pretty - }); - return content(this.locals); - } catch (_error) { - err = _error; - return error = err; - } finally { - callback(error, ""); - } - }; - - JadeAngularJsCompiler.prototype.preparePairStatic = function(pair) { - pair.path.push(pair.path.pop().slice(0, -this.extension.length) + 'html'); - return pair.path.splice(0, 1, this["public"]); - }; - - JadeAngularJsCompiler.prototype.writeStatic = function(pairs) { - var _this = this; - return _.each(pairs, function(pair) { - var writer; - _this.preparePairStatic(pair); - writer = fileWriter(sysPath.join.apply(_this, pair.path)); - return writer(null, pair.result); - }); - }; - - JadeAngularJsCompiler.prototype.parsePairsIntoAssetsTree = function(pairs) { - var assets, root, - _this = this; - assets = _.map(pairs, function(v) { - return _this.removeFileNameFromPath(v.path); - }); - root = []; - _.each(assets, function(path) { - var node; - node = root; - return _.each(path, function(v) { - var child; - child = _.find(node, function(vv) { - return vv.name === v; - }); - if (child === void 0) { - child = { - name: v, - children: [] - }; - node.push(child); - } - return node = child.children; - }); - }); - return root; - }; - - JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair, assetsTree) { - var findedPath, node, path; - path = this.removeFileNameFromPath(pair.path); - console.log('path:' , path) - if (this.angularModuleNamespace) { - pair.module = this.angularModuleNamespace + '.templates'; - return; - } - - if (assetsTree.length === 0) { - pair.module = "" + path[0] + ".templates"; - console.log('returning: ' , pair.module) - return; - } - findedPath = []; - node = assetsTree; - _.each(path, function(v) { - var child; - child = _.find(node, function(vv) { - return vv.name === v; - }); - if (child === void 0) { - return; - } - findedPath.push(child.name); - return node = child.children; - }); - findedPath.push("templates"); - return pair.module = findedPath.join('.'); - }; - - JadeAngularJsCompiler.prototype.removeFileNameFromPath = function(path) { - return path.slice(0, -1); - }; - - JadeAngularJsCompiler.prototype.generateModuleFileName = function(module) { - return module.filename = sysPath.join.apply(this, [this["public"], 'js', module.name + ".js"]); - }; - - JadeAngularJsCompiler.prototype.writeModules = function(modules) { - var buildModule, content, writer; - buildModule = function(module) { - var addEndOfModule, content, moduleHeader, templateRecord; - moduleHeader = function(name) { - return "angular.module('" + name + "', [])"; - }; - templateRecord = function(result, path) { - var parseStringToJSArray; - parseStringToJSArray = function(str) { - var stringArray; - stringArray = '['; - str.split('\n').map(function(e, i) { - return stringArray += "\n'" + e.replace(/'/g, "\\'") + "',"; - }); - return stringArray += "''" + '].join("\\n")'; - }; - return "\n.run(['$templateCache', function($templateCache) {\n return $templateCache.put('" + path + "', " + (parseStringToJSArray(result)) + ");\n}])"; - }; - addEndOfModule = function() { - return ";\n"; - }; - content = moduleHeader(module.name); - _.each(module.templates, function(template) { - return content += templateRecord(template.result, template.path); - }); - return content += addEndOfModule(); - }; - content = ""; - _.each(modules, function(module) { - var moduleContent, writer; - moduleContent = buildModule(module); - if (this.singleFile) { - return content += "\n" + moduleContent; - } else { - writer = fileWriter(module.filename); - return writer(null, moduleContent); - } - }); - if (this.singleFile) { - writer = fileWriter(this.singleFileName); - return writer(null, content); - } - }; - - JadeAngularJsCompiler.prototype.prepareResult = function(compiled) { - var pathes, - _this = this; - pathes = _.find(compiled, function(v) { - return v.path === _this.compileTrigger; - }); - if (pathes === void 0) { - return []; - } - return pathes.sourceFiles.map(function(e, i) { - var content, data; - data = fs.readFileSync(e.path, 'utf8'); - content = jade.compile(data, { - compileDebug: false, - client: false, - filename: e.path, - doctype: _this.doctype, - pretty: _this.pretty - }); - - var path; - path = e.path.split(sysPath.sep) - if (_this.angularModuleNamespace) { - path[0] = _this.angularModuleNamespace; - } - return { - path: path, - result: content(_this.locals) - }; - }); - }; - - JadeAngularJsCompiler.prototype.onCompile = function(compiled) { - var assets, assetsTree, preResult, - _this = this; - preResult = this.prepareResult(compiled); - assets = _.filter(preResult, function(v) { - return _this.staticMask.test(v.path); - }); - assetsTree = this.parsePairsIntoAssetsTree(assets); - this.writeStatic(assets); - return this.writeModules(_.chain(preResult).difference(assets).each(function(v) { - return _this.attachModuleNameToTemplate(v, assetsTree); - }).each(function(v) { - return v.path = v.path.join('/'); - }).groupBy(function(v) { - return v.module; - }).map(function(v, k) { - return { - name: k, - templates: v - }; - }).each(function(v) { - return _this.generateModuleFileName(v); - }).value()); - }; - - return JadeAngularJsCompiler; - -})(); diff --git a/src/index.coffee b/src/index.coffee index 263e540..1a940ed 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -27,7 +27,7 @@ module.exports = class JadeAngularJsCompiler @compileTrigger = sysPath.normalize @public + sysPath.sep + (config.paths?.jadeCompileTrigger or 'js/dontUseMe') @singleFile = !!config?.plugins?.jade_angular?.single_file @singleFileName = sysPath.join @public, (config?.plugins?.jade_angular?.single_file_name or "js/angular_templates.js") - @angularModuleNamespace = config.plugins?.jade_angular?.angular_module_namespace or "" + @angularModule = config.plugins?.jade_angular?.angular_module or {} @outputDirectory = config.plugins?.jade_angular?.output_directory or 'js' # Do nothing, just check possibility of Jade compilation @@ -80,8 +80,8 @@ module.exports = class JadeAngularJsCompiler attachModuleNameToTemplate: (pair, assetsTree) -> path = @removeFileNameFromPath pair.path - if @angularModuleNamespace - pair.module = @angularModuleNamespace + '.templates'; + if @angularModule.namespace + pair.module = @angularModule.namespace + '.templates'; return; if assetsTree.length is 0 @@ -107,11 +107,17 @@ module.exports = class JadeAngularJsCompiler module.filename = sysPath.join.apply(this, [@public, @outputDirectory, module.name+".js"]) writeModules: (modules) -> + predefined = @angularModule.predefined buildModule = (module) -> moduleHeader = (name) -> - """ - angular.module('#{name}', []) - """ + if !predefined + """ + angular.module('#{name}', []) + """ + else + """ + angular.module('#{name}') + """ templateRecord = (result, path) -> parseStringToJSArray = (str) -> @@ -167,8 +173,8 @@ module.exports = class JadeAngularJsCompiler path = e.path.split sysPath.sep - if @angularModuleNamespace - path[0] = @angularModuleNamespace; + if @angularModule.namespace + path[0] = @angularModule.namespace; path: path result: content @locals