vite-tsconfig-paths
Advanced tools
Comparing version 3.3.11 to 3.3.12
import { Plugin } from 'vite'; | ||
import { PluginOptions } from './types'; | ||
interface 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; | ||
} | ||
declare const _default: (opts?: PluginOptions) => Plugin; | ||
export default _default; |
@@ -1,167 +0,213 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const path_1 = require("./path"); | ||
const vite_1 = require("vite"); | ||
const tsconfig_paths_1 = require("tsconfig-paths"); | ||
const recrawl_sync_1 = require("recrawl-sync"); | ||
const globRex = require("globrex"); | ||
const config_1 = require("./config"); | ||
const debug = require('debug')('vite-tsconfig-paths'); | ||
exports.default = (opts = {}) => ({ | ||
name: 'vite:tsconfig-paths', | ||
enforce: 'pre', | ||
configResolved({ root: viteRoot }) { | ||
const projects = findProjects(viteRoot, opts); | ||
const extensions = getFileExtensions(opts.extensions); | ||
debug('options:', { projects, extensions }); | ||
let viteResolve; | ||
this.buildStart = function () { | ||
viteResolve = async (id, importer) => { var _a; return (_a = (await this.resolve(id, importer, { skipSelf: true }))) === null || _a === void 0 ? void 0 : _a.id; }; | ||
}; | ||
const resolvers = projects.map(createResolver).filter(Boolean); | ||
this.resolveId = async function (id, importer) { | ||
if (importer && !relativeImportRE.test(id) && !path_1.isAbsolute(id)) { | ||
for (const resolve of resolvers) { | ||
const resolved = await resolve(id, importer); | ||
if (resolved) { | ||
return resolved; | ||
} | ||
} | ||
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/path.ts | ||
var _os = require('os'); | ||
var _path = require('path'); | ||
var _vite = require('vite'); | ||
var isWindows = _os.platform.call(void 0, ) == "win32"; | ||
var resolve = isWindows ? (...paths) => _vite.normalizePath.call(void 0, _path.win32.resolve(...paths)) : _path.posix.resolve; | ||
var isAbsolute = isWindows ? _path.win32.isAbsolute : _path.posix.isAbsolute; | ||
var join = _path.posix.join; | ||
var relative = _path.posix.relative; | ||
// src/index.ts | ||
var _tsconfigpaths = require('tsconfig-paths'); | ||
var _recrawlsync = require('recrawl-sync'); | ||
var _globrex = require('globrex'); var _globrex2 = _interopRequireDefault(_globrex); | ||
// src/config.ts | ||
var _tsconfigloader = require('tsconfig-paths/lib/tsconfig-loader'); | ||
var _fs = require('fs'); | ||
function loadConfig(cwd) { | ||
const configPath = resolveConfigPath(cwd); | ||
if (configPath) { | ||
const config = _tsconfigloader.loadTsconfig.call(void 0, configPath); | ||
const { | ||
compilerOptions: {allowJs, checkJs, baseUrl, paths, outDir} = {} | ||
} = config; | ||
return { | ||
configPath: _vite.normalizePath.call(void 0, configPath), | ||
include: config.include, | ||
exclude: config.exclude, | ||
allowJs: allowJs || checkJs, | ||
baseUrl: baseUrl && _vite.normalizePath.call(void 0, _path.resolve.call(void 0, configPath, "..", baseUrl)), | ||
paths, | ||
outDir | ||
}; | ||
} | ||
} | ||
function resolveConfigPath(cwd) { | ||
if (_fs.statSync.call(void 0, cwd).isFile()) { | ||
return cwd; | ||
} | ||
const configPath = _tsconfigloader.walkForTsConfig.call(void 0, cwd); | ||
if (configPath) { | ||
return configPath; | ||
} | ||
} | ||
// src/index.ts | ||
var _debug2 = require('debug'); var _debug3 = _interopRequireDefault(_debug2); | ||
var debug = _debug3.default.call(void 0, "vite-tsconfig-paths"); | ||
var src_default = (opts = {}) => ({ | ||
name: "vite:tsconfig-paths", | ||
enforce: "pre", | ||
configResolved({root: viteRoot}) { | ||
const projects = findProjects(viteRoot, opts); | ||
const extensions = getFileExtensions(opts.extensions); | ||
debug("options:", {projects, extensions}); | ||
let viteResolve; | ||
this.buildStart = function() { | ||
viteResolve = async (id, importer) => { | ||
var _a; | ||
return (_a = await this.resolve(id, importer, {skipSelf: true})) == null ? void 0 : _a.id; | ||
}; | ||
}; | ||
const resolvers = projects.map(createResolver).filter(Boolean); | ||
this.resolveId = async function(id, importer) { | ||
if (importer && !relativeImportRE.test(id) && !isAbsolute(id)) { | ||
for (const resolve3 of resolvers) { | ||
const resolved = await resolve3(id, importer); | ||
if (resolved) { | ||
return resolved; | ||
} | ||
} | ||
} | ||
}; | ||
function createResolver(root) { | ||
const configPath = root.endsWith(".json") ? root : null; | ||
if (configPath) | ||
root = _path.dirname.call(void 0, root); | ||
root += "/"; | ||
const config = loadConfig(configPath || root); | ||
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); | ||
let resolveId = (id, importer) => viteResolve(join(baseUrl, id), importer); | ||
if (paths) { | ||
const matchPath = _tsconfigpaths.createMatchPathAsync.call(void 0, baseUrl, paths, mainFields); | ||
const resolveWithBaseUrl = resolveId; | ||
const resolveWithPaths = (id, importer) => new Promise((done) => { | ||
matchPath(id, void 0, void 0, extensions, (error, path2) => { | ||
if (path2) { | ||
path2 = _vite.normalizePath.call(void 0, path2); | ||
done(viteResolve(path2, importer)); | ||
} else { | ||
error && debug(error.message); | ||
done(void 0); | ||
} | ||
}; | ||
function createResolver(root) { | ||
const configPath = root.endsWith('.json') ? root : null; | ||
if (configPath) | ||
root = path_1.dirname(root); | ||
root += '/'; | ||
const config = config_1.loadConfig(configPath || root); | ||
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 = (id, importer) => viteResolve(path_1.join(baseUrl, id), importer); | ||
if (paths) { | ||
const matchPath = tsconfig_paths_1.createMatchPathAsync(baseUrl, paths, mainFields); | ||
const resolveWithBaseUrl = resolveId; | ||
const resolveWithPaths = (id, importer) => new Promise((done) => { | ||
matchPath(id, void 0, void 0, extensions, (error, path) => { | ||
if (path) { | ||
path = vite_1.normalizePath(path); | ||
done(viteResolve(path, importer)); | ||
} | ||
else { | ||
error && debug(error.message); | ||
done(void 0); | ||
} | ||
}); | ||
}); | ||
resolveId = (id, importer) => resolveWithPaths(id, importer).then((resolved) => resolved || resolveWithBaseUrl(id, importer)); | ||
} | ||
const isIncluded = getIncluder(config); | ||
let importerExtRE = /./; | ||
if (!opts.loose) { | ||
importerExtRE = config.allowJs | ||
? /\.(vue|svelte|mdx|mjs|[jt]sx?)$/ | ||
: /\.tsx?$/; | ||
} | ||
const resolved = new Map(); | ||
return async (id, importer) => { | ||
importer = vite_1.normalizePath(importer); | ||
// Ignore importers with unsupported extensions. | ||
if (!importerExtRE.test(importer)) | ||
return; | ||
// Respect the include/exclude properties. | ||
if (!isIncluded(path_1.relative(root, importer))) | ||
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, | ||
}); | ||
} | ||
} | ||
return path; | ||
}; | ||
}); | ||
}); | ||
resolveId = (id, importer) => resolveWithPaths(id, importer).then((resolved2) => resolved2 || resolveWithBaseUrl(id, importer)); | ||
} | ||
const isIncluded = getIncluder(config); | ||
let importerExtRE = /./; | ||
if (!opts.loose) { | ||
importerExtRE = config.allowJs ? /\.(vue|svelte|mdx|mjs|[jt]sx?)$/ : /\.tsx?$/; | ||
} | ||
const resolved = new Map(); | ||
return async (id, importer) => { | ||
importer = _vite.normalizePath.call(void 0, importer); | ||
if (!importerExtRE.test(importer)) | ||
return; | ||
if (!isIncluded(relative(root, importer))) | ||
return; | ||
let path2 = resolved.get(id); | ||
if (!path2) { | ||
path2 = await resolveId(id, importer); | ||
if (path2) { | ||
resolved.set(id, path2); | ||
debug(`resolved:`, { | ||
id, | ||
importer, | ||
resolvedId: path2, | ||
configPath: config.configPath | ||
}); | ||
} | ||
} | ||
}, | ||
return path2; | ||
}; | ||
} | ||
} | ||
}); | ||
const relativeImportRE = /^\.\.?(\/|$)/; | ||
const mainFields = ['module', 'jsnext', 'jsnext:main', 'browser', 'main']; | ||
const defaultInclude = ['**/*']; | ||
const defaultExclude = ['node_modules', 'bower_components', 'jspm_packages']; | ||
var relativeImportRE = /^\.\.?(\/|$)/; | ||
var mainFields = ["module", "jsnext", "jsnext:main", "browser", "main"]; | ||
var defaultInclude = ["**/*"]; | ||
var defaultExclude = ["node_modules", "bower_components", "jspm_packages"]; | ||
function compileGlob(glob) { | ||
if (!relativeImportRE.test(glob)) { | ||
glob = './' + glob; | ||
} | ||
if (!glob.split('/').pop().includes('*')) { | ||
glob += '/**'; | ||
} | ||
return globRex(glob, { | ||
extended: true, | ||
globstar: true, | ||
}).regex; | ||
if (!relativeImportRE.test(glob)) { | ||
glob = "./" + glob; | ||
} | ||
if (!glob.split("/").pop().includes("*")) { | ||
glob += "/**"; | ||
} | ||
return _globrex2.default.call(void 0, glob, { | ||
extended: true, | ||
globstar: true | ||
}).regex; | ||
} | ||
/** | ||
* The returned function does not support absolute paths. | ||
* Be sure to call `path.relative` on your path first. | ||
*/ | ||
function getIncluder({ include = defaultInclude, exclude = defaultExclude, outDir, }) { | ||
if (outDir) { | ||
exclude = exclude.concat(outDir); | ||
} | ||
if (include.length || exclude.length) { | ||
const included = include.map(compileGlob); | ||
const excluded = exclude.map(compileGlob); | ||
debug(`compiled globs:`, { included, excluded }); | ||
return (path) => { | ||
if (!relativeImportRE.test(path)) { | ||
path = './' + path; | ||
} | ||
const test = (glob) => glob.test(path); | ||
return included.some(test) && !excluded.some(test); | ||
}; | ||
} | ||
return () => true; | ||
function getIncluder({ | ||
include = defaultInclude, | ||
exclude = defaultExclude, | ||
outDir | ||
}) { | ||
if (outDir) { | ||
exclude = exclude.concat(outDir); | ||
} | ||
if (include.length || exclude.length) { | ||
const included = include.map(compileGlob); | ||
const excluded = exclude.map(compileGlob); | ||
debug(`compiled globs:`, {included, excluded}); | ||
return (path2) => { | ||
if (!relativeImportRE.test(path2)) { | ||
path2 = "./" + path2; | ||
} | ||
const test = (glob) => glob.test(path2); | ||
return included.some(test) && !excluded.some(test); | ||
}; | ||
} | ||
return () => true; | ||
} | ||
function findProjects(viteRoot, opts) { | ||
const root = opts.root | ||
? path_1.resolve(viteRoot, vite_1.normalizePath(opts.root)) | ||
: viteRoot; | ||
let { projects } = opts; | ||
if (!projects) { | ||
debug(`crawling "${root}"`); | ||
projects = recrawl_sync_1.crawl(root, { | ||
only: ['tsconfig.json'], | ||
skip: ['node_modules', '.git'], | ||
}); | ||
} | ||
// Calculate the depth of each project path. | ||
const depthMap = {}; | ||
projects = projects.map((projectPath) => { | ||
projectPath = path_1.resolve(root, vite_1.normalizePath(projectPath)); | ||
depthMap[projectPath] = | ||
projectPath.split('/').length - (projectPath.endsWith('.json') ? 1 : 0); | ||
return projectPath; | ||
const root = opts.root ? resolve(viteRoot, _vite.normalizePath.call(void 0, opts.root)) : viteRoot; | ||
let {projects} = opts; | ||
if (!projects) { | ||
debug(`crawling "${root}"`); | ||
projects = _recrawlsync.crawl.call(void 0, root, { | ||
only: ["tsconfig.json"], | ||
skip: ["node_modules", ".git"] | ||
}); | ||
// Ensure deeper projects take precedence. | ||
return projects.sort((a, b) => depthMap[b] - depthMap[a]); | ||
} | ||
const depthMap = {}; | ||
projects = projects.map((projectPath) => { | ||
projectPath = resolve(root, _vite.normalizePath.call(void 0, projectPath)); | ||
depthMap[projectPath] = projectPath.split("/").length - (projectPath.endsWith(".json") ? 1 : 0); | ||
return projectPath; | ||
}); | ||
return projects.sort((a, b) => depthMap[b] - depthMap[a]); | ||
} | ||
function getFileExtensions(exts) { | ||
const requiredExts = ['.ts', '.tsx', '.js', '.jsx', '.mjs']; | ||
return exts ? exts.concat(requiredExts) : requiredExts; | ||
const requiredExts = [".ts", ".tsx", ".js", ".jsx", ".mjs"]; | ||
return exts ? exts.concat(requiredExts) : requiredExts; | ||
} | ||
//# sourceMappingURL=index.js.map | ||
exports.default = src_default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "3.3.11", | ||
"version": "3.3.12", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"exports": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"author": "aleclarson", | ||
@@ -10,3 +15,3 @@ "repository": "aleclarson/vite-tsconfig-paths", | ||
"scripts": { | ||
"build": "tsc -p .", | ||
"build": "rimraf dist/* && tsup src/index.ts --sourcemap --dts --format cjs,esm", | ||
"prepare": "yarn build", | ||
@@ -22,2 +27,4 @@ "test": "jest" | ||
"devDependencies": { | ||
"@alloc/fast-rimraf": "^1.0.8", | ||
"@types/debug": "^4.1.5", | ||
"@types/globrex": "^0.1.0", | ||
@@ -32,3 +39,4 @@ "@types/jest": "^26.0.22", | ||
"rollup": "^2.45.2", | ||
"typescript": "^3.9.7", | ||
"tsup": "^4.10.1", | ||
"typescript": "^4.2.4", | ||
"vite": "latest", | ||
@@ -35,0 +43,0 @@ "vite-tsconfig-paths": "link:." |
@@ -5,7 +5,8 @@ import { dirname, isAbsolute, join, relative, resolve } from './path' | ||
import { crawl } from 'recrawl-sync' | ||
import globRex = require('globrex') | ||
import globRex from 'globrex' | ||
import { PluginOptions } from './types' | ||
import { loadConfig } from './config' | ||
const debug = require('debug')('vite-tsconfig-paths') | ||
import _debug from 'debug' | ||
const debug = _debug('vite-tsconfig-paths') | ||
@@ -12,0 +13,0 @@ export default (opts: PluginOptions = {}): Plugin => ({ |
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
52943
722
15
12
3