vite-tsconfig-paths
Advanced tools
Comparing version 1.1.0 to 2.0.0-alpha.1
{ | ||
"name": "vite-tsconfig-paths", | ||
"version": "1.1.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "Vite resolver for TypeScript compilerOptions.paths", | ||
@@ -20,6 +20,6 @@ "main": "dist/index.js", | ||
"typescript": "^3.9.7", | ||
"vite": "^1.0.0-rc.4" | ||
"vite": "latest" | ||
}, | ||
"peerDependencies": { | ||
"vite": "*" | ||
"vite": ">=2" | ||
}, | ||
@@ -26,0 +26,0 @@ "keywords": [ |
@@ -0,35 +1,55 @@ | ||
import type { Plugin } from 'vite' | ||
import { relative } from 'path' | ||
import { createMatchPath, loadConfig } from 'tsconfig-paths' | ||
import { mainFields, supportedExts, Resolver } from 'vite/dist/node/resolver' | ||
const debug = require('debug')('vite-tsconfig-paths') | ||
let resolver: Resolver = {} | ||
type PluginOptions = { | ||
/** | ||
* The root directory to load `tsconfig.json` from. | ||
* | ||
* @default viteConfig.root | ||
*/ | ||
root?: string | ||
/** | ||
* File extensions to search for. | ||
* | ||
* @default Object.keys(require.extensions) | ||
*/ | ||
extensions?: string[] | ||
} | ||
const config = loadConfig() | ||
if (config.resultType == 'failed') { | ||
console.warn('[vite-tsconfig-paths]', config.message) | ||
} else if (config.paths) { | ||
const matchPath = createMatchPath( | ||
config.absoluteBaseUrl, | ||
config.paths, | ||
config.mainFields || mainFields, | ||
config.addMatchAll | ||
) | ||
const resolved = new Map<string, string>() | ||
resolver = { | ||
alias(id) { | ||
let path = resolved.get(id) | ||
if (!path) { | ||
path = matchPath(id, undefined, undefined, supportedExts) | ||
if (path) { | ||
resolved.set(id, (path = '/' + relative(process.cwd(), path))) | ||
debug(`resolved "${id}" to "${path}"`) | ||
export default (opts: PluginOptions): Plugin => ({ | ||
name: 'vite:tsconfig-paths', | ||
configResolved({ root, logger }) { | ||
const config = loadConfig(opts.root || root) | ||
if (config.resultType == 'failed') { | ||
logger.warn(`[vite-tsconfig-paths] ${config.message}`) | ||
} else if (config.paths) { | ||
const matchPath = createMatchPath( | ||
config.absoluteBaseUrl, | ||
config.paths, | ||
config.mainFields || [ | ||
'module', | ||
'jsnext', | ||
'jsnext:main', | ||
'browser', | ||
'main', | ||
], | ||
config.addMatchAll | ||
) | ||
const resolved = new Map<string, string>() | ||
this.resolveId = (id) => { | ||
let path = resolved.get(id) | ||
if (!path) { | ||
path = matchPath(id, undefined, undefined, opts.extensions) | ||
if (path) { | ||
resolved.set(id, (path = '/' + relative(process.cwd(), path))) | ||
debug(`resolved "${id}" to "${path}"`) | ||
} | ||
} | ||
return path | ||
} | ||
return path | ||
}, | ||
} | ||
} | ||
export default resolver | ||
} | ||
}, | ||
}) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
6667
84
2