UNPKG

7.73 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/path.ts
2
3
4var _os = require('os');
5
6
7
8var _path = require('path');
9var _vite = require('vite');
10
11var isWindows = _os.platform.call(void 0, ) == "win32";
12var resolve = isWindows ? (...paths) => _vite.normalizePath.call(void 0, _path.win32.resolve(...paths)) : _path.posix.resolve;
13var isAbsolute = isWindows ? _path.win32.isAbsolute : _path.posix.isAbsolute;
14var join = _path.posix.join;
15var relative = _path.posix.relative;
16var basename = _path.posix.basename;
17
18// src/index.ts
19
20var _tsconfigpaths = require('tsconfig-paths');
21var _recrawlsync = require('recrawl-sync');
22var _globrex = require('globrex'); var _globrex2 = _interopRequireDefault(_globrex);
23
24// src/config.ts
25
26
27
28var _tsconfigloaderjs = require('tsconfig-paths/lib/tsconfig-loader.js');
29
30
31var _fs = require('fs');
32function loadConfig(cwd) {
33 const configPath = resolveConfigPath(cwd);
34 if (configPath) {
35 const config = _tsconfigloaderjs.loadTsconfig.call(void 0, configPath);
36 const {
37 compilerOptions: { allowJs, checkJs, baseUrl, paths, outDir } = {}
38 } = config;
39 return {
40 configPath: _vite.normalizePath.call(void 0, configPath),
41 include: config.include,
42 exclude: config.exclude,
43 allowJs: allowJs || checkJs,
44 baseUrl: baseUrl && _vite.normalizePath.call(void 0, _path.resolve.call(void 0, configPath, "..", baseUrl)),
45 paths,
46 outDir
47 };
48 }
49}
50function resolveConfigPath(cwd) {
51 if (_fs.statSync.call(void 0, cwd).isFile()) {
52 return cwd;
53 }
54 const configPath = _tsconfigloaderjs.walkForTsConfig.call(void 0, cwd);
55 if (configPath) {
56 return configPath;
57 }
58}
59
60// src/index.ts
61var _debug2 = require('debug'); var _debug3 = _interopRequireDefault(_debug2);
62var debug = _debug3.default.call(void 0, "vite-tsconfig-paths");
63var src_default = (opts = {}) => {
64 let resolvers;
65 return {
66 name: "vite-tsconfig-paths",
67 enforce: "pre",
68 configResolved(config) {
69 const projects = findProjects(config.root, opts);
70 const extensions = getFileExtensions(opts.extensions);
71 debug("options:", { projects, extensions });
72 resolvers = projects.map((project) => createResolver(project, extensions)).filter(Boolean);
73 },
74 async resolveId(id, importer) {
75 if (importer && !relativeImportRE.test(id) && !isAbsolute(id)) {
76 const viteResolve = async (id2, importer2) => {
77 var _a;
78 return (_a = await this.resolve(id2, importer2, { skipSelf: true })) == null ? void 0 : _a.id;
79 };
80 for (const resolve3 of resolvers) {
81 const resolved = await resolve3(viteResolve, id, importer);
82 if (resolved) {
83 return resolved;
84 }
85 }
86 }
87 }
88 };
89 function createResolver(root, extensions) {
90 const configPath = root.endsWith(".json") ? root : null;
91 if (configPath)
92 root = _path.dirname.call(void 0, root);
93 root += "/";
94 const config = loadConfig(configPath || root);
95 if (!config) {
96 debug(`[!] config not found: "${configPath || root}"`);
97 return null;
98 }
99 const { baseUrl, paths } = config;
100 if (!baseUrl && !paths) {
101 debug(`[!] missing baseUrl and paths: "${config.configPath}"`);
102 return null;
103 }
104 debug("config loaded:", config);
105 const resolveWithBaseUrl = baseUrl ? (viteResolve, id, importer) => viteResolve(join(baseUrl, id), importer) : void 0;
106 let resolveId;
107 if (paths) {
108 const matchPath = _tsconfigpaths.createMatchPathAsync.call(void 0, baseUrl != null ? baseUrl : root, paths, mainFields);
109 const resolveWithPaths = (viteResolve, id, importer) => new Promise((done) => {
110 matchPath(id, void 0, void 0, extensions, (error, path2) => {
111 if (path2) {
112 path2 = _vite.normalizePath.call(void 0, path2);
113 done(viteResolve(path2, importer));
114 } else {
115 error && debug(error.message);
116 done(void 0);
117 }
118 });
119 });
120 if (resolveWithBaseUrl) {
121 resolveId = (viteResolve, id, importer) => resolveWithPaths(viteResolve, id, importer).then((resolved2) => {
122 return resolved2 != null ? resolved2 : resolveWithBaseUrl(viteResolve, id, importer);
123 });
124 } else {
125 resolveId = resolveWithPaths;
126 }
127 } else {
128 resolveId = resolveWithBaseUrl;
129 }
130 const isIncluded = getIncluder(config);
131 let importerExtRE = /./;
132 if (!opts.loose) {
133 importerExtRE = config.allowJs || basename(config.configPath) === "jsconfig.json" ? /\.(vue|svelte|mdx|[mc]?[jt]sx?)$/ : /\.[mc]?tsx?$/;
134 }
135 const resolved = new Map();
136 return async (viteResolve, id, importer) => {
137 var _a;
138 if (id.includes("\0"))
139 return;
140 importer = _vite.normalizePath.call(void 0, importer);
141 const importerFile = importer.replace(/[#?].+$/, "");
142 if (!importerExtRE.test(importerFile))
143 return;
144 if (!isIncluded(relative(root, importerFile)))
145 return;
146 const suffix = (_a = /\?.+$/.exec(id)) == null ? void 0 : _a[0];
147 if (suffix) {
148 id = id.slice(0, -suffix.length);
149 }
150 let path2 = resolved.get(id);
151 if (!path2) {
152 path2 = await resolveId(viteResolve, id, importer);
153 if (path2) {
154 resolved.set(id, path2);
155 debug(`resolved:`, {
156 id,
157 importer,
158 resolvedId: path2,
159 configPath: config.configPath
160 });
161 }
162 }
163 return path2 && suffix ? path2 + suffix : path2;
164 };
165 }
166};
167var relativeImportRE = /^\.\.?(\/|$)/;
168var mainFields = ["module", "jsnext", "jsnext:main", "browser", "main"];
169var defaultInclude = ["**/*"];
170var defaultExclude = ["node_modules", "bower_components", "jspm_packages"];
171function compileGlob(glob) {
172 if (!relativeImportRE.test(glob)) {
173 glob = "./" + glob;
174 }
175 if (!glob.split("/").pop().includes("*")) {
176 glob += "/**";
177 }
178 return _globrex2.default.call(void 0, glob, {
179 extended: true,
180 globstar: true
181 }).regex;
182}
183function getIncluder({
184 include = defaultInclude,
185 exclude = defaultExclude,
186 outDir
187}) {
188 if (outDir) {
189 exclude = exclude.concat(outDir);
190 }
191 if (include.length || exclude.length) {
192 const included = include.map(compileGlob);
193 const excluded = exclude.map(compileGlob);
194 debug(`compiled globs:`, { included, excluded });
195 return (path2) => {
196 path2 = path2.replace(/\?.+$/, "");
197 if (!relativeImportRE.test(path2)) {
198 path2 = "./" + path2;
199 }
200 const test = (glob) => glob.test(path2);
201 return included.some(test) && !excluded.some(test);
202 };
203 }
204 return () => true;
205}
206function findProjects(viteRoot, opts) {
207 const root = opts.root ? resolve(viteRoot, _vite.normalizePath.call(void 0, opts.root)) : viteRoot;
208 let { projects } = opts;
209 if (!projects) {
210 debug(`crawling "${root}"`);
211 projects = _recrawlsync.crawl.call(void 0, root, {
212 only: ["jsconfig.json", "tsconfig.json"],
213 skip: ["node_modules", ".git"]
214 });
215 }
216 const depthMap = {};
217 projects = projects.map((projectPath) => {
218 projectPath = resolve(root, _vite.normalizePath.call(void 0, projectPath));
219 depthMap[projectPath] = projectPath.split("/").length - (projectPath.endsWith(".json") ? 1 : 0);
220 return projectPath;
221 });
222 return projects.sort((a, b) => depthMap[b] - depthMap[a]);
223}
224function getFileExtensions(exts) {
225 const requiredExts = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
226 return exts ? exts.concat(requiredExts) : requiredExts;
227}
228
229
230exports.default = src_default;
231//# sourceMappingURL=index.js.map