1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var crypto = require("crypto");
|
8 | var npmUtils = require("steal/ext/npm-utils");
|
9 | var isNpm = npmUtils.moduleName.isNpm;
|
10 | var parseModuleName = npmUtils.moduleName.parse;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var removeFiletype = function(modulePath) {
|
19 | if(modulePath.lastIndexOf('.') !== -1) {
|
20 | modulePath = modulePath.substr(0, modulePath.lastIndexOf('.'));
|
21 | }
|
22 | return modulePath;
|
23 | };
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var filename = function (uri) {
|
36 | var lastSlash = uri.lastIndexOf("/");
|
37 | var sub;
|
38 | if (lastSlash == -1) {
|
39 | sub = uri;
|
40 | } else {
|
41 | sub = uri.substr(lastSlash + 1);
|
42 | }
|
43 | sub = sub.toLowerCase();
|
44 | var parsed = parseModuleName(sub);
|
45 | var path = removeFiletype(parsed.modulePath || sub);
|
46 | var matches = path.match(/^[\w-\s\.]+/);
|
47 | return matches ? matches[0] : "";
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | var pluginPart = function (name) {
|
57 | var bang = name.lastIndexOf("!");
|
58 | if (bang !== -1) {
|
59 | return name.substr(bang + 1);
|
60 | }
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | var pluginResource = function (name) {
|
70 | var bang = name.lastIndexOf("!");
|
71 | if (bang !== -1) {
|
72 | return name.substr(0, bang);
|
73 | }
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function makeGetName() {
|
82 |
|
83 | var bundleCounter = 0;
|
84 | var usedBundleNames = {};
|
85 |
|
86 | if(arguments.length === 1){
|
87 | return getName(arguments[0]);
|
88 | }else{
|
89 | return getName;
|
90 | }
|
91 |
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function getUniqueName(dirName, shortened, buildTypeSuffix) {
|
102 | if (!usedBundleNames[dirName + shortened + buildTypeSuffix]) {
|
103 | return dirName + shortened + buildTypeSuffix + "";
|
104 | }else {
|
105 | return dirName + shortened + "-" + (bundleCounter++) + buildTypeSuffix + "";
|
106 | }
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function getName(bundle) {
|
116 |
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | if(!String.prototype.removeTrailing) {
|
124 | String.prototype.removeTrailing = function (whatever) {
|
125 | var result = this;
|
126 | if (this.substr((-1 * whatever.length)) === whatever) {
|
127 | result = this.substr(0, this.length - (1 * whatever.length));
|
128 | }
|
129 | return result + "";
|
130 | };
|
131 | }
|
132 |
|
133 | var dirName = "bundles/",
|
134 | shortened,
|
135 | bundleName;
|
136 |
|
137 |
|
138 | var bundleNames = bundle.bundles.map(function(appName){
|
139 | appName = appName.trim().removeTrailing('/');
|
140 | return appName;
|
141 | });
|
142 |
|
143 |
|
144 | if (bundle.bundles.length === 1) {
|
145 |
|
146 | shortened = bundleNames[0];
|
147 |
|
148 | if(pluginPart(shortened)) {
|
149 | shortened = pluginResource(shortened);
|
150 | }
|
151 |
|
152 |
|
153 | if(isNpm(shortened)) {
|
154 | var parsed = parseModuleName(shortened);
|
155 | shortened = removeFiletype(parsed.modulePath);
|
156 | dirName += parsed.packageName + "/";
|
157 |
|
158 |
|
159 | }else{
|
160 | shortened = removeFiletype(shortened);
|
161 | }
|
162 |
|
163 |
|
164 | } else if (bundle.bundles.length > 1) {
|
165 |
|
166 |
|
167 |
|
168 | shortened = bundleNames.map(function(l){
|
169 | return filename(l);
|
170 | }).join('-');
|
171 |
|
172 |
|
173 |
|
174 | shortened = shortened.replace(/[^\w\-_]/g, "-").replace(/-{2,}/g, '-');
|
175 |
|
176 | if(shortened.length > 25) {
|
177 | var hasher = crypto.createHash("md5");
|
178 | hasher.update(shortened);
|
179 | var shortenedHash = hasher.digest('hex');
|
180 | shortened = shortened.substr(0, 16) + "-" + shortenedHash.substr(0, 8);
|
181 | }
|
182 |
|
183 | }
|
184 |
|
185 | var buildType = bundle.buildType || "js",
|
186 | buildTypeSuffix = buildType === "js" ? "" : "."+buildType+"!";
|
187 |
|
188 |
|
189 | delete String.prototype.removeTrailing;
|
190 |
|
191 |
|
192 | if(bundle.bundles.length === 1) {
|
193 |
|
194 | bundleName = dirName + shortened + buildTypeSuffix;
|
195 |
|
196 | } else {
|
197 | bundleName = getUniqueName(dirName, shortened, buildTypeSuffix);
|
198 | }
|
199 |
|
200 | usedBundleNames[bundleName] = true;
|
201 |
|
202 | return bundleName;
|
203 | }
|
204 |
|
205 | }
|
206 |
|
207 | exports = module.exports = function(bundles) {
|
208 |
|
209 | var getName = makeGetName();
|
210 |
|
211 | bundles.forEach(function(bundle){
|
212 | bundle.name = getName(bundle);
|
213 | });
|
214 |
|
215 | };
|
216 |
|
217 | exports.getName = function(bundle) {
|
218 | return makeGetName(bundle);
|
219 | };
|