vite-tsconfig-paths
Advanced tools
Comparing version 3.2.1 to 3.3.0
import { Plugin } from 'vite'; | ||
declare type PluginOptions = { | ||
/** | ||
* The root directories to load `tsconfig.json` from. | ||
* The directory to crawl for `tsconfig.json` files. | ||
* | ||
* @default [viteConfig.root] | ||
* @default viteConfig.root | ||
*/ | ||
root?: string; | ||
/** | ||
* An array of `tsconfig.json` paths (relative to `viteConfig.root`) | ||
* and/or directories that contain a `tsconfig.json` file. | ||
* | ||
* When undefined, we crawl the project for `tsconfig.json` files. | ||
* You can set the `root` option to control where crawling starts. | ||
*/ | ||
projects?: string[]; | ||
@@ -21,3 +29,3 @@ /** | ||
* Useful if you want asset URLs in Vue templates to be resolved, | ||
* or when `"checkJs": true` in your tsconfig isn't good enough. | ||
* or when `"allowJs": true` in your tsconfig isn't good enough. | ||
*/ | ||
@@ -24,0 +32,0 @@ loose?: boolean; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -43,2 +54,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
var tsconfig_loader_1 = require("tsconfig-paths/lib/tsconfig-loader"); | ||
var recrawl_sync_1 = require("recrawl-sync"); | ||
var globRex = require("globrex"); | ||
@@ -52,8 +64,6 @@ var debug = require('debug')('vite-tsconfig-paths'); | ||
configResolved: function (_a) { | ||
var _b; | ||
var viteRoot = _a.root, logger = _a.logger; | ||
var extensions = ((_b = opts.extensions) === null || _b === void 0 ? void 0 : _b.concat(implicitExtensions)) || implicitExtensions; | ||
var resolvers = (opts.projects || [viteRoot]) | ||
.map(createResolver) | ||
.filter(Boolean); | ||
var viteRoot = _a.root; | ||
var projects = findProjects(viteRoot, opts); | ||
var extensions = getFileExtensions(opts.extensions); | ||
debug('options:', { projects: projects, extensions: extensions }); | ||
var viteResolve; | ||
@@ -69,2 +79,3 @@ this.buildStart = function () { | ||
}; | ||
var resolvers = projects.map(createResolver).filter(Boolean); | ||
this.resolveId = function (id, importer) { | ||
@@ -99,11 +110,11 @@ return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
var configPath = root.endsWith('.json') ? root : null; | ||
if (configPath) | ||
root = path_1.dirname(root); | ||
root = vite_1.normalizePath(path_1.resolve(viteRoot, root)) + '/'; | ||
var config = tsconfig_paths_1.loadConfig(root); | ||
var config = tsconfig_paths_1.loadConfig(configPath || root); | ||
debug('loadConfig:', __assign({ configPath: configPath }, config)); | ||
if (config.resultType == 'failed') { | ||
logger.warn("[vite-tsconfig-paths] " + config.message); | ||
return null; | ||
} | ||
if (!config.baseUrl) { | ||
return null; | ||
} | ||
var resolveId; | ||
@@ -180,3 +191,2 @@ if (config.paths) { | ||
} | ||
var implicitExtensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs']; | ||
function loadCompilerOptions(configPath) { | ||
@@ -199,2 +209,20 @@ var config = tsconfig_loader_1.loadTsconfig(configPath); | ||
} | ||
function findProjects(viteRoot, opts) { | ||
var projects = opts.projects; | ||
if (!projects) { | ||
var root_1 = opts.root && vite_1.normalizePath(path_1.resolve(viteRoot, opts.root)); | ||
projects = recrawl_sync_1.crawl(root_1 || viteRoot, { | ||
only: ['tsconfig.json'], | ||
skip: ['node_modules', '.git'], | ||
}); | ||
if (root_1) { | ||
projects = projects.map(function (configPath) { return path_1.resolve(root_1, configPath); }); | ||
} | ||
} | ||
return projects; | ||
} | ||
function getFileExtensions(exts) { | ||
var requiredExts = ['.ts', '.tsx', '.js', '.jsx', '.mjs']; | ||
return exts ? exts.concat(requiredExts) : requiredExts; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
@@ -16,2 +16,3 @@ "main": "dist/index.js", | ||
"globrex": "^0.1.2", | ||
"recrawl-sync": "^2.0.3", | ||
"tsconfig-paths": "^3.9.0" | ||
@@ -18,0 +19,0 @@ }, |
@@ -56,3 +56,3 @@ # vite-tsconfig-paths | ||
Say the `baseUrl` is `../root` and you import `react`. This plugin will use `../root/react` if it exists. If not found, then `react` is resolved normally. | ||
Say the `baseUrl` is `../root` and you import `react`. This plugin will use `../root/react` if it exists. If not found, then `react` is resolved normally. The `baseUrl` is relative to the project root (where `tsconfig.json` lives). | ||
@@ -59,0 +59,0 @@ |
@@ -1,5 +0,6 @@ | ||
import { join, resolve, isAbsolute } from 'path' | ||
import { dirname, join, resolve, isAbsolute } from 'path' | ||
import { normalizePath, Plugin } from 'vite' | ||
import { createMatchPathAsync, loadConfig } from 'tsconfig-paths' | ||
import { loadTsconfig } from 'tsconfig-paths/lib/tsconfig-loader' | ||
import { crawl } from 'recrawl-sync' | ||
import globRex = require('globrex') | ||
@@ -11,6 +12,14 @@ | ||
/** | ||
* The root directories to load `tsconfig.json` from. | ||
* The directory to crawl for `tsconfig.json` files. | ||
* | ||
* @default [viteConfig.root] | ||
* @default viteConfig.root | ||
*/ | ||
root?: string | ||
/** | ||
* An array of `tsconfig.json` paths (relative to `viteConfig.root`) | ||
* and/or directories that contain a `tsconfig.json` file. | ||
* | ||
* When undefined, we crawl the project for `tsconfig.json` files. | ||
* You can set the `root` option to control where crawling starts. | ||
*/ | ||
projects?: string[] | ||
@@ -29,3 +38,3 @@ /** | ||
* Useful if you want asset URLs in Vue templates to be resolved, | ||
* or when `"checkJs": true` in your tsconfig isn't good enough. | ||
* or when `"allowJs": true` in your tsconfig isn't good enough. | ||
*/ | ||
@@ -38,8 +47,6 @@ loose?: boolean | ||
enforce: 'pre', | ||
configResolved({ root: viteRoot, logger }) { | ||
const extensions = | ||
opts.extensions?.concat(implicitExtensions) || implicitExtensions | ||
const resolvers = (opts.projects || [viteRoot]) | ||
.map(createResolver) | ||
.filter(Boolean) as Resolver[] | ||
configResolved({ root: viteRoot }) { | ||
const projects = findProjects(viteRoot, opts) | ||
const extensions = getFileExtensions(opts.extensions) | ||
debug('options:', { projects, extensions }) | ||
@@ -52,2 +59,3 @@ let viteResolve: Resolver | ||
const resolvers = projects.map(createResolver).filter(Boolean) as Resolver[] | ||
this.resolveId = async function (id, importer) { | ||
@@ -70,12 +78,11 @@ if (importer && !relativeImportRE.test(id) && !isAbsolute(id)) { | ||
function createResolver(root: string): Resolver | null { | ||
const configPath = root.endsWith('.json') ? root : null | ||
if (configPath) root = dirname(root) | ||
root = normalizePath(resolve(viteRoot, root)) + '/' | ||
const config = loadConfig(root) | ||
const config = loadConfig(configPath || root) | ||
debug('loadConfig:', { configPath, ...config }) | ||
if (config.resultType == 'failed') { | ||
logger.warn(`[vite-tsconfig-paths] ${config.message}`) | ||
return null | ||
} | ||
if (!config.baseUrl) { | ||
return null | ||
} | ||
@@ -150,4 +157,2 @@ let resolveId: Resolver | ||
const implicitExtensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs'] | ||
interface CompilerOptions { | ||
@@ -175,1 +180,21 @@ include?: string[] | ||
} | ||
function findProjects(viteRoot: string, opts: PluginOptions) { | ||
let { projects } = opts | ||
if (!projects) { | ||
const root = opts.root && normalizePath(resolve(viteRoot, opts.root)) | ||
projects = crawl(root || viteRoot, { | ||
only: ['tsconfig.json'], | ||
skip: ['node_modules', '.git'], | ||
}) | ||
if (root) { | ||
projects = projects.map((configPath) => resolve(root, configPath)) | ||
} | ||
} | ||
return projects | ||
} | ||
function getFileExtensions(exts?: string[]) { | ||
const requiredExts = ['.ts', '.tsx', '.js', '.jsx', '.mjs'] | ||
return exts ? exts.concat(requiredExts) : requiredExts | ||
} |
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
28423
429
5
+ Addedrecrawl-sync@^2.0.3
+ Added@cush/relative@1.0.0(transitive)
+ Added@isaacs/cliui@8.0.2(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Added@pkgjs/parseargs@0.11.0(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedany-promise@1.3.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcommander@4.1.1(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedforeground-child@3.3.0(transitive)
+ Addedglob@10.4.5(transitive)
+ Addedglob-regex@0.3.2(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@3.4.3(transitive)
+ Addedlines-and-columns@1.2.4(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedminimatch@9.0.5(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addedmz@2.7.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedpirates@4.0.6(transitive)
+ Addedrecrawl-sync@2.2.3(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedsucrase@3.35.0(transitive)
+ Addedthenify@3.3.1(transitive)
+ Addedthenify-all@1.6.0(transitive)
+ Addedts-interface-checker@0.1.13(transitive)
+ Addedtslib@1.14.1(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@7.0.08.1.0(transitive)