-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
198 lines (158 loc) · 5.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
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
var copy = require('shallow-copy');
var glob = require('glob');
var uniq = require('nub');
var path = require('path');
var shasum = require('shasum');
var fs = require('fs');
var EventEmitter = require( 'events' ).EventEmitter;
var mothership = require( 'mothership' );
var _ = require( 'underscore' );
var through = require( 'through2' );
module.exports = function( browserifyInstance, opts ) {
var eventEmitter = new EventEmitter();
var keypaths = opts.keys || opts.key || opts.k;
if (!keypaths) keypaths = [];
if (!Array.isArray(keypaths)) keypaths = [ keypaths ];
var defaults = opts.defaults || opts.d || {};
var assets = opts.assets || {};
var packages = {};
var oldPackages = opts.packages;
var oldPackageDependencies = opts.dependencies;
var dependencies = {};
var mainFile;
browserifyInstance.pipeline.get( 'label' ).unshift( through.obj( function( row, enc, next ) {
var thisFilePath = row.file;
var thisFileDependencies = _.values( row.deps || {} );
this.push( row );
if( fs.lstatSync( thisFilePath ).isDirectory() ) {
var err = new Error( 'Parcel map can not operate on directories. Please specify the full entry point path when running browserify.' );
eventEmitter.emit( 'error', err );
}
if( thisFileDependencies.length ) {
if( ! dependencies[ thisFilePath ] ) dependencies[ thisFilePath ] = [];
dependencies[ thisFilePath ] = dependencies[ thisFilePath ].concat( thisFileDependencies );
}
mothership( thisFilePath, function() { return true; }, function( err, res ) {
if( err ) {
eventEmitter.emit( 'error', err );
}
var pkg, dir;
// the file has no mothership package.json. eventhough it is not usually relevant for
// the purposes of parcelify, since parcelify does not care about orphaned
// js files without any other asset types, we still have to include it in the
// map. it might be an entry point and therefore cartero needs an ide for it.
if( ! res ) {
pkg = {};
dir = path.dirname( thisFilePath );
} else {
pkg = res.pack;
dir = path.dirname( res.path );
}
assets[ thisFilePath ] = dir;
// if we've already registered this package, don't do it again (avoid cycles), but
// do make sure we mark parcels as such, since they can contain both entry point
// and non-entry point js files.
if( packages[ dir ] ) {
if( row.entry ) {
packages[ dir ].__isParcel = true;
packages[ dir ].__mainPath = thisFilePath;
}
return next();
}
if( typeof browserifyInstance._options.packageFilter === 'function' ) pkg = browserifyInstance._options.packageFilter( pkg, dir );
pkg.__path = dir;
pkg.__isParcel = !! row.entry;
if( pkg.__isParcel ) pkg.__mainPath = thisFilePath;
packages[ dir ] = pkg;
var globs = getKeys( keypaths, defaults, copy( pkg ) );
if( typeof globs === 'string' ) globs = [ globs ];
if( ! globs ) globs = [];
_.each( globs, function( thisGlob ) {
var thisGlobAbsPath = path.resolve( dir, thisGlob );
glob.sync( thisGlobAbsPath ).forEach( function( thisAssetPath ) {
assets[ thisAssetPath ] = dir;
} );
} );
next();
} );
} ) );
browserifyInstance.pipeline.get( 'label' ).on( 'end', finish );
function finish() {
packages = _.extend( {}, oldPackages, packages );
var pkgdeps = Object.keys(dependencies).reduce( function( acc, file ) {
var deps = dependencies[file];
var dir = assets[file];
var pkgid = dir;
if( ! acc[pkgid] ) acc[pkgid] = [];
acc[pkgid] = _.unique( acc[pkgid].concat( deps
.map(function (id) {
if (pkgid === assets[id]) return null;
var did = assets[id];
//if (pkgCount[did] === 0) return false;
return did;
})
.filter(Boolean) ).sort() );
return acc;
}, {});
pkgdeps = _.extend( {}, oldPackageDependencies, pkgdeps );
var pkgids = {};
var walked = {};
var getPkgId = (function () {
return function get (dir) {
if (pkgids[dir]) return pkgids[dir];
walked[dir] = true;
var deps = (pkgdeps[dir] || [])
.filter(function (x) { return !walked[x] || pkgids[x]})
.map(get)
.sort()
;
pkgids[dir] = shasum(dir + '!' + deps.join(','));
return pkgids[dir];
}
})();
var result = {
packages: Object.keys(packages).sort().reduce(function (acc, dir) {
// we used to get rid of packages that dont have assets or directly
// but we want to know about them if they ahve indirect dependencies.
// just keep all packages around for now, see where it gets us.
// if (pkgCount[dir] === 0 && !packages[dir].view) {
// return acc;
// }
var pkgid = getPkgId(dir);
acc[pkgid] = packages[dir];
return acc;
}, {}),
assets: Object.keys(assets).reduce(function (acc, file) {
acc[file] = getPkgId(assets[file]);
return acc;
}, {}),
dependencies: Object.keys(pkgdeps).reduce(function (acc, dir) {
var pkgid = getPkgId(dir);
acc[pkgid] = pkgdeps[dir].map(getPkgId);
return acc;
}, {})
};
eventEmitter.emit( 'done', result );
var outfile = opts.o || opts.outfile;
if (outfile) fs.writeFile(outfile, JSON.stringify(result, null, 2));
};
return eventEmitter;
};
function getKeys (keys, defaults, pkg) {
return uniq(keys.concat(Object.keys(defaults))).map(function (key) {
var cur = pkg, curDef = defaults;
if (typeof key === 'string' && /\./.test(key)) {
key = key.split('.');
}
if (Array.isArray(key)) {
for (var i = 0; i < key.length - 1; i++) {
cur = cur && cur[key[i]];
curDef = curDef && curDef[key[i]];
}
key = key[i];
}
return (cur && cur[key]) || (curDef && curDef[key]);
})
.reduce(function (acc, g) { return acc.concat(g) }, [])
.filter(Boolean);
}