From b6933aafe1f8b59c2dec2cb82a1951aff7e77c67 Mon Sep 17 00:00:00 2001 From: James Robey Date: Thu, 22 Oct 2015 13:20:31 -0600 Subject: [PATCH 1/3] Add the verbose and noParse options through a dictionary provided to the AngularWebpackPlugin in webpack.config.js. noParse expects a dictionary where the keys are exact module names and the values are strings or regexes that represent dependencies that, when detected on an angular.module() definition, will be ignored. Verbose merely pretty prints angular.modules() with dependencies and skipped dependencies as they are found. noParse allows for angular code that cannot be modified for use with webpack to be successfully packaged. --- lib/index.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index ab83ec5..631ad71 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,9 @@ // Main entry point of the angular-webpack-plugin module // Defines a plugin for webpack to help it understand angular modules. +// 10-2015: Modified to support verbose and noParse options +// by James Robey + var path = require('path'); var LocalModulesHelpers = require("webpack/lib/dependencies/LocalModulesHelpers"); @@ -14,7 +17,27 @@ var RequireHeaderDependency = require("webpack/lib/dependencies/RequireHeaderDep var AngularModuleDependency = require('./AngularModuleDependency'); var AngularModuleDefinition = require('./AngularModuleDefinition'); -function AngularPlugin() { +//options provided will apply to all instances of AngularPlugin, but +//usage with webpack requires only a single instance. This eases +//access to these options in contexts without access to AngularPlugin +//during compilation, below. +var noParse, verbose; +function AngularPlugin(options) { + + //should we print information to the console about angular + //modules and dependencies as we find/process them? + verbose = options.verbose; + + //specify angular module dependencies that should be ignored: + + //a dictionary where the keys are exact angular modules names (or "*", + //for matching all modules) where the value is either one, or a list, + //of strings and/or regexes that, if they match a dependency specified on an + //angular module definition, will be ignored as a dependency to be + //handled by webpack. This is essential when porting projects to + //webpack whose source may not change to accomodate webpack conventions + //as is the case, for instance, with angular-ui-bootstrap builtin templates. + noParse = options.noParse || {}; } module.exports = AngularPlugin; @@ -84,10 +107,28 @@ AngularPlugin.prototype = { addAngularVariable: function(parser) { return ModuleParserHelpers.addParsedVariable(parser, 'angular', "require('exports?window.angular!angular')"); }, - + + lastModule:false, + lastContext:false, + lastRawRequest:false, + // Each call to `angular.module()` is analysed here parseModuleCall: function(parser, expr){ this.addAngularVariable(parser); + + if(verbose && this.lastContext != parser.state.current.context){ + console.log("\ncd", parser.state.current.context); + this.lastContext = parser.state.current.context; + } + + if(verbose && this.lastRawRequest != parser.state.current.rawRequest){ + console.log("\n require(\""+parser.state.current.rawRequest+"\")"); + this.lastRawRequest = parser.state.current.rawRequest; + } + + this.lastModule = expr.arguments[0].value; + verbose && console.log(" angular.module(\""+this.lastModule+"\")"); + switch(expr.arguments.length){ case 1: return this._parseModuleCallSingleArgument(parser, expr); case 2: return this._parseModuleCallTwoArgument(parser, expr); @@ -214,7 +255,7 @@ AngularPlugin.prototype = { parser.state.current.addDependency(dep); return true; }, - + // A dependency (module) has been found _addDependency: function(parser, expr, param){ if( param.isConditional() ){ @@ -232,13 +273,32 @@ AngularPlugin.prototype = { return true; } if( param.isString() ){ - var dep; - var localModule = LocalModulesHelpers.getLocalModule(parser.state, param.string); + var dep, localModule; + + //see JROBEY comment above; skip paths that refer to templates that defy dependency "rules". + for(var key in noParse){ + if(key == '*' || key == this.lastModule){ + var to_match = noParse[key] instanceof Array ? noParse[key] : [noParse[key]]; + + for(var i = 0; i !== to_match.length; i ++){ + var consider = to_match[i]; + + if((consider.exec && consider.exec(param.string)) || param.string == consider){ + verbose && console.log(" (skip", param.string+")"); + return true; + } + } + } + } + + localModule = LocalModulesHelpers.getLocalModule(parser.state, param.string); if( localModule ) { return true; } + dep = new AngularModuleDependency(param.string, param.range); dep.loc = param.loc; + verbose && param.string !== this.lastModule && console.log(" ", param.string); parser.state.current.addDependency(dep); return true; } From 9152b51b29a177e6c404e67e8488494f2bc6199d Mon Sep 17 00:00:00 2001 From: James Robey Date: Thu, 22 Oct 2015 13:38:27 -0600 Subject: [PATCH 2/3] fix case where no options given --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 631ad71..481407d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,10 +23,11 @@ var AngularModuleDefinition = require('./AngularModuleDefinition'); //during compilation, below. var noParse, verbose; function AngularPlugin(options) { + options = options || {}; //should we print information to the console about angular //modules and dependencies as we find/process them? - verbose = options.verbose; + verbose = options.verbose || false; //specify angular module dependencies that should be ignored: From ff0c2500a6ad266b0903930d9f282641b9123202 Mon Sep 17 00:00:00 2001 From: James Robey Date: Thu, 22 Oct 2015 14:00:21 -0600 Subject: [PATCH 3/3] minor comment change --- lib/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 481407d..a733cec 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,9 +4,6 @@ // Main entry point of the angular-webpack-plugin module // Defines a plugin for webpack to help it understand angular modules. -// 10-2015: Modified to support verbose and noParse options -// by James Robey - var path = require('path'); var LocalModulesHelpers = require("webpack/lib/dependencies/LocalModulesHelpers");