({tsconfigPath})
| 11 | const {parseTsconfig, createPathsMatcher} = require('get-tsconfig'); |
| 12 | |
| 13 | function pathPlugin({tsconfigPath}) { |
| 14 | if (tsconfigPath === undefined) { |
| 15 | throw Error('A path tsconfig file must be provided.'); |
| 16 | } |
| 17 | const fullTsconfigPath = join(process.cwd(), tsconfigPath); |
| 18 | const tsconfig = parseTsconfig(fullTsconfigPath); |
| 19 | const pathMappingMatcher = createPathsMatcher({config: tsconfig, path: fullTsconfigPath}); |
| 20 | return { |
| 21 | name: 'paths', |
| 22 | resolveId: (source) => { |
| 23 | /** |
| 24 | * A list containing all of the potential paths which match the provided source based |
| 25 | * on the paths field from the tsconfig. |
| 26 | */ |
| 27 | const matchedSources = pathMappingMatcher(source); |
| 28 | if (matchedSources.length == 0) { |
| 29 | return null; |
| 30 | } |
| 31 | // We need to check each matched source to see if it loads at the path directly, or is |
| 32 | // a directory with an index.js file to import. |
| 33 | for (let matchedSource of matchedSources) { |
| 34 | const indexPath = join(matchedSource, 'index.js'); |
| 35 | if (existsSync(indexPath) && statSync(indexPath).isFile) { |
| 36 | return {id: indexPath}; |
| 37 | } |
| 38 | const filePath = matchedSource + '.js'; |
| 39 | if (existsSync(filePath) && statSync(filePath).isFile) { |
| 40 | return {id: filePath}; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | throw Error(`Cannot find ${source}\nLocations checked:\n-${matchedSources.join('\n')}`); |
| 45 | }, |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | module.exports = {pathPlugin}; |
no test coverage detected
searching dependent graphs…