1 | "use strict";
|
2 | var instance_1 = require("./instance");
|
3 | var path = require("path");
|
4 | var _ = require("lodash");
|
5 | var ModulesInRootPlugin = require('enhanced-resolve/lib/ModulesInRootPlugin');
|
6 | var createInnerCallback = require('enhanced-resolve/lib/createInnerCallback');
|
7 | var getInnerRequest = require('enhanced-resolve/lib/getInnerRequest');
|
8 | function escapeRegExp(str) {
|
9 | return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
10 | }
|
11 | var PathPlugin = (function () {
|
12 | function PathPlugin(config) {
|
13 | if (config === void 0) { config = {}; }
|
14 | var _this = this;
|
15 | this.source = 'described-resolve';
|
16 | this.target = 'resolve';
|
17 | this.ts = instance_1.setupTs(config.compiler).tsImpl;
|
18 | var context = config.context || process.cwd();
|
19 | var _a = instance_1.readConfigFile(context, config, {}, this.ts), configFilePath = _a.configFilePath, compilerConfig = _a.compilerConfig;
|
20 | this.options = compilerConfig.options;
|
21 | this.configFilePath = configFilePath;
|
22 | this.baseUrl = this.options.baseUrl;
|
23 | this.absoluteBaseUrl = path.resolve(path.dirname(this.configFilePath), this.baseUrl || '.');
|
24 | this.mappings = [];
|
25 | var paths = this.options.paths || {};
|
26 | Object.keys(paths).forEach(function (alias) {
|
27 | var onlyModule = alias.indexOf('*') === -1;
|
28 | var excapedAlias = escapeRegExp(alias);
|
29 | var targets = paths[alias];
|
30 | targets.forEach(function (target) {
|
31 | var aliasPattern;
|
32 | if (onlyModule) {
|
33 | aliasPattern = new RegExp("^" + excapedAlias + "$");
|
34 | }
|
35 | else {
|
36 | var withStarCapturing = excapedAlias.replace('\\*', '(.*)');
|
37 | aliasPattern = new RegExp("^" + withStarCapturing);
|
38 | }
|
39 | _this.mappings.push({
|
40 | onlyModule: onlyModule,
|
41 | alias: alias,
|
42 | aliasPattern: aliasPattern,
|
43 | target: target
|
44 | });
|
45 | });
|
46 | });
|
47 | }
|
48 | PathPlugin.prototype.apply = function (resolver) {
|
49 | var _this = this;
|
50 | var _a = this, baseUrl = _a.baseUrl, mappings = _a.mappings;
|
51 | if (baseUrl) {
|
52 | resolver.apply(new ModulesInRootPlugin("module", this.absoluteBaseUrl, "resolve"));
|
53 | }
|
54 | mappings.forEach(function (mapping) {
|
55 | resolver.plugin(_this.source, _this.createPlugin(resolver, mapping));
|
56 | });
|
57 | };
|
58 | PathPlugin.prototype.createPlugin = function (resolver, mapping) {
|
59 | var _this = this;
|
60 | return function (request, callback) {
|
61 | var innerRequest = getInnerRequest(resolver, request);
|
62 | if (!innerRequest) {
|
63 | return callback();
|
64 | }
|
65 | var match = innerRequest.match(mapping.aliasPattern);
|
66 | if (!match) {
|
67 | return callback();
|
68 | }
|
69 | var newRequestStr = mapping.target;
|
70 | if (!mapping.onlyModule) {
|
71 | newRequestStr = newRequestStr.replace('*', match[1]);
|
72 | }
|
73 | if (newRequestStr[0] === '.') {
|
74 | newRequestStr = path.resolve(_this.absoluteBaseUrl, newRequestStr);
|
75 | }
|
76 | var newRequest = _.extend({}, request, {
|
77 | request: newRequestStr
|
78 | });
|
79 | return resolver.doResolve(_this.target, newRequest, "aliased with mapping '" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'", createInnerCallback(function (err, result) {
|
80 | if (arguments.length > 0) {
|
81 | return callback(err, result);
|
82 | }
|
83 | callback(null, null);
|
84 | }, callback));
|
85 | };
|
86 | };
|
87 | return PathPlugin;
|
88 | }());
|
89 | exports.PathPlugin = PathPlugin;
|
90 |
|
\ | No newline at end of file |