vite-tsconfig-paths
Advanced tools
Comparing version 3.3.5 to 3.3.6
import { Plugin } from 'vite'; | ||
declare type PluginOptions = { | ||
/** | ||
* The directory to crawl for `tsconfig.json` files. | ||
* | ||
* @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[]; | ||
/** | ||
* Implicit extensions used when resolving an import path | ||
* like `./App` which has no explicit extension like `./App.vue` does. | ||
* | ||
* TypeScript and JavaScript extensions are used by default. | ||
*/ | ||
extensions?: string[]; | ||
/** | ||
* Disable strictness that limits path resolution to TypeScript | ||
* and JavaScript modules. | ||
* | ||
* Useful if you want asset URLs in Vue templates to be resolved, | ||
* or when `"allowJs": true` in your tsconfig isn't good enough. | ||
*/ | ||
loose?: boolean; | ||
}; | ||
import { PluginOptions } from './types'; | ||
declare const _default: (opts?: PluginOptions) => Plugin; | ||
export default _default; |
"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) { | ||
@@ -53,5 +42,5 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
var tsconfig_paths_1 = require("tsconfig-paths"); | ||
var tsconfig_loader_1 = require("tsconfig-paths/lib/tsconfig-loader"); | ||
var recrawl_sync_1 = require("recrawl-sync"); | ||
var globRex = require("globrex"); | ||
var config_1 = require("./config"); | ||
var join = path_1.posix.join, resolve = path_1.posix.resolve; | ||
@@ -113,20 +102,20 @@ var debug = require('debug')('vite-tsconfig-paths'); | ||
root += '/'; | ||
var config = tsconfig_paths_1.loadConfig(configPath || root); | ||
debug('loadConfig:', __assign({ configPath: configPath }, config)); | ||
if (config.resultType == 'failed') { | ||
var config = config_1.loadConfig(configPath || root); | ||
if (!config) { | ||
debug("[!] config not found: \"" + (configPath || root) + "\""); | ||
return null; | ||
} | ||
var baseUrl = config.baseUrl, paths = config.paths; | ||
if (!baseUrl) { | ||
debug("[!] missing baseUrl: \"" + config.configPath + "\""); | ||
return null; | ||
} | ||
debug('config loaded:', config); | ||
// Even if "paths" is undefined, the "baseUrl" is still | ||
// used to resolve bare imports. | ||
var resolveId = function (id, importer) { | ||
return viteResolve(join(config.absoluteBaseUrl, id), importer); | ||
return viteResolve(join(baseUrl, id), importer); | ||
}; | ||
if (config.paths) { | ||
var matchPath_1 = tsconfig_paths_1.createMatchPathAsync(config.absoluteBaseUrl, config.paths, config.mainFields || [ | ||
'module', | ||
'jsnext', | ||
'jsnext:main', | ||
'browser', | ||
'main', | ||
], config.addMatchAll); | ||
if (paths) { | ||
var matchPath_1 = tsconfig_paths_1.createMatchPathAsync(baseUrl, paths, mainFields); | ||
var resolveWithBaseUrl_1 = resolveId; | ||
@@ -151,7 +140,6 @@ var resolveWithPaths_1 = function (id, importer) { | ||
} | ||
var compilerOptions = loadCompilerOptions(config.configFileAbsolutePath); | ||
var isIncluded = getIncluder(compilerOptions); | ||
var isIncluded = getIncluder(config); | ||
var importerExtRE = /./; | ||
if (!opts.loose) { | ||
importerExtRE = compilerOptions.allowJs | ||
importerExtRE = config.allowJs | ||
? /\.(vue|svelte|mdx|mjs|[jt]sx?)$/ | ||
@@ -166,7 +154,13 @@ : /\.tsx?$/; | ||
case 0: | ||
if (!importerExtRE.test(importer)) return [3 /*break*/, 3]; | ||
path = resolved.get(id); | ||
if (!(!path && isLocalDescendant(importer, root))) return [3 /*break*/, 2]; | ||
// Ignore importers with unsupported extensions. | ||
if (!importerExtRE.test(importer)) | ||
return [2 /*return*/]; | ||
// Ignore node_modules and modules outside the root. | ||
if (!isLocalDescendant(importer, root)) | ||
return [2 /*return*/]; | ||
// Respect the include/exclude properties. | ||
if (!isIncluded(importer.slice(root.length))) | ||
return [2 /*return*/]; | ||
path = resolved.get(id); | ||
if (!!path) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, resolveId(id, importer)]; | ||
@@ -181,9 +175,7 @@ case 1: | ||
resolvedId: path, | ||
configPath: config.configFileAbsolutePath, | ||
compilerOptions: compilerOptions, | ||
configPath: config.configPath, | ||
}); | ||
} | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, path]; | ||
case 3: return [2 /*return*/]; | ||
return [2 /*return*/, path]; | ||
case 2: return [2 /*return*/]; | ||
} | ||
@@ -198,2 +190,3 @@ }); | ||
var relativeImportRE = /^\.\.?(\/|$)/; | ||
var mainFields = ['module', 'jsnext', 'jsnext:main', 'browser', 'main']; | ||
/** Returns true when `path` is within `root` and not an installed dependency. */ | ||
@@ -203,10 +196,2 @@ function isLocalDescendant(path, root) { | ||
} | ||
function loadCompilerOptions(configPath) { | ||
var _a = tsconfig_loader_1.loadTsconfig(configPath), include = _a.include, exclude = _a.exclude, _b = _a.compilerOptions, allowJs = _b.allowJs, checkJs = _b.checkJs; | ||
return { | ||
include: include, | ||
exclude: exclude, | ||
allowJs: allowJs || checkJs, | ||
}; | ||
} | ||
function compileGlob(glob) { | ||
@@ -232,3 +217,3 @@ return globRex(glob + (glob.endsWith('*') ? '' : '/**'), { | ||
var root = opts.root | ||
? vite_1.normalizePath(resolve(viteRoot, opts.root)) | ||
? resolve(viteRoot, vite_1.normalizePath(opts.root)) | ||
: viteRoot; | ||
@@ -235,0 +220,0 @@ var projects = opts.projects; |
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "3.3.5", | ||
"version": "3.3.6", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
@@ -11,3 +11,4 @@ "main": "dist/index.js", | ||
"build": "tsc -p .", | ||
"prepare": "yarn build" | ||
"prepare": "yarn build", | ||
"test": "jest" | ||
}, | ||
@@ -22,6 +23,11 @@ "dependencies": { | ||
"@types/globrex": "^0.1.0", | ||
"@types/jest": "^26.0.22", | ||
"@types/node": "^14.0.27", | ||
"esbuild": "^0.11.12", | ||
"esbuild-jest": "npm:@alloc/esbuild-jest@^0.3.6", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.0.5", | ||
"typescript": "^3.9.7", | ||
"vite": "latest" | ||
"vite": "latest", | ||
"vite-tsconfig-paths": "link:." | ||
}, | ||
@@ -28,0 +34,0 @@ "peerDependencies": { |
# vite-tsconfig-paths | ||
[![npm](https://img.shields.io/npm/v/vite-tsconfig-paths.svg)](https://www.npmjs.com/package/vite-tsconfig-paths) | ||
[![codecov](https://codecov.io/gh/aleclarson/vite-tsconfig-paths/branch/master/graph/badge.svg)](https://codecov.io/gh/aleclarson/vite-tsconfig-paths) | ||
[![Code style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) | ||
@@ -22,5 +23,3 @@ [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/alecdotbiz) | ||
export default defineConfig({ | ||
plugins: [ | ||
tsconfigPaths(), | ||
], | ||
plugins: [tsconfigPaths()], | ||
}) | ||
@@ -27,0 +26,0 @@ ``` |
133
src/index.ts
import { dirname, posix, isAbsolute } from 'path' | ||
import { normalizePath, Plugin } from 'vite' | ||
import { createMatchPathAsync, loadConfig } from 'tsconfig-paths' | ||
import { loadTsconfig } from 'tsconfig-paths/lib/tsconfig-loader' | ||
import { createMatchPathAsync } from 'tsconfig-paths' | ||
import { crawl } from 'recrawl-sync' | ||
import globRex = require('globrex') | ||
import { PluginOptions } from './types' | ||
import { loadConfig } from './config' | ||
@@ -12,34 +13,2 @@ const { join, resolve } = posix | ||
type PluginOptions = { | ||
/** | ||
* The directory to crawl for `tsconfig.json` files. | ||
* | ||
* @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[] | ||
/** | ||
* Implicit extensions used when resolving an import path | ||
* like `./App` which has no explicit extension like `./App.vue` does. | ||
* | ||
* TypeScript and JavaScript extensions are used by default. | ||
*/ | ||
extensions?: string[] | ||
/** | ||
* Disable strictness that limits path resolution to TypeScript | ||
* and JavaScript modules. | ||
* | ||
* Useful if you want asset URLs in Vue templates to be resolved, | ||
* or when `"allowJs": true` in your tsconfig isn't good enough. | ||
*/ | ||
loose?: boolean | ||
} | ||
export default (opts: PluginOptions = {}): Plugin => ({ | ||
@@ -82,25 +51,21 @@ name: 'vite:tsconfig-paths', | ||
const config = loadConfig(configPath || root) | ||
debug('loadConfig:', { configPath, ...config }) | ||
if (config.resultType == 'failed') { | ||
if (!config) { | ||
debug(`[!] config not found: "${configPath || root}"`) | ||
return null | ||
} | ||
const { baseUrl, paths } = config | ||
if (!baseUrl) { | ||
debug(`[!] missing baseUrl: "${config.configPath}"`) | ||
return null | ||
} | ||
debug('config loaded:', config) | ||
// Even if "paths" is undefined, the "baseUrl" is still | ||
// used to resolve bare imports. | ||
let resolveId: Resolver = (id, importer) => | ||
viteResolve(join(config.absoluteBaseUrl, id), importer) | ||
viteResolve(join(baseUrl, id), importer) | ||
if (config.paths) { | ||
const matchPath = createMatchPathAsync( | ||
config.absoluteBaseUrl, | ||
config.paths, | ||
config.mainFields || [ | ||
'module', | ||
'jsnext', | ||
'jsnext:main', | ||
'browser', | ||
'main', | ||
], | ||
config.addMatchAll | ||
) | ||
if (paths) { | ||
const matchPath = createMatchPathAsync(baseUrl, paths, mainFields) | ||
@@ -127,8 +92,7 @@ const resolveWithBaseUrl = resolveId | ||
const compilerOptions = loadCompilerOptions(config.configFileAbsolutePath) | ||
const isIncluded = getIncluder(compilerOptions) | ||
const isIncluded = getIncluder(config) | ||
let importerExtRE = /./ | ||
if (!opts.loose) { | ||
importerExtRE = compilerOptions.allowJs | ||
importerExtRE = config.allowJs | ||
? /\.(vue|svelte|mdx|mjs|[jt]sx?)$/ | ||
@@ -140,17 +104,20 @@ : /\.tsx?$/ | ||
return async (id, importer) => { | ||
if (importerExtRE.test(importer)) { | ||
let path = resolved.get(id) | ||
if (!path && isLocalDescendant(importer, root)) { | ||
if (!isIncluded(importer.slice(root.length))) return | ||
path = await resolveId(id, importer) | ||
if (path) { | ||
resolved.set(id, path) | ||
debug(`resolved:`, { | ||
id, | ||
importer, | ||
resolvedId: path, | ||
configPath: config.configFileAbsolutePath, | ||
compilerOptions, | ||
}) | ||
} | ||
// Ignore importers with unsupported extensions. | ||
if (!importerExtRE.test(importer)) return | ||
// Ignore node_modules and modules outside the root. | ||
if (!isLocalDescendant(importer, root)) return | ||
// Respect the include/exclude properties. | ||
if (!isIncluded(importer.slice(root.length))) return | ||
let path = resolved.get(id) | ||
if (!path) { | ||
path = await resolveId(id, importer) | ||
if (path) { | ||
resolved.set(id, path) | ||
debug(`resolved:`, { | ||
id, | ||
importer, | ||
resolvedId: path, | ||
configPath: config.configPath, | ||
}) | ||
} | ||
@@ -166,2 +133,3 @@ return path | ||
const relativeImportRE = /^\.\.?(\/|$)/ | ||
const mainFields = ['module', 'jsnext', 'jsnext:main', 'browser', 'main'] | ||
@@ -173,21 +141,2 @@ /** Returns true when `path` is within `root` and not an installed dependency. */ | ||
interface CompilerOptions { | ||
include?: string[] | ||
exclude?: string[] | ||
allowJs?: boolean | ||
} | ||
function loadCompilerOptions(configPath: string): CompilerOptions { | ||
const { | ||
include, | ||
exclude, | ||
compilerOptions: { allowJs, checkJs }, | ||
}: any = loadTsconfig(configPath) | ||
return { | ||
include, | ||
exclude, | ||
allowJs: allowJs || checkJs, | ||
} | ||
} | ||
function compileGlob(glob: string) { | ||
@@ -200,3 +149,9 @@ return globRex(glob + (glob.endsWith('*') ? '' : '/**'), { | ||
function getIncluder({ include = [], exclude = [] }: CompilerOptions) { | ||
function getIncluder({ | ||
include = [], | ||
exclude = [], | ||
}: { | ||
include?: string[] | ||
exclude?: string[] | ||
}) { | ||
if (include.length || exclude.length) { | ||
@@ -214,3 +169,3 @@ const included = include.map(compileGlob) | ||
const root = opts.root | ||
? normalizePath(resolve(viteRoot, opts.root)) | ||
? resolve(viteRoot, normalizePath(opts.root)) | ||
: viteRoot | ||
@@ -217,0 +172,0 @@ |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
35401
15
575
10
89
2