forked from brix/crypto-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modularize.js
95 lines (74 loc) · 2.27 KB
/
modularize.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*jshint node: true*/
var _ = require("lodash"),
fmd = require("fmd");
module.exports = function (grunt) {
grunt.registerMultiTask('modularize', function () {
var options = this.options(),
done = this.async(),
modules = {},
config = {
target: this.target + '/',
factories: ["commonjs", "amd", "global"],
trim_whitespace: true,
new_line: "unix",
indent: "\t"
};
// Prepare Factory-Module-Definition settings
_.each(options, function (conf, name) {
var sources = [],
opts = {
depends: {}
},
deps = [];
if (conf.exports) {
opts.exports = conf.exports;
}
if (conf.global) {
opts.global = conf.global;
}
// Find and add self as source
_.each(this.filesSrc, function (source) {
if (grunt.file.exists(source + name + ".js")) {
sources.push(source + name + ".js");
}
}, this);
if (conf.pack) {
// Collect all components
deps = _.chain(conf.components)
.map(function (depName) {
return options[depName].components;
})
.flatten()
.uniq()
.without(name)
.sort(function (a, b) {
return options[a].components.indexOf(b) === -1 ? -1 : 1;
})
.value();
// Add components as source files -> results a single file
_.each(this.filesSrc, function (source) {
_.each(deps, function (depName) {
if (grunt.file.exists(source + depName + ".js")) {
sources.push(source + depName + ".js");
}
});
}, this);
} else {
// Read components and add them as dependecies
_.each(_.without(conf.components, name), function (value, i) {
opts.depends['./' + value] = value === "core" ? "CryptoJS" : null;
});
}
// Remove duplicates
sources = _.uniq(sources);
// Add module settings to fmd definition
modules[name] = [sources, opts];
}, this);
// Build packege modules
fmd(config)
.define(modules)
.build(function (createdFiles) {
done();
});
});
};