grunt-jasper
Advanced tools
Comparing version 0.1.13 to 0.1.14
{ | ||
"name": "grunt-jasper", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "Grunt task to build and package jasper application", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/jasperjs/grunt-jasper", |
@@ -7,121 +7,140 @@ 'use strict'; | ||
this.getPath = function (filepath) { | ||
return filepath.substring(0, filepath.lastIndexOf('/')); | ||
}; | ||
this.getPath = function (filepath) { | ||
return filepath.substring(0, filepath.lastIndexOf('/')); | ||
}; | ||
this.getParentFolderName = function (filepath) { | ||
var path = this.getPath(filepath); | ||
return path.match(/([^\/]*)\/*$/)[1]; | ||
}; | ||
this.getParentFolderName = function (filepath) { | ||
var path = this.getPath(filepath); | ||
return path.match(/([^\/]*)\/*$/)[1]; | ||
}; | ||
this.camelCase = function (name) { | ||
var regex = /[A-Z]/g; | ||
return name.replace(regex, function (letter, pos) { | ||
return pos ? letter : letter.toLowerCase(); | ||
}); | ||
}; | ||
this.camelCase = function (name) { | ||
var regex = /[A-Z]/g; | ||
return name.replace(regex, function (letter, pos) { | ||
return pos ? letter : letter.toLowerCase(); | ||
}); | ||
}; | ||
this.camelCaseTagName = function (tagName) { | ||
if (tagName.indexOf('-') < 0) { | ||
return this.camelCase(tagName); | ||
} | ||
this.camelCaseTagName = function (tagName) { | ||
if (tagName.indexOf('-') < 0) { | ||
return this.camelCase(tagName); | ||
} | ||
return tagName.replace(/\-(\w)/g, function (match, letter) { | ||
return letter.toUpperCase(); | ||
}); | ||
}; | ||
return tagName.replace(/\-(\w)/g, function (match, letter) { | ||
return letter.toUpperCase(); | ||
}); | ||
}; | ||
this.shakeCase = function (name) { | ||
var SNAKE_CASE_REGEXP = /[A-Z]/g; | ||
var separator = '-'; | ||
return name.replace(SNAKE_CASE_REGEXP, function (letter, pos) { | ||
return (pos ? separator : '') + letter.toLowerCase(); | ||
}); | ||
this.shakeCase = function (name) { | ||
var SNAKE_CASE_REGEXP = /[A-Z]/g; | ||
var separator = '-'; | ||
return name.replace(SNAKE_CASE_REGEXP, function (letter, pos) { | ||
return (pos ? separator : '') + letter.toLowerCase(); | ||
}); | ||
}; | ||
this.minifyHtml = function (source) { | ||
var escapeContent = function (content) { | ||
var quotRegexp = /\'/g; | ||
var breaklineRegexp = /(?:\r\n|\r|\n)/g; | ||
var first = content.replace(quotRegexp, '\\\'').replace(breaklineRegexp, ' '); | ||
return first; | ||
}; | ||
this.minifyHtml = function (source) { | ||
var escapeContent = function (content) { | ||
var quotRegexp = /\'/g; | ||
var breaklineRegexp = /(?:\r\n|\r|\n)/g; | ||
var first = content.replace(quotRegexp, '\\\'').replace(breaklineRegexp, ' '); | ||
return first; | ||
}; | ||
var result = minify(source); | ||
result = result.replace(/(^\s*)/gm, ''); | ||
return escapeContent(result); | ||
} | ||
var result = minify(source); | ||
result = result.replace(/(^\s*)/gm, ''); | ||
return escapeContent(result); | ||
this.getPageTemplateUrl = function (page) { | ||
return '#_page_' + page.name; | ||
}; | ||
this.writeContent = function (grunt, path, content) { | ||
grunt.file.write(path, '\ufeff' + content, {encoding: 'utf8'}); | ||
} | ||
this.getAreaScripts = function (grunt, areaPath, initPath) { | ||
var tsScripts = grunt.file.expand(areaPath + '/**/*.ts'); | ||
var jsScripts = []; | ||
for (var i = tsScripts.length - 1; i >= 0; i--) { | ||
var jsScript = tsScripts[i].replace(/\.ts$/, '.js'); | ||
if (grunt.file.exists(jsScript)) { | ||
jsScripts.push(jsScript); | ||
} | ||
} | ||
// place _init.js at the end of file set | ||
jsScripts.push(initPath); | ||
return jsScripts; | ||
}; | ||
this.getPageTemplateUrl = function (page) { | ||
return '#_page_' + page.name; | ||
}; | ||
this.getBootstrapScripts = function (bootstrapScripts, areasConfigPath, routesConfigPath, valuesConfigPath) { | ||
this.writeContent = function (grunt, path, content) { | ||
grunt.file.write(path, '\ufeff' + content, { encoding: 'utf8' }); | ||
var wildcards = { | ||
'%areas_config%': areasConfigPath, | ||
'%routes_config%': routesConfigPath, | ||
'%values_config%': valuesConfigPath | ||
} | ||
this.getAreaScripts = function (grunt, areaPath, initPath) { | ||
var tsScripts = grunt.file.expand(areaPath + '/**/*.ts'); | ||
var jsScripts = []; | ||
for (var i = 0; i < bootstrapScripts.length; i++) { | ||
var scriptPath = bootstrapScripts[i]; | ||
if (wildcards[scriptPath]) { | ||
bootstrapScripts[i] = wildcards[scriptPath] | ||
} | ||
} | ||
return bootstrapScripts; | ||
}; | ||
for (var i = tsScripts.length - 1; i >= 0; i--) { | ||
var jsScript = tsScripts[i].replace(/\.ts$/, '.js'); | ||
if (grunt.file.exists(jsScript)) { | ||
jsScripts.push(jsScript); | ||
} | ||
} | ||
// place _init.js at the end of file set | ||
jsScripts.push(initPath); | ||
return jsScripts; | ||
}; | ||
this.getAppStyles = function (grunt, baseCss, appPath) { | ||
return baseCss.concat(grunt.file.expand(appPath + '/**/*.css')); | ||
}; | ||
this.getBootstrapScripts = function (bootstrapScripts, areasConfigPath, routesConfigPath, valuesConfigPath) { | ||
/** | ||
* Split attributes definition string by object for jasper client library usage | ||
* @param attrs string that represents attributes | ||
* @returns {name: '', type: ''} collection of attributes | ||
*/ | ||
this.getJasperAttributes = function (attrs) { | ||
if (typeof (attrs) === 'string') { | ||
var resultAttrs = []; | ||
var wildcards = { | ||
'%areas_config%': areasConfigPath, | ||
'%routes_config%': routesConfigPath, | ||
'%values_config%': valuesConfigPath | ||
} | ||
for (var i = 0; i < bootstrapScripts.length; i++) { | ||
var scriptPath = bootstrapScripts[i]; | ||
if(wildcards[scriptPath]){ | ||
bootstrapScripts[i] = wildcards[scriptPath] | ||
var attrsParts = attrs.split(' '); | ||
attrsParts.forEach(function (part) { | ||
var indx = part.indexOf(':'); | ||
if (indx > -1) { | ||
// attr type specified | ||
var attrName = part.substring(0, indx); | ||
var attrType = part.substring(indx + 1, part.length); | ||
resultAttrs.push({name: attrName, type: attrType}); | ||
} else { | ||
resultAttrs.push({name: part}); | ||
} | ||
} | ||
return bootstrapScripts; | ||
}; | ||
}); | ||
this.getAppStyles = function (grunt, baseCss, appPath) { | ||
return baseCss.concat(grunt.file.expand(appPath + '/**/*.css')); | ||
}; | ||
return resultAttrs; | ||
} | ||
return attrs; | ||
}; | ||
/** | ||
* Split attributes definition string by object for jasper client library usage | ||
* @param attrs string that represents attributes | ||
* @returns {name: '', type: ''} collection of attributes | ||
*/ | ||
this.getJasperAttributes = function(attrs) { | ||
if(typeof (attrs) ==='string'){ | ||
var resultAttrs = []; | ||
this.isAbsUrl = function (url) { | ||
var r = new RegExp('^(?:[a-z]+:)?//', 'i'); | ||
return r.test(url); | ||
}; | ||
var attrsParts = attrs.split(' '); | ||
attrsParts.forEach(function(part){ | ||
var indx = part.indexOf(':'); | ||
if(indx > -1){ | ||
// attr type specified | ||
var attrName = part.substring(0, indx); | ||
var attrType = part.substring(indx + 1, part.length); | ||
resultAttrs.push({name: attrName, type: attrType}); | ||
}else{ | ||
resultAttrs.push({name: part}); | ||
} | ||
}); | ||
return resultAttrs; | ||
} | ||
return attrs; | ||
}; | ||
/** | ||
* Excludes from @scripts absolutes script paths and returns array of excluded paths | ||
* @param scripts Array of scripts | ||
*/ | ||
this.excludeAbsScripts = function(scripts){ | ||
var result = []; | ||
for(var i = scripts.length - 1; i >= 0; i--){ | ||
if(this.isAbsUrl(scripts[i])) { | ||
result.push(scripts.splice( i, 1)[0]); | ||
} | ||
} | ||
return result; | ||
}; | ||
}; | ||
module.exports = new JasperUtils(); |
@@ -178,3 +178,3 @@ /* | ||
return undefined; | ||
} | ||
}; | ||
@@ -185,7 +185,14 @@ areas.forEach(function (area) { | ||
area.__scripts = utils.getAreaScripts(grunt, area.__path, area.__initPath); | ||
area.__scripts = (area.scripts || []).concat(area.__scripts); | ||
if (options.package) { | ||
if(area.bootstrap && area.scripts && area.scripts.length){ | ||
grunt.log.error('Configuration error: bootstrap area \"' + area.name + '\" must not contains "scripts". Use "bootstrapScripts" instead') | ||
return; | ||
} | ||
if (!area.bootstrap) { | ||
// get all area external scripts | ||
var areaScrtips = utils.excludeAbsScripts(area.__scripts); | ||
// during package build each area represents by one .js file | ||
config.scripts = ['scripts/' + area.name + '.min.js']; | ||
areaScrtips.push('scripts/' + area.name + '.min.js'); | ||
config.scripts = areaScrtips; | ||
} | ||
@@ -218,3 +225,3 @@ } else { | ||
var fileName = '_routes.' + this.target + '.js'; | ||
var routesConfigPath = options.appPath + '/' + fileName; | ||
routesConfigPath = options.appPath + '/' + fileName; | ||
@@ -248,2 +255,3 @@ utils.writeContent(grunt, routesConfigPath, routesConfigScript); | ||
// boostrapscripts - scrtips, that are loaded at first (base.js): angular, jasper etc... | ||
var processedBootstrapScripts = utils.getBootstrapScripts(options.bootstrapScripts, areasConfigPath, routesConfigPath, valuesConfigPath); | ||
@@ -250,0 +258,0 @@ |
@@ -36,8 +36,7 @@ 'use strict'; | ||
var wildcards = { | ||
'%areas_config%': config.appPath + '/_areas.debug.js', | ||
'%values_config%': config.appPath + '/_values.debug.js', | ||
'%routes_config%': config.appPath + '/_routes.debug.js' | ||
'%areas_config%': config.appPath + '/_areas.debug.js', | ||
'%values_config%': config.appPath + '/_values.debug.js', | ||
'%routes_config%': config.appPath + '/_routes.debug.js' | ||
}; | ||
if(wildcards[path]) | ||
{ | ||
if (wildcards[path]) { | ||
return wildcards[path]; | ||
@@ -76,3 +75,3 @@ } | ||
var styles = grunt.file.expand(path.join(appPath, '/app/**/*.css')); | ||
var parts = [ 'test/testApp/base.css' ]; | ||
var parts = ['test/testApp/base.css']; | ||
styles.forEach(function (path) { | ||
@@ -99,2 +98,6 @@ parts.push('<link rel="stylesheet" href="' + path + '"/>'); | ||
var parts = [ | ||
'test/testApp/scripts/custom.js', | ||
'//path/to/external/script.js', | ||
'http://another.path/to/external/script.js', | ||
appPath + '/app/core/pages/home-page/HomePage.js', | ||
@@ -174,3 +177,4 @@ appPath + '/app/core/components/site-footer/SiteFooter.js', | ||
test.ok(grunt.file.exists(routePath)); | ||
var configObject = testUtils.parseRoutesConfig(routePath);; | ||
var configObject = testUtils.parseRoutesConfig(routePath); | ||
; | ||
@@ -177,0 +181,0 @@ test.equals(configObject.defaultRoutePath, '/'); |
@@ -58,5 +58,41 @@ 'use strict'; | ||
test.done(); | ||
}, | ||
testAbsUrlDetection: function (test) { | ||
var params = { | ||
'/path/to/script.js': false, | ||
'path/to/script.js': false, | ||
'http://path.to/script.js': true, | ||
'https://path.to/script.js?v=2': true, | ||
'//maps.googleapis.com/maps/api/js?key=qwe8&callback=onMapReady': true | ||
}; | ||
for(var addr in params){ | ||
test.ok(utils.isAbsUrl(addr) === params[addr], addr + ' not excepted as abs url'); | ||
} | ||
test.done(); | ||
}, | ||
testAbsUrlExclution: function (test) { | ||
var scripts = [ | ||
'path/to/script.js', | ||
'/path/to/script.js', | ||
'https://google.com/script.js', | ||
'scripts/temp.js' | ||
]; | ||
var absScripts = utils.excludeAbsScripts(scripts); | ||
test.ok(absScripts.length === 1, 'Result of exclusion must contains excluded scripts'); | ||
test.ok(absScripts[0] === 'https://google.com/script.js', 'Result of exclusion must contains excluded scripts'); | ||
test.ok(scripts.length === 3); | ||
test.ok(scripts[0] === 'path/to/script.js'); | ||
test.ok(scripts[1] === '/path/to/script.js'); | ||
test.ok(scripts[2] === 'scripts/temp.js'); | ||
test.done(); | ||
} | ||
}; |
@@ -90,4 +90,10 @@ 'use strict'; | ||
test.strictEqual(configObject.boot.scripts, undefined, 'Scripts of bootstraped aread must be undefined') | ||
// test external scripts: | ||
test.ok(configObject.core.scripts.length === 3, 'Core area must contains 3 scripts after package: 1 area script and 2 external scripts'); | ||
test.ok(configObject.core.scripts[0] === 'http://another.path/to/external/script.js', 'Core area must contains external script'); | ||
test.ok(configObject.core.scripts[1] === '//path/to/external/script.js', 'Core area must contains external script'); | ||
test.ok(configObject.core.scripts[2] === 'scripts/core.min.js','Core area must contains area script'); | ||
test.strictEqual(configObject.boot.scripts, undefined, 'Scripts of bootstrapped area must be undefined') | ||
ensurePartsExistence(test, configObject.feature.dependencies, ['core']); | ||
@@ -94,0 +100,0 @@ ensurePartsExistence(test, configObject.core.scripts, ['scripts/core.min.js']); |
{ | ||
"name": "core", | ||
"bootstrap": false | ||
"name": "core", | ||
"bootstrap": false, | ||
"scripts": [ | ||
"test/testApp/scripts/custom.js", | ||
"//path/to/external/script.js", | ||
"http://another.path/to/external/script.js" | ||
] | ||
} |
46712
46
1033