-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (35 loc) · 1.42 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
'use strict';
const globParent = require('glob-parent');
const matched = require('matched').promise;
const getOptions = require('loader-utils').getOptions;
const mapImports = d => `import ${JSON.stringify(d)};`;
const mapExports = d => `export * from ${JSON.stringify(d)};`;
function multiEntryLoader(content, map) {
const done = this.async();
if (content !== null) {
return done(null, content, map);
}
const options = Object.assign({
include: [],
exclude: [],
exports: false
}, getOptions(this));
const include = Array.isArray(options.include) ? options.include : [options.include];
const exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude];
const patterns = include.concat(exclude.map(pattern => '!' + pattern));
const mapPaths = options.exports ? mapExports : mapImports;
const combinePaths = paths => paths.map(mapPaths).join('\n');
if (!include.length) {
throw Error('include option should be specified');
}
const parents = include.reduce((list, pattern) => {
const parent = globParent(pattern);
if (list.indexOf(parent) === -1) {
list.push(parent);
}
return list;
}, []);
parents.forEach(parent => this.addContextDependency(parent));
return matched(patterns, { realpath: true }).then(combinePaths).then(code => done(null, code));
}
module.exports = multiEntryLoader;