forked from TypeStrong/ts-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.ts
59 lines (51 loc) · 2.7 KB
/
resolver.ts
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
// This file serves as a hacky workaround for the lack of "resolveSync" support in webpack.
// We make our own resolver using a sync file system but using the same plugins & options
// that webpack does.
///<reference path="typings/node/node.d.ts" />
var Resolver = require("enhanced-resolve/lib/Resolver");
var SyncNodeJsInputFileSystem = require("enhanced-resolve/lib/SyncNodeJsInputFileSystem");
var CachedInputFileSystem = require("enhanced-resolve/lib/CachedInputFileSystem");
var UnsafeCachePlugin = require("enhanced-resolve/lib/UnsafeCachePlugin");
var ModulesInDirectoriesPlugin = require("enhanced-resolve/lib/ModulesInDirectoriesPlugin");
var ModulesInRootPlugin = require("enhanced-resolve/lib/ModulesInRootPlugin");
var ModuleAsFilePlugin = require("enhanced-resolve/lib/ModuleAsFilePlugin");
var ModuleAsDirectoryPlugin = require("enhanced-resolve/lib/ModuleAsDirectoryPlugin");
var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
var DirectoryDefaultFilePlugin = require("enhanced-resolve/lib/DirectoryDefaultFilePlugin");
var DirectoryDescriptionFilePlugin = require("enhanced-resolve/lib/DirectoryDescriptionFilePlugin");
var DirectoryDescriptionFileFieldAliasPlugin = require("enhanced-resolve/lib/DirectoryDescriptionFileFieldAliasPlugin");
var FileAppendPlugin = require("enhanced-resolve/lib/FileAppendPlugin");
var ResultSymlinkPlugin = require("enhanced-resolve/lib/ResultSymlinkPlugin");
function makeRootPlugin(name, root) {
if(typeof root === "string")
return new ModulesInRootPlugin(name, root);
else if(Array.isArray(root)) {
return function() {
root.forEach(function(root) {
this.apply(new ModulesInRootPlugin(name, root));
}, this);
};
}
return function() {};
}
function makeResolver(options) {
let fileSystem = new CachedInputFileSystem(new SyncNodeJsInputFileSystem(), 60000);
let resolver = new Resolver(fileSystem);
// apply the same plugins that webpack does, see webpack/lib/WebpackOptionsApply.js
resolver.apply(
new UnsafeCachePlugin(options.resolve.unsafeCache),
options.resolve.packageAlias ? new DirectoryDescriptionFileFieldAliasPlugin("package.json", options.resolve.packageAlias) : function() {},
new ModuleAliasPlugin(options.resolve.alias),
makeRootPlugin("module", options.resolve.root),
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
makeRootPlugin("module", options.resolve.fallback),
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
new DirectoryDescriptionFilePlugin("package.json", options.resolve.packageMains),
new DirectoryDefaultFilePlugin(["index"]),
new FileAppendPlugin(options.resolve.extensions),
new ResultSymlinkPlugin()
);
return resolver;
}
export = makeResolver;