vite-tsconfig-paths
Advanced tools
Comparing version 4.0.0-alpha.3 to 4.0.0-alpha.4
@@ -7,11 +7,18 @@ import { Plugin } from 'vite'; | ||
* | ||
* @default viteConfig.root | ||
* When the `projects` option is set, crawling is skipped, so this is | ||
* used to resolve `projects` paths into absolute paths instead. | ||
* | ||
* @default | ||
* // The config root is used for resolving project paths, and the | ||
* // workspace root is used for crawling. | ||
* options.projects ? viteConfig.root : | ||
* vite.searchForWorkspaceRoot(viteConfig.root) | ||
*/ | ||
root?: string; | ||
/** | ||
* An array of `tsconfig.json` paths (relative to `viteConfig.root`) | ||
* and/or directories that contain a `tsconfig.json` file. | ||
* An array of `.json` files and/or directories that contain a | ||
* `tsconfig.json` file. These JSON files must adhere to the | ||
* {@link https://www.typescriptlang.org/tsconfig TypeScript config interface}. The `extends` property is supported. | ||
* | ||
* When undefined, we crawl the project for `tsconfig.json` files. | ||
* You can set the `root` option to control where crawling starts. | ||
* This disables crawling for `tsconfig.json` files when defined. | ||
*/ | ||
@@ -18,0 +25,0 @@ projects?: string[]; |
@@ -86,11 +86,18 @@ "use strict"; | ||
async configResolved(config) { | ||
const root = opts.root || (0, import_vite2.searchForWorkspaceRoot)(config.root); | ||
const configRoot = opts.root || config.root; | ||
const workspaceRoot = opts.root || (0, import_vite2.searchForWorkspaceRoot)(config.root); | ||
debug("roots:", { configRoot, workspaceRoot }); | ||
const projects = await resolveProjectPaths( | ||
opts.projects, | ||
opts.root || config.root | ||
configRoot, | ||
workspaceRoot | ||
); | ||
debug("projects:", projects); | ||
let hasTypeScriptDep = false; | ||
if (opts.parseNative) { | ||
try { | ||
const pkgJson = fs.readFileSync(join(root, "package.json"), "utf8"); | ||
const pkgJson = fs.readFileSync( | ||
join(workspaceRoot, "package.json"), | ||
"utf8" | ||
); | ||
const pkg = JSON.parse(pkgJson); | ||
@@ -105,8 +112,8 @@ const deps = { ...pkg.dependencies, ...pkg.devDependencies }; | ||
} | ||
debug("options:", { root, projects }); | ||
resolvers = (await Promise.all( | ||
const parsedProjects = await Promise.all( | ||
projects.map( | ||
(tsconfigFile) => (hasTypeScriptDep ? tsconfck.parseNative(tsconfigFile) : tsconfck.parse(tsconfigFile)).then((config2) => createResolver(config2)) | ||
(tsconfigFile) => hasTypeScriptDep ? tsconfck.parseNative(tsconfigFile) : tsconfck.parse(tsconfigFile) | ||
) | ||
)).filter(Boolean); | ||
); | ||
resolvers = parsedProjects.map(createResolver).filter(Boolean); | ||
}, | ||
@@ -181,6 +188,3 @@ async resolveId(id, importer) { | ||
); | ||
let importerExtRE = /./; | ||
if (!opts.loose) { | ||
importerExtRE = options.allowJs || basename(configPath) === "jsconfig.json" ? jsLikeRE : /\.[mc]?tsx?$/; | ||
} | ||
const importerExtRE = opts.loose ? /./ : options.allowJs || basename(configPath).startsWith("jsconfig.") ? jsLikeRE : /\.[mc]?tsx?$/; | ||
const resolutionCache = /* @__PURE__ */ new Map(); | ||
@@ -267,3 +271,3 @@ return async (viteResolve, id, importer) => { | ||
} | ||
function resolveProjectPaths(projects, root) { | ||
function resolveProjectPaths(projects, configRoot, workspaceRoot) { | ||
if (projects) { | ||
@@ -274,6 +278,6 @@ return projects.map((file) => { | ||
} | ||
return (0, import_path4.resolve)(root, file); | ||
return (0, import_path4.resolve)(configRoot, file); | ||
}); | ||
} | ||
return tsconfck.findAll(root, { | ||
return tsconfck.findAll(workspaceRoot, { | ||
skip(dir) { | ||
@@ -280,0 +284,0 @@ return dir == "node_modules" || dir == ".git"; |
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "4.0.0-alpha.3", | ||
"version": "4.0.0-alpha.4", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -31,12 +31,22 @@ import { basename, dirname, isAbsolute, join, relative } from './path' | ||
async configResolved(config) { | ||
const root = opts.root || searchForWorkspaceRoot(config.root) | ||
const configRoot = opts.root || config.root | ||
const workspaceRoot = opts.root || searchForWorkspaceRoot(config.root) | ||
debug('roots:', { configRoot, workspaceRoot }) | ||
const projects = await resolveProjectPaths( | ||
opts.projects, | ||
opts.root || config.root | ||
configRoot, | ||
workspaceRoot | ||
) | ||
debug('projects:', projects) | ||
let hasTypeScriptDep = false | ||
if (opts.parseNative) { | ||
try { | ||
const pkgJson = fs.readFileSync(join(root, 'package.json'), 'utf8') | ||
const pkgJson = fs.readFileSync( | ||
join(workspaceRoot, 'package.json'), | ||
'utf8' | ||
) | ||
const pkg = JSON.parse(pkgJson) | ||
@@ -52,14 +62,13 @@ const deps = { ...pkg.dependencies, ...pkg.devDependencies } | ||
debug('options:', { root, projects }) | ||
const parsedProjects = await Promise.all( | ||
projects.map((tsconfigFile) => | ||
hasTypeScriptDep | ||
? tsconfck.parseNative(tsconfigFile) | ||
: tsconfck.parse(tsconfigFile) | ||
) | ||
) | ||
resolvers = ( | ||
await Promise.all( | ||
projects.map((tsconfigFile) => | ||
(hasTypeScriptDep | ||
? tsconfck.parseNative(tsconfigFile) | ||
: tsconfck.parse(tsconfigFile) | ||
).then((config) => createResolver(config)) | ||
) | ||
) | ||
).filter(Boolean) as Resolver[] | ||
resolvers = parsedProjects | ||
.map(createResolver) | ||
.filter(Boolean) as Resolver[] | ||
}, | ||
@@ -155,9 +164,7 @@ async resolveId(id, importer) { | ||
let importerExtRE = /./ | ||
if (!opts.loose) { | ||
importerExtRE = | ||
options.allowJs || basename(configPath) === 'jsconfig.json' | ||
? jsLikeRE | ||
: /\.[mc]?tsx?$/ | ||
} | ||
const importerExtRE = opts.loose | ||
? /./ | ||
: options.allowJs || basename(configPath).startsWith('jsconfig.') | ||
? jsLikeRE | ||
: /\.[mc]?tsx?$/ | ||
@@ -269,3 +276,7 @@ const resolutionCache = new Map<string, string>() | ||
function resolveProjectPaths(projects: string[] | undefined, root: string) { | ||
function resolveProjectPaths( | ||
projects: string[] | undefined, | ||
configRoot: string, | ||
workspaceRoot: string | ||
) { | ||
if (projects) { | ||
@@ -276,6 +287,6 @@ return projects.map((file) => { | ||
} | ||
return resolve(root, file) | ||
return resolve(configRoot, file) | ||
}) | ||
} | ||
return tsconfck.findAll(root, { | ||
return tsconfck.findAll(workspaceRoot, { | ||
skip(dir) { | ||
@@ -282,0 +293,0 @@ return dir == 'node_modules' || dir == '.git' |
@@ -39,25 +39,5 @@ import { resolve } from 'path' | ||
// | ||
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://* | ||
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https:// | ||
// sindresorhus.com) | ||
// | ||
// Permission is hereby granted, free of charge, to any person * | ||
// obtaining a copy of this software and associated documentation * | ||
// files (the "Software"), to deal in the Software without * | ||
// restriction, including without limitation the rights to use, * copy, | ||
// modify, merge, publish, distribute, sublicense, and/or * sell copies | ||
// of the Software, and to permit persons to whom the * Software is | ||
// furnished to do so, subject to the following * conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be * | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
function escapeStringRegexp(string: string) { | ||
@@ -64,0 +44,0 @@ // Escape characters with special meaning either inside or outside |
@@ -5,11 +5,18 @@ export interface PluginOptions { | ||
* | ||
* @default viteConfig.root | ||
* When the `projects` option is set, crawling is skipped, so this is | ||
* used to resolve `projects` paths into absolute paths instead. | ||
* | ||
* @default | ||
* // The config root is used for resolving project paths, and the | ||
* // workspace root is used for crawling. | ||
* options.projects ? viteConfig.root : | ||
* vite.searchForWorkspaceRoot(viteConfig.root) | ||
*/ | ||
root?: string | ||
/** | ||
* An array of `tsconfig.json` paths (relative to `viteConfig.root`) | ||
* and/or directories that contain a `tsconfig.json` file. | ||
* An array of `.json` files and/or directories that contain a | ||
* `tsconfig.json` file. These JSON files must adhere to the | ||
* {@link https://www.typescriptlang.org/tsconfig TypeScript config interface}. The `extends` property is supported. | ||
* | ||
* When undefined, we crawl the project for `tsconfig.json` files. | ||
* You can set the `root` option to control where crawling starts. | ||
* This disables crawling for `tsconfig.json` files when defined. | ||
*/ | ||
@@ -16,0 +23,0 @@ projects?: string[] |
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
943
71077