This repository was archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
307 lines (270 loc) · 11.3 KB
/
index.js
File metadata and controls
307 lines (270 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// © Copyright 2014 Paul Thomas <paul@stackfull.com>.
// Licensed under the MIT license.
//
// Main entry point of the angular-webpack-plugin module
// Defines a plugin for webpack to help it understand angular modules.
var path = require('path');
var LocalModulesHelpers = require("webpack/lib/dependencies/LocalModulesHelpers");
var NullFactory = require("webpack/lib/NullFactory");
var ModuleParserHelpers = require("webpack/lib/ModuleParserHelpers");
var RequireHeaderDependency = require("webpack/lib/dependencies/RequireHeaderDependency");
var AngularModuleDependency = require('./AngularModuleDependency');
var AngularModuleDefinition = require('./AngularModuleDefinition');
//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) {
options = options || {};
//should we print information to the console about angular
//modules and dependencies as we find/process them?
verbose = options.verbose || false;
//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;
function bindCallbackMethod(source, plugname, obj, method){
source.plugin(plugname, method.bind(obj, source));
}
function containsSlash(str){
return str.indexOf("/") >= 0 || str.indexOf("\\") >= 0;
}
// Try to resolve a file of the form /somepath/module/module
// (calling it modmod for want of a better term)
function resolveModModFile(resolver, request, callback){
var joined = path.join(request.request, request.request);
return resolver.doResolve("file", {
path: request.path,
request: joined,
query: request.query
}, callback, true);
}
function requestIsModModFile(request){
if( containsSlash(request.request) || ! request.file ){
return false;
}
var starting = request.path.length - request.request.length;
var match = request.path.lastIndexOf(request.request);
return match === starting &&
(request.path[starting-1] === '/' || request.path[starting-1] === '\\');
}
AngularPlugin.prototype = {
constructor: AngularPlugin,
// This is the entrypoint that is called when the plugin is added to a
// compiler
apply: function(compiler){
bindCallbackMethod(compiler, "compilation",
this, this.addDependencyFactories);
bindCallbackMethod(compiler.parser, "expression window.angular",
this, this.addAngularVariable);
bindCallbackMethod(compiler.parser, "expression angular",
this, this.addAngularVariable);
bindCallbackMethod(compiler.parser, "call angular.module",
this, this.parseModuleCall);
bindCallbackMethod(compiler.resolvers.normal, "module-module",
this, this.resolveModule);
},
// #### Plugin Callbacks
// This sets up the compiler with the right dependency module factories
addDependencyFactories: function(compiler, compilation, params){
compilation.dependencyFactories.set(AngularModuleDependency,
params.normalModuleFactory);
compilation.dependencyTemplates.set(AngularModuleDependency,
new AngularModuleDependency.Template());
compilation.dependencyFactories.set(AngularModuleDefinition,
new NullFactory());
compilation.dependencyTemplates.set(AngularModuleDefinition,
new AngularModuleDefinition.Template());
},
// This injects the angular module wherever it's used.
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);
case 3: return this._parseModuleCallThreeArgument(parser, expr);
default:
console.warn("Don't recognise angular.module() with " +
expr.arguments.length + " args");
}
},
// Additional module resolving specific to angular modules.
//
// We're trying to follow as many existing conventions as possible. Including:
// - dots as path separators
// - camelCase module names convert to dashed file names
// - module, directory and file names all the same.
// - files containing multiple modules (shared prefix)
//
resolveModule: function(resolver, request, callback){
if( containsSlash(request.request) ){
return callback();
}
var split = request.request.split('.');
if( split.length === 1 ) {
if( ! requestIsModModFile(request) ){
return resolveModModFile(resolver, request, callback);
}
}else{
// Try treating `.` as a path separator, but in a non-greedy way.
// There are lots of options here because we allow the file name to be
// just a prefix.
// prefer the segments to be directories and prefer the full name to be
// used.
var namespaced = [], ns, mod;
for( var j = 0; j < split.length; j++ ){
ns = split.slice(0, -j);
mod = split.slice(-j);
namespaced.push({
namespace: ns,
module: mod.join('.')
});
}
for( var k = 0; k < split.length; k++ ){
ns = split.slice(0, -k);
mod = split.slice(-k);
for( var i = 1; i < mod.length; i++ ){
namespaced.push({
namespace: ns,
module: mod.slice(0, -i).join('.')
});
}
}
namespaced.shift();
return resolver.forEachBail(namespaced, function(nsmod, cb){
callback.log("resolve module " + nsmod.module +
" in namespace " + JSON.stringify(nsmod.namespace));
var req = {
path: request.path,
request: path.join.apply(path, nsmod.namespace.concat(nsmod.module)),
query: request.query
};
callback.log(JSON.stringify(req));
return resolver.doResolve("module", req, cb, true);
}, function(err, resolved){
if( err || !resolved ){
return callback(err);
}
if( resolved.file ){
// We're taking this as resolved. It should contain other modules with
// this prefix
return resolver.doResolve("result", resolved, callback);
}
if(callback.log){
callback.log(" is not an angular module");
}
callback();
});
}
return callback();
},
// #### Private Methods
// A single argument is the equivalent of `require()`
// angular.module(name)
_parseModuleCallSingleArgument: function(parser, expr) {
var mod = parser.evaluateExpression(expr.arguments[0]);
return this._addDependency(parser, expr, mod);
},
// 2 arguments mean a module definition if the 2nd is an array.
// angular.module(name, requires)
// angular.module(name, configFn)
_parseModuleCallTwoArgument: function(parser, expr) {
var mod = parser.evaluateExpression(expr.arguments[0]);
var deps = parser.evaluateExpression(expr.arguments[1]);
this._addModuleDefinition(parser, expr, mod);
if( deps.items ){
return deps.items.every(
this._addDependency.bind(this, parser, expr));
}
return this._addDependency(parser, expr, mod);
},
// 3 arguments must be a module definition
// angular.module(name, requires, configFn)
_parseModuleCallThreeArgument: function(parser, expr) {
var mod = parser.evaluateExpression(expr.arguments[0]);
var deps = parser.evaluateExpression(expr.arguments[1]);
this._addModuleDefinition(parser, expr, mod);
// Some libraries, such as angular-classy, will break compilation without this check
if( !deps.items ){
return true;
}
return deps.items.every(
this._addDependency.bind(this, parser, expr));
},
_addModuleDefinition: function(parser, expr, mod){
var dep = new AngularModuleDefinition(expr.range, mod.string);
dep.loc = expr.loc;
dep.localModule = LocalModulesHelpers.addLocalModule(parser.state, mod.string);
parser.state.current.addDependency(dep);
return true;
},
// A dependency (module) has been found
_addDependency: function(parser, expr, param){
if( param.isConditional() ){
// TODO: not sure what this will output
parser.state.current.addDependency(
new RequireHeaderDependency(expr.callee.range));
param.options.forEach(function(param) {
var result = parser.applyPluginsBailResult("call require:commonjs:item",
expr, param);
if(result === undefined) {
throw new Error(
"Cannot convert options with mixed known and unknown stuff");
}
});
return true;
}
if( param.isString() ){
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;
}
parser.applyPluginsBailResult("call require:commonjs:context", expr, param);
return true;
}
};