├── tsconfig.json ├── .gitignore ├── .npmignore ├── test.ts ├── yarn.lock ├── package.json ├── index.d.ts ├── index.js └── README.md /tsconfig.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /test.ts 2 | /tsconfig.json 3 | -------------------------------------------------------------------------------- /test.ts: -------------------------------------------------------------------------------- 1 | import {resolveModuleName} from './'; 2 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | typescript@3.5.3: 6 | version "3.5.3" 7 | resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977" 8 | integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g== 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ts-pnp", 3 | "version": "1.2.0", 4 | "description": "plug'n'play resolver for TypeScript", 5 | "license": "MIT", 6 | "engines": { 7 | "node": ">=6" 8 | }, 9 | "homepage": "https://github.com/arcanis/ts-pnp", 10 | "bugs": { 11 | "url": "https://github.com/arcanis/ts-pnp/issues" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/arcanis/ts-pnp.git" 16 | }, 17 | "keywords": [ 18 | "typescript", 19 | "yarn", 20 | "plugnplay", 21 | "pnp" 22 | ], 23 | "devDependencies": { 24 | "typescript": "3.5.3" 25 | }, 26 | "peerDependenciesMeta": { 27 | "typescript": { 28 | "optional": true 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import * as ts from 'typescript'; 2 | 3 | export declare function resolveModuleName( 4 | moduleName: string, 5 | containingFile: string, 6 | options: ts.CompilerOptions, 7 | moduleResolutionHost: ts.ModuleResolutionHost, 8 | 9 | realResolveModuleName: ( 10 | moduleName: string, 11 | containingFile: string, 12 | options: ts.CompilerOptions, 13 | moduleResolutionHost: ts.ResolvedModuleWithFailedLookupLocations, 14 | ) => ts.ResolvedModuleWithFailedLookupLocations, 15 | ): ts.ResolvedModuleWithFailedLookupLocations; 16 | 17 | export declare function resolveModuleName( 18 | moduleName: string, 19 | containingFile: string, 20 | options: ts.CompilerOptions, 21 | moduleResolutionHost: ts.ModuleResolutionHost, 22 | 23 | realResolveModuleName: ( 24 | moduleName: string, 25 | containingFile: string, 26 | options: ts.CompilerOptions, 27 | moduleResolutionHost: ts.ModuleResolutionHost, 28 | ) => ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations, 29 | ): ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations; 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function resolveModuleName(request, issuer, compilerOptions, moduleResolutionHost, parentResolver) { 2 | const pnp = require(`pnpapi`); 3 | 4 | const [, prefix = ``, packageName = ``, rest] = request.match(/^(!(?:.*!)+)?((?!\.{0,2}\/)(?:@[^\/]+\/)?[^\/]+)?(.*)/); 5 | 6 | let failedLookupLocations = []; 7 | 8 | // First we try the resolution on "@types/package-name" starting from the project root 9 | if (packageName) { 10 | const typesPackagePath = `@types/${packageName.replace(/\//g, `__`)}${rest}`; 11 | 12 | let unqualified; 13 | try { 14 | unqualified = pnp.resolveToUnqualified(typesPackagePath, issuer, {considerBuiltins: false}); 15 | } catch (error) {} 16 | 17 | if (unqualified) { 18 | // TypeScript checks whether the directory of the candidate is a directory 19 | // which may cause issues w/ zip loading (since the zip archive is still 20 | // reported as a file). To workaround this we add a trailing slash, which 21 | // causes TypeScript to assume the parent is a directory. 22 | if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified)) 23 | unqualified += `/`; 24 | 25 | const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost); 26 | 27 | if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) { 28 | return finalResolution; 29 | } else { 30 | failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations); 31 | } 32 | } 33 | } 34 | 35 | // Then we try on "package-name", this time starting from the package that makes the request 36 | if (true) { 37 | const regularPackagePath = `${packageName || ``}${rest}`; 38 | 39 | let unqualified; 40 | try { 41 | unqualified = pnp.resolveToUnqualified(regularPackagePath, issuer, {considerBuiltins: false}); 42 | } catch (error) {} 43 | 44 | if (unqualified) { 45 | // TypeScript checks whether the directory of the candidate is a directory 46 | // which may cause issues w/ zip loading (since the zip archive is still 47 | // reported as a file). To workaround this we add a trailing slash, which 48 | // causes TypeScript to assume the parent is a directory. 49 | if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified)) 50 | unqualified += `/`; 51 | 52 | const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost); 53 | 54 | if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) { 55 | return finalResolution; 56 | } else { 57 | failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations); 58 | } 59 | } 60 | } 61 | 62 | return { 63 | resolvedModule: undefined, 64 | resolvedTypeReferenceDirective: undefined, 65 | failedLookupLocations, 66 | }; 67 | } 68 | 69 | module.exports.resolveModuleName = process.versions.pnp 70 | ? resolveModuleName 71 | : (moduleName, containingFile, compilerOptions, compilerHost, resolveModuleName) => 72 | resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost); 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Plug'n'Play](https://github.com/yarnpkg/rfcs/pull/101) resolver for TypeScript 2 | 3 | [![npm version](https://img.shields.io/npm/v/ts-pnp.svg)](https://www.npmjs.com/package/ts-pnp) 4 | [![node version](https://img.shields.io/node/v/ts-pnp.svg)](https://www.npmjs.com/package/ts-pnp) 5 | 6 | *This plugin is also available for Webpack ([pnp-webpack-plugin](https://github.com/arcanis/pnp-webpack-plugin)), Jest ([jest-pnp-resolver](https://github.com/arcanis/jest-pnp-resolver)), and Rollup ([rollup-plugin-pnp-resolve](https://github.com/arcanis/rollup-plugin-pnp-resolve))* 7 | 8 | ## Installation 9 | 10 | ``` 11 | yarn add -D ts-pnp 12 | ``` 13 | 14 | ## Usage 15 | 16 | *Note that `ts-pnp` is a low-level package - you shouldn't have to use it unless you're writing a TS compiler host. If you just want to write TypeScript and have it Just Work™, take a look at [`pnp-webpack-plugin`](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration) instead.* 17 | 18 | This package exports a function that can be used to implement the [`resolveModuleName` hook from `CompilerHost`](https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#customizing-module-resolution). It mimics the interface from the one you'd typically use and, as all other PnP plugins, works just fine whether your application is actually running under PnP or not. 19 | 20 | ```js 21 | import {resolveModuleName} from 'ts-pnp'; 22 | import * as ts from 'typescript'; 23 | 24 | function createCompilerHost( 25 | compilerOptions: ts.CompilerOptions, 26 | ): ts.CompilerHost { 27 | const compilerHost = { 28 | resolveModuleNames, 29 | resolveTypeReferenceDirectives, 30 | }; 31 | 32 | return compilerHost; 33 | 34 | function resolveModuleNames(moduleNames: string[], containingFile: string) { 35 | return moduleNames.map(moduleName => { 36 | return resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost, ts.resolveModuleName).resolvedModule; 37 | }); 38 | } 39 | 40 | function resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string) { 41 | return typeDirectiveNames.map(typeDirectiveName => { 42 | return resolveModuleName(typeDirectiveName, containingFile, compilerOptions, compilerHost, ts.resolveTypeReferenceDirective).resolvedTypeReferenceDirective; 43 | }); 44 | } 45 | } 46 | ``` 47 | 48 | ## License (MIT) 49 | 50 | > **Copyright © 2016 Maël Nison** 51 | > 52 | > 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: 53 | > 54 | > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 55 | > 56 | > 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. 57 | --------------------------------------------------------------------------------