vite-tsconfig-paths
Advanced tools
Comparing version 4.0.0-alpha.4 to 4.0.0-alpha.5
@@ -49,2 +49,3 @@ "use strict"; | ||
var tsconfck = __toESM(require("tsconfck")); | ||
var import_util = require("util"); | ||
var import_debug = __toESM(require("debug")); | ||
@@ -55,3 +56,3 @@ var fs = __toESM(require("fs")); | ||
var import_path2 = require("path"); | ||
function resolvePathMappings(paths, root) { | ||
function resolvePathMappings(paths, base) { | ||
const sortedPatterns = Object.keys(paths).sort( | ||
@@ -66,3 +67,3 @@ (a, b) => getPrefixLength(b) - getPrefixLength(a) | ||
pattern: new RegExp("^" + pattern + "$"), | ||
paths: relativePaths.map((relativePath) => (0, import_path2.resolve)(root, relativePath)) | ||
paths: relativePaths.map((relativePath) => (0, import_path2.resolve)(base, relativePath)) | ||
}); | ||
@@ -114,8 +115,22 @@ } | ||
} | ||
const parsedProjects = await Promise.all( | ||
projects.map( | ||
(tsconfigFile) => hasTypeScriptDep ? tsconfck.parseNative(tsconfigFile) : tsconfck.parse(tsconfigFile) | ||
const parsedProjects = new Set( | ||
await Promise.all( | ||
projects.map( | ||
(tsconfigFile) => hasTypeScriptDep ? tsconfck.parseNative(tsconfigFile) : tsconfck.parse(tsconfigFile) | ||
) | ||
) | ||
); | ||
resolvers = parsedProjects.map(createResolver).filter(Boolean); | ||
resolvers = []; | ||
parsedProjects.forEach((project) => { | ||
if (project.referenced) { | ||
project.referenced.forEach((projectRef) => { | ||
parsedProjects.add(projectRef); | ||
}); | ||
} else { | ||
const resolver = createResolver(project); | ||
if (resolver) { | ||
resolvers.push(resolver); | ||
} | ||
} | ||
}); | ||
}, | ||
@@ -137,8 +152,9 @@ async resolveId(id, importer) { | ||
}; | ||
function createResolver(parsed) { | ||
const configPath = parsed.tsconfigFile; | ||
function createResolver(project) { | ||
var _a; | ||
const configPath = project.tsconfigFile; | ||
const configDir = (0, import_path.dirname)(configPath); | ||
const config = parsed.tsconfig; | ||
const config = project.tsconfig; | ||
const options = config.compilerOptions; | ||
debug("config loaded:", parsed); | ||
debug("config loaded:", (0, import_util.inspect)({ configPath, config }, false, 10, true)); | ||
const { baseUrl, paths } = options; | ||
@@ -152,3 +168,6 @@ if (!baseUrl && !paths) { | ||
if (paths) { | ||
const pathMappings = resolvePathMappings(paths, (0, import_path.dirname)(configPath)); | ||
const pathMappings = resolvePathMappings( | ||
paths, | ||
(_a = options.baseUrl) != null ? _a : (0, import_path.dirname)(configPath) | ||
); | ||
const resolveWithPaths = async (viteResolve, id, importer) => { | ||
@@ -195,3 +214,3 @@ for (const mapping of pathMappings) { | ||
return async (viteResolve, id, importer) => { | ||
var _a; | ||
var _a2; | ||
if (id.includes("\0")) { | ||
@@ -209,3 +228,3 @@ return; | ||
} | ||
const suffix = (_a = /\?.+$/.exec(id)) == null ? void 0 : _a[0]; | ||
const suffix = (_a2 = /\?.+$/.exec(id)) == null ? void 0 : _a2[0]; | ||
if (suffix) { | ||
@@ -212,0 +231,0 @@ id = id.slice(0, -suffix.length); |
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "4.0.0-alpha.4", | ||
"version": "4.0.0-alpha.5", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,2 +6,3 @@ import { basename, dirname, isAbsolute, join, relative } from './path' | ||
import * as tsconfck from 'tsconfck' | ||
import { inspect } from 'util' | ||
import { PluginOptions } from './types' | ||
@@ -62,13 +63,27 @@ | ||
const parsedProjects = await Promise.all( | ||
projects.map((tsconfigFile) => | ||
hasTypeScriptDep | ||
? tsconfck.parseNative(tsconfigFile) | ||
: tsconfck.parse(tsconfigFile) | ||
const parsedProjects = new Set( | ||
await Promise.all( | ||
projects.map((tsconfigFile) => | ||
hasTypeScriptDep | ||
? tsconfck.parseNative(tsconfigFile) | ||
: tsconfck.parse(tsconfigFile) | ||
) | ||
) | ||
) | ||
resolvers = parsedProjects | ||
.map(createResolver) | ||
.filter(Boolean) as Resolver[] | ||
resolvers = [] | ||
parsedProjects.forEach((project) => { | ||
// Don't create a resolver for projects with a references array. | ||
// Instead, create a resolver for each project in that array. | ||
if (project.referenced) { | ||
project.referenced.forEach((projectRef) => { | ||
parsedProjects.add(projectRef) | ||
}) | ||
} else { | ||
const resolver = createResolver(project) | ||
if (resolver) { | ||
resolvers.push(resolver) | ||
} | ||
} | ||
}) | ||
}, | ||
@@ -91,7 +106,7 @@ async resolveId(id, importer) { | ||
function createResolver( | ||
parsed: tsconfck.TSConfckParseResult | ||
project: tsconfck.TSConfckParseResult | ||
): Resolver | null { | ||
const configPath = parsed.tsconfigFile | ||
const configPath = project.tsconfigFile | ||
const configDir = dirname(configPath) | ||
const config = parsed.tsconfig as { | ||
const config = project.tsconfig as { | ||
include?: string[] | ||
@@ -102,3 +117,3 @@ exclude?: string[] | ||
const options = config.compilerOptions | ||
debug('config loaded:', parsed) | ||
debug('config loaded:', inspect({ configPath, config }, false, 10, true)) | ||
@@ -117,3 +132,6 @@ const { baseUrl, paths } = options | ||
if (paths) { | ||
const pathMappings = resolvePathMappings(paths, dirname(configPath)) | ||
const pathMappings = resolvePathMappings( | ||
paths, | ||
options.baseUrl ?? dirname(configPath) | ||
) | ||
const resolveWithPaths: Resolver = async (viteResolve, id, importer) => { | ||
@@ -120,0 +138,0 @@ for (const mapping of pathMappings) { |
@@ -10,3 +10,3 @@ import { resolve } from 'path' | ||
paths: Record<string, string[]>, | ||
root: string | ||
base: string | ||
) { | ||
@@ -24,3 +24,3 @@ // If a module name can be matched with multiple patterns then pattern | ||
pattern: new RegExp('^' + pattern + '$'), | ||
paths: relativePaths.map((relativePath) => resolve(root, relativePath)), | ||
paths: relativePaths.map((relativePath) => resolve(base, relativePath)), | ||
}) | ||
@@ -27,0 +27,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74522
999