Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(build): correct detection and generation ngMaterial module defini…
Browse files Browse the repository at this point in the history
…tion

Correct the dynamic generation of the `ngMaterial` module definition with dependencies on angular material components only.
```js
angular.module('ngMaterial', ["ng","ngAnimate","ngAria","material.core","material.core.gestures","material.layout".... ]);
```
  • Loading branch information
ThomasBurleson committed Aug 25, 2015
1 parent e4ca61f commit d85e14a
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 127 deletions.
63 changes: 45 additions & 18 deletions config/ngModuleData.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
// Regex adapted from https://github.com/ceymard/gulp-ngcompile

module.exports = function processContent(fileContent, materialOnly) {
var NG_MODULE_REGEX = materialOnly ? /\.module\(('material\.[^']*'|"material\.[^"]*")\s*,(?:\s*\[([^\]]+)\])?/g : /\.module\(('[^']*'|"[^"]*")\s*,(?:\s*\[([^\]]+)\])?/g;
var match = NG_MODULE_REGEX.exec(fileContent || '');
var module = match && match[1] && match[1].slice(1, -1); //remove quotes
var depsMatch = match && match[2] && match[2].trim();

var dependencies = [];
if (depsMatch) {
dependencies = depsMatch.split(/\s*,\s*/).map(function(dep) {
dep = dep.slice(1, -1); //remove quotes
return dep;
});
/**
* The Angular Material module `ngMaterial` is generated by scanning all Material components
* for valid module definitions. @see gulp-utils.js ::buildNgMaterialDefinition()
*
* angular.module('ngMaterial', [
* "ng","ngAnimate","ngAria",
* "material.core","material.core.gestures","material.layout","material.core.theming.palette",
* ...
* ]);
*
*/

// Define patterns for AngularJS Module definitions

var MATERIAL_ONLY = /\.module\(['|"](material\.[a-zA-Z\-\.]*)['|"]\s*,(\s*\[([^\]]*)\])/;
var ANY = /\.module\(('[^']*'|"[^"]*")\s*,(?:\s*\[([^\]]+)\])?/;

/**
* Find module definition s that match the module definition pattern
*/
function buildScanner(pattern) {

return function findPatternIn(content) {
var dependencies;
var match = pattern.exec(content || '');
var moduleName = match ? match[1].replace(/\'/gi,'') : null;
var depsMatch = match && match[2] && match[2].trim();

if (depsMatch) {
dependencies = depsMatch.split(/\s*,\s*/).map(function(dep) {
dep = dep.trim().slice(1, -1); //remove quotes
return dep;
});
}

return match ? {
name : moduleName || '',
module : moduleName || '',
dependencies : dependencies || [ ]
} : null;
}
}

return {
module: module || '',
dependencies: dependencies
};
module.exports = {
material : buildScanner( MATERIAL_ONLY ),
any : buildScanner( ANY )
};

5 changes: 2 additions & 3 deletions docs/app/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var DocsApp = angular.module('docsApp', ['ngMaterial', 'ngRoute', 'angularytics', 'ngMessages'])
var DocsApp = angular.module('docsApp', [ 'angularytics', 'ngRoute', 'ngMessages', 'ngMaterial' ])

.config([
'SERVICES',
Expand Down Expand Up @@ -716,5 +716,4 @@ function($rootScope, $scope, component, demos, $http, $templateCache, $q) {
}
return str;
};
})
;
});
65 changes: 34 additions & 31 deletions scripts/gulp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var Buffer = require('buffer').Buffer;
var fs = require('fs');

var path = require('path');

var getModuleInfo = require('../config/ngModuleData.js');
var findModule = require('../config/ngModuleData.js');

exports.humanizeCamelCase = function(str) {
switch (str) {
Expand Down Expand Up @@ -77,7 +76,7 @@ exports.readModuleDemos = function(moduleName, fileTasks) {
} else {
var fileType = path.extname(file.path).substring(1);
if (fileType == 'js') {
demo.ngModule = demo.ngModule || getModuleInfo(file.contents.toString());
demo.ngModule = demo.ngModule || findModule.any(file.contents.toString());
}
demo[fileType] && demo[fileType].push(toDemoObject(file));
}
Expand Down Expand Up @@ -106,8 +105,8 @@ exports.pathsForModule = function(name) {
function lookupPath() {
gulp.src('src/{services,components,core}/**/*')
.pipe(through2.obj(function(file, enc, next) {
var modName = getModuleInfo(file.contents).module;
if (modName == name) {
var module = findModule.any(file.contents);
if (module && module.name == name) {
var modulePath = file.path.split(path.sep).slice(0, -1).join(path.sep);
pathsForModules[name] = modulePath + '/**';
}
Expand All @@ -123,8 +122,8 @@ exports.filesForModule = function(name) {
} else {
return gulp.src('src/{services,components,core}/**/*')
.pipe(through2.obj(function(file, enc, next) {
var modName = getModuleInfo(file.contents).module;
if (modName == name) {
var module = findModule.any(file.contents);
if (module && (module.name == name)) {
var modulePath = file.path.split(path.sep).slice(0, -1).join(path.sep);
pathsForModules[name] = modulePath + '/**';
var self = this;
Expand Down Expand Up @@ -165,29 +164,33 @@ exports.appendToFile = function(filePath) {
};

exports.buildNgMaterialDefinition = function() {
var buffer = [];
var srcBuffer = [];
var modulesSeen = [];
var count = 0;
return through2.obj(function(file, enc, next) {
var moduleName;
if (moduleName = getModuleInfo(file.contents, true).module) {
modulesSeen.push(moduleName);
}
buffer.push(file);
var module = findModule.material(file.contents);
if (module) modulesSeen.push(module.name);
srcBuffer.push(file);
next();
}, function(done) {
var EXPLICIT_DEPS = ['ng', 'ngAnimate', 'ngAria'];
var angularFileContents = "angular.module('ngMaterial', " + JSON.stringify(EXPLICIT_DEPS.concat(modulesSeen)) + ');';
var self = this;
var requiredLibs = ['ng', 'ngAnimate', 'ngAria'];
var dependencies = JSON.stringify(requiredLibs.concat(modulesSeen));
var ngMaterialModule = "angular.module('ngMaterial', " + dependencies + ');';
var angularFile = new gutil.File({
base: process.cwd(),
path: process.cwd() + '/ngMaterial.js',
contents: new Buffer(angularFileContents)
contents: new Buffer(ngMaterialModule)
});
this.push(angularFile);
var self = this;
buffer.forEach(function(file) {

// Elevate ngMaterial module registration to first in queue
self.push(angularFile);

srcBuffer.forEach(function(file) {
self.push(file);
});
buffer = [];

srcBuffer = [];
done();
});
};
Expand All @@ -197,8 +200,8 @@ function moduleNameToClosureName(name) {
}
exports.addJsWrapper = function(enforce) {
return through2.obj(function(file, enc, next) {
var moduleInfo = getModuleInfo(file.contents);
if (!!enforce || moduleInfo.module) {
var module = findModule.any(file.contents);
if (!!enforce || module) {
file.contents = new Buffer([
!!enforce ? '(function(){' : '(function( window, angular, undefined ){',
'"use strict";\n',
Expand All @@ -212,18 +215,18 @@ exports.addJsWrapper = function(enforce) {
};
exports.addClosurePrefixes = function() {
return through2.obj(function(file, enc, next) {
var moduleInfo = getModuleInfo(file.contents);
if (moduleInfo.module) {
var closureModuleName = moduleNameToClosureName(moduleInfo.module);
var requires = (moduleInfo.dependencies || []).sort().map(function(dep) {
return dep.indexOf(moduleInfo.module) === 0 ? '' : 'goog.require(\'' + moduleNameToClosureName(dep) + '\');';
var module = findModule.any(file.contents);
if (module) {
var closureModuleName = moduleNameToClosureName(module.name);
var requires = (module.dependencies || []).sort().map(function(dep) {
return dep.indexOf(module.name) === 0 ? '' : 'goog.require(\'' + moduleNameToClosureName(dep) + '\');';
}).join('\n');

file.contents = new Buffer([
'goog.provide(\'' + closureModuleName + '\');',
requires,
file.contents.toString(),
closureModuleName + ' = angular.module("' + moduleInfo.module + '");'
closureModuleName + ' = angular.module("' + module.name + '");'
].join('\n'));
}
this.push(file);
Expand All @@ -234,10 +237,10 @@ exports.addClosurePrefixes = function() {
exports.buildModuleBower = function(name, version) {
return through2.obj(function(file, enc, next) {
this.push(file);
var moduleInfo = getModuleInfo(file.contents);
if (moduleInfo.module) {
var module = findModule.any(file.contents);
if (module) {
var bowerDeps = {};
(moduleInfo.dependencies || []).forEach(function(dep) {
(module.dependencies || []).forEach(function(dep) {
var convertedName = 'angular-material-' + dep.split('.').pop();
bowerDeps[convertedName] = version;
});
Expand Down
9 changes: 5 additions & 4 deletions src/components/bottomSheet/bottom-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* @description
* BottomSheet
*/
angular.module('material.components.bottomSheet', [
'material.core',
'material.components.backdrop'
])
angular
.module('material.components.bottomSheet', [
'material.core',
'material.components.backdrop'
])
.directive('mdBottomSheet', MdBottomSheetDirective)
.provider('$mdBottomSheet', MdBottomSheetProvider);

Expand Down
6 changes: 3 additions & 3 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var SELECT_EDGE_MARGIN = 8;
var selectNextId = 0;

angular.module('material.components.select', [
'material.core',
'material.components.backdrop'
])
'material.core',
'material.components.backdrop'
])
.directive('mdSelect', SelectDirective)
.directive('mdSelectMenu', SelectMenuDirective)
.directive('mdOption', OptionDirective)
Expand Down
4 changes: 2 additions & 2 deletions src/components/sidenav/sidenav.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* @description
* A Sidenav QP component.
*/
angular.module('material.components.sidenav', [
angular
.module('material.components.sidenav', [
'material.core',
'material.components.backdrop'
])
Expand All @@ -16,7 +17,6 @@ angular.module('material.components.sidenav', [


/**
* @private
* @ngdoc service
* @name $mdSidenav
* @module material.components.sidenav
Expand Down
Loading

0 comments on commit d85e14a

Please sign in to comment.