This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (57 loc) · 2.62 KB
/
index.js
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
var ModuleParserHelpers = require('webpack/lib/ModuleParserHelpers');
var NullFactory = require('webpack/lib/NullFactory');
var PushDependency = require('./src/PushDependency');
var ngrequire = require('ngrequire');
var _ = require('lodash');
var s = require('./src/string');
var path = require('path');
function apply(options, compiler) {
// Clean cache
ngrequire.clean();
compiler.plugin('compilation', function(compilation, params) {
compilation.dependencyFactories.set(PushDependency, new NullFactory());
compilation.dependencyTemplates.set(PushDependency, new PushDependency.Template());
});
compiler.parser.plugin('call angular.module', function (expression) {
var self = this;
var filePath = self.state.current.resource || self.state.module.resource;
var requireStatement = "require('{0}')";
var angularDecoratorStatement = s.escapeSlashes(requireStatement.f(path.resolve(__dirname, './src/angularDecorator')));
var moduleLoaderStatement = s.escapeSlashes(requireStatement.f(path.resolve(__dirname, './src/moduleLoader')));
ModuleParserHelpers.addParsedVariable(compiler.parser, 'angular', angularDecoratorStatement);
// Update cache
ngrequire.update(options.include, options.options);
// Add dependencies
var meta = ngrequire.getMeta(filePath);
if (meta) {
ModuleParserHelpers.addParsedVariable(compiler.parser, '__ngrequire_load__', moduleLoaderStatement);
var deps = ngrequire.getMissingDependencies(filePath);
var currentModule = meta.moduleName;
var requiredModules = _.uniq(_.map(deps, 'moduleName'));
var relativePaths = _.uniq(_.map(deps, 'relativePath'));
_.each(relativePaths, function (relativePath, index) {
var normalizedName = '__ngrequire_module_{0}__'.f(index);
ModuleParserHelpers.addParsedVariable(compiler.parser, normalizedName, requireStatement.f(relativePath));
});
self.state.current.addDependency(new PushDependency(currentModule, requiredModules, filePath, expression));
}
});
}
module.exports = function(options) {
if (options instanceof Array) {
options = {
include: options
};
}
if (!Array.isArray(options.include)) {
options.include = [ options.include ];
}
// Change all path to absolute
var cwd = options.basePath || process.cwd();
options.include = options.include.map(function (include) {
return path.resolve(cwd, include);
});
return {
apply: apply.bind(this, options)
};
};