-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
101 lines (85 loc) · 3.47 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
var path = require('path');
var assign = require('object-assign');
var forEachBail = require('enhanced-resolve/lib/forEachBail');
var basename = require('enhanced-resolve/lib/getPaths').basename;
module.exports = function (options) {
var optionsToUse = (typeof options === 'boolean') ? { honorIndex: options } : (options || {});
var mainFields = optionsToUse.honorPackage,
exclude = optionsToUse.exclude,
include = optionsToUse.include;
optionsToUse.mainFields = mainFields !== false && !Array.isArray(mainFields) ? ["main"] : mainFields;
// make exclude array if not
optionsToUse.exclude = exclude && !Array.isArray(exclude) ? [exclude] : exclude;
// make include array if not
optionsToUse.include = include && !Array.isArray(include) ? [include] : include;
return {
apply: doApply.bind(this, optionsToUse)
};
};
function stringIncludes(string, maybeString) {
// String.includes throws if the argument is not a string
return typeof maybeString === 'string' ? string.includes(maybeString) : false;
}
function doApply(options, resolver) {
// file type taken from: https://github.com/webpack/enhanced-resolve/blob/v4.0.0/test/plugins.js
var target = resolver.ensureHook("undescribed-raw-file");
resolver.getHook(options.resolverHook || "before-existing-directory")
.tapAsync("DirectoryNamedWebpackPlugin", (request, resolveContext, callback) => {
if (options.ignoreFn && options.ignoreFn(request)) {
return callback();
}
var dirPath = request.path;
var dirName = basename(dirPath);
var attempts = [];
// return if path matches with excludes
if (options.exclude && options.exclude.some(function(exclude) {
return dirPath.search(exclude) >= 0 || stringIncludes(dirPath, exclude);
})) {
return callback();
}
// return if path doesn't match with includes
if (options.include && !options.include.some(function(include) {
return dirPath.search(include) >= 0 || stringIncludes(dirPath, include);
})) {
return callback();
}
if (options.mainFields) {
try {
var pkg = require(path.resolve(dirPath, "package.json"));
options.mainFields.forEach(function(field) {
pkg[field] && attempts.push(pkg[field]);
});
} catch (e) {
// No problem, this is optional.
}
}
if (options.honorIndex) {
attempts.push('index');
}
if (options.transformFn) {
var transformResult = options.transformFn(dirName, dirPath, request);
if (!Array.isArray(transformResult)) {
transformResult = [transformResult];
}
transformResult = transformResult.filter(function (attemptName) {
return typeof attemptName === 'string' && attemptName.length > 0;
});
attempts = attempts.concat(transformResult);
} else {
attempts.push(dirName);
}
forEachBail(
attempts,
function (fileName, innerCallback) {
// approach taken from: https://github.com/webpack/enhanced-resolve/blob/v4.0.0/lib/CloneBasenamePlugin.js
var filePath = resolver.join(dirPath, fileName);
var obj = assign({}, request, {
path: filePath,
relativePath: request.relativePath && resolver.join(request.relativePath, fileName)
});
resolver.doResolve(target, obj, "using path: " + filePath, resolveContext, innerCallback);
},
callback
);
});
}