UNPKG

4.74 kBJavaScriptView Raw
1/**
2 * Naming bundles
3 *
4 * for naming rules see .md file
5 */
6
7var crypto = require("crypto");
8var npmUtils = require("steal/ext/npm-utils");
9var isNpm = npmUtils.moduleName.isNpm;
10var parseModuleName = npmUtils.moduleName.parse;
11
12/**
13 * remove the filetype from a
14 *
15 * @param modulePath
16 * @returns {*}
17 */
18var removeFiletype = function(modulePath) {
19 if(modulePath.lastIndexOf('.') !== -1) {
20 modulePath = modulePath.substr(0, modulePath.lastIndexOf('.'));
21 }
22 return modulePath;
23};
24
25/**
26 * get the last part of a module path
27 * if a npm package is provided
28 * e.g.
29 * main/bar/foo => foo
30 * packagename@1.0.0#foo/bar => bar
31 *
32 * @param uri
33 * @returns {string}
34 */
35var 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 * return the plugin part
52 *
53 * @param name
54 * @returns {string}
55 */
56var pluginPart = function (name) {
57 var bang = name.lastIndexOf("!");
58 if (bang !== -1) {
59 return name.substr(bang + 1);
60 }
61};
62
63/**
64 * return the plugin resource
65 *
66 * @param name
67 * @returns {string}
68 */
69var pluginResource = function (name) {
70 var bang = name.lastIndexOf("!");
71 if (bang !== -1) {
72 return name.substr(0, bang);
73 }
74};
75
76/**
77 *
78 * @param bundle
79 * @returns {string}
80 */
81function 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 * get a unique name, based on bundleCounter
95 *
96 * @param dirName
97 * @param shortened
98 * @param buildTypeSuffix
99 * @returns {string}
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 * get the name of a bundle
111 *
112 * @param bundle
113 * @returns {string}
114 */
115 function getName(bundle) {
116
117 /**
118 * add a prototype method to String
119 *
120 * @param whatever
121 * @returns {string}
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 // remove trailing and leading parts
138 var bundleNames = bundle.bundles.map(function(appName){
139 appName = appName.trim().removeTrailing('/');
140 return appName;
141 });
142
143 // If this is a "main" bundle
144 if (bundle.bundles.length === 1) {
145 // main module
146 shortened = bundleNames[0];
147
148 if(pluginPart(shortened)) {
149 shortened = pluginResource(shortened);
150 }
151
152 // for the shortened we only want the path of the module
153 if(isNpm(shortened)) {
154 var parsed = parseModuleName(shortened);
155 shortened = removeFiletype(parsed.modulePath);
156 dirName += parsed.packageName + "/";
157
158 // no npm module !
159 }else{
160 shortened = removeFiletype(shortened);
161 }
162
163 // if multiple bundles and very long concatenated name
164 } else if (bundle.bundles.length > 1) {
165
166 // concat multiple bundles into one shortened filename,
167 // for that use only the last part of each modulepath
168 shortened = bundleNames.map(function(l){
169 return filename(l);
170 }).join('-');
171
172 // we dont care about naming rules
173 // we add the bundles into the bundles config
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 // delete the String prototype method
189 delete String.prototype.removeTrailing;
190
191
192 if(bundle.bundles.length === 1) {
193 // we dont want do rewrite a main module
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
207exports = module.exports = function(bundles) {
208
209 var getName = makeGetName();
210
211 bundles.forEach(function(bundle){
212 bundle.name = getName(bundle);
213 });
214
215};
216
217exports.getName = function(bundle) {
218 return makeGetName(bundle);
219};