1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TsconfigPathsPlugin = void 0;
|
4 | const chalk = require("chalk");
|
5 | const TsconfigPaths = require("tsconfig-paths");
|
6 | const path = require("path");
|
7 | const Options = require("./options");
|
8 | const Logger = require("./logger");
|
9 | const util_1 = require("util");
|
10 | const getInnerRequest = require("enhanced-resolve/lib/getInnerRequest");
|
11 | class TsconfigPathsPlugin {
|
12 | constructor(rawOptions = {}) {
|
13 | this.source = "described-resolve";
|
14 | this.target = "resolve";
|
15 | const options = Options.getOptions(rawOptions);
|
16 | this.extensions = options.extensions;
|
17 |
|
18 | this.log = Logger.makeLogger(options, new chalk.Instance({ level: options.colors ? undefined : 0 }));
|
19 | const context = options.context || process.cwd();
|
20 | const loadFrom = options.configFile || context;
|
21 | const loadResult = TsconfigPaths.loadConfig(loadFrom);
|
22 | if (loadResult.resultType === "failed") {
|
23 | this.log.logError(`Failed to load ${loadFrom}: ${loadResult.message}`);
|
24 | }
|
25 | else {
|
26 | this.log.logInfo(`tsconfig-paths-webpack-plugin: Using config file at ${loadResult.configFileAbsolutePath}`);
|
27 | this.baseUrl = options.baseUrl || loadResult.baseUrl;
|
28 | this.absoluteBaseUrl = options.baseUrl
|
29 | ? path.resolve(options.baseUrl)
|
30 | : loadResult.absoluteBaseUrl;
|
31 | this.matchPath = TsconfigPaths.createMatchPathAsync(this.absoluteBaseUrl, loadResult.paths, options.mainFields);
|
32 | }
|
33 | }
|
34 | apply(resolver) {
|
35 | if (!resolver) {
|
36 | this.log.logWarning("tsconfig-paths-webpack-plugin: Found no resolver, not applying tsconfig-paths-webpack-plugin");
|
37 | return;
|
38 | }
|
39 | const { baseUrl } = this;
|
40 | if (!baseUrl) {
|
41 |
|
42 | this.log.logWarning("tsconfig-paths-webpack-plugin: Found no baseUrl in tsconfig.json, not applying tsconfig-paths-webpack-plugin");
|
43 | return;
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 | if (!("fileSystem" in resolver)) {
|
49 | this.log.logWarning("tsconfig-paths-webpack-plugin: No file system found on resolver." +
|
50 | " Please make sure you've placed the plugin in the correct part of the configuration." +
|
51 | " This plugin is a resolver plugin and should be placed in the resolve part of the Webpack configuration.");
|
52 | return;
|
53 | }
|
54 |
|
55 | if ("getHook" in resolver && typeof resolver.getHook === "function") {
|
56 | resolver
|
57 | .getHook(this.source)
|
58 | .tapAsync({ name: "TsconfigPathsPlugin" }, createPluginCallback(this.matchPath, resolver, this.absoluteBaseUrl, resolver.getHook(this.target), this.extensions));
|
59 | }
|
60 | else if ("plugin" in resolver) {
|
61 |
|
62 | const legacyResolver = resolver;
|
63 | legacyResolver.plugin(this.source, createPluginLegacy(this.matchPath, resolver, this.absoluteBaseUrl, this.target, this.extensions));
|
64 | }
|
65 | }
|
66 | }
|
67 | exports.TsconfigPathsPlugin = TsconfigPathsPlugin;
|
68 | function createPluginCallback(matchPath, resolver, absoluteBaseUrl, hook, extensions) {
|
69 | const fileExistAsync = createFileExistAsync(resolver.fileSystem);
|
70 | const readJsonAsync = createReadJsonAsync(resolver.fileSystem);
|
71 | return (request, resolveContext, callback) => {
|
72 | const innerRequest = getInnerRequest(resolver, request);
|
73 | if (!innerRequest ||
|
74 | innerRequest.startsWith(".") ||
|
75 | innerRequest.startsWith("..")) {
|
76 | return callback();
|
77 | }
|
78 | matchPath(innerRequest, readJsonAsync, fileExistAsync, extensions, (err, foundMatch) => {
|
79 | if (err) {
|
80 | return callback(err);
|
81 | }
|
82 | if (!foundMatch) {
|
83 | return callback();
|
84 | }
|
85 | const newRequest = Object.assign(Object.assign({}, request), { request: foundMatch, path: absoluteBaseUrl });
|
86 |
|
87 |
|
88 |
|
89 | const createInnerContext = require("enhanced-resolve/lib/createInnerContext");
|
90 | return resolver.doResolve(hook, newRequest, `Resolved request '${innerRequest}' to '${foundMatch}' using tsconfig.json paths mapping`,
|
91 |
|
92 | createInnerContext(Object.assign({}, resolveContext)), (err2, result2) => {
|
93 |
|
94 |
|
95 | if (err2) {
|
96 | return callback(err2);
|
97 | }
|
98 |
|
99 | if (result2 === undefined) {
|
100 | return callback(undefined, undefined);
|
101 | }
|
102 | console.log(`Returning with path ${util_1.inspect({ result2 }, false, 4, true)}`);
|
103 |
|
104 | callback(undefined, result2);
|
105 | });
|
106 | });
|
107 | };
|
108 | }
|
109 | function createPluginLegacy(matchPath, resolver, absoluteBaseUrl, target, extensions) {
|
110 | const fileExistAsync = createFileExistAsync(resolver.fileSystem);
|
111 | const readJsonAsync = createReadJsonAsync(resolver.fileSystem);
|
112 | return (request, callback) => {
|
113 | const innerRequest = getInnerRequest(resolver, request);
|
114 | if (!innerRequest ||
|
115 | innerRequest.startsWith(".") ||
|
116 | innerRequest.startsWith("..")) {
|
117 | return callback();
|
118 | }
|
119 | matchPath(innerRequest, readJsonAsync, fileExistAsync, extensions, (err, foundMatch) => {
|
120 | if (err) {
|
121 | return callback(err);
|
122 | }
|
123 | if (!foundMatch) {
|
124 | return callback();
|
125 | }
|
126 | const newRequest = Object.assign(Object.assign({}, request), { request: foundMatch, path: absoluteBaseUrl });
|
127 |
|
128 |
|
129 |
|
130 | const createInnerCallback = require("enhanced-resolve/lib/createInnerCallback");
|
131 | return resolver.doResolve(target, newRequest, `Resolved request '${innerRequest}' to '${foundMatch}' using tsconfig.json paths mapping`, createInnerCallback(function (err2, result2) {
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | if (arguments.length > 0) {
|
138 | return callback(err2, result2);
|
139 | }
|
140 |
|
141 | callback(undefined, undefined);
|
142 | }, callback));
|
143 | });
|
144 | };
|
145 | }
|
146 | function readJson(fileSystem, path2, callback) {
|
147 | if ("readJson" in fileSystem && fileSystem.readJson) {
|
148 | return fileSystem.readJson(path2, callback);
|
149 | }
|
150 | fileSystem.readFile(path2, (err, buf) => {
|
151 | if (err) {
|
152 | return callback(err);
|
153 | }
|
154 | let data;
|
155 | try {
|
156 |
|
157 | data = JSON.parse(buf.toString("utf-8"));
|
158 | }
|
159 | catch (e) {
|
160 | return callback(e);
|
161 | }
|
162 | return callback(undefined, data);
|
163 | });
|
164 | }
|
165 | function createReadJsonAsync(filesystem) {
|
166 |
|
167 | return (path2, callback2) => {
|
168 | readJson(filesystem, path2, (err, json) => {
|
169 |
|
170 | if (err || !json) {
|
171 | callback2();
|
172 | return;
|
173 | }
|
174 | callback2(undefined, json);
|
175 | });
|
176 | };
|
177 | }
|
178 | function createFileExistAsync(filesystem) {
|
179 | return (path2, callback2) => {
|
180 | filesystem.stat(path2, (err, stats) => {
|
181 |
|
182 | if (err) {
|
183 | callback2(undefined, false);
|
184 | return;
|
185 | }
|
186 | callback2(undefined, stats ? stats.isFile() : false);
|
187 | });
|
188 | };
|
189 | }
|