├── src ├── options.ts ├── rollup-plugin-ts-paths.ts └── tsconfig.ts ├── .npmignore ├── .gitignore ├── rollup.config.js ├── tsconfig.json ├── LICENSE ├── package.json └── README.md /src/options.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | tsConfigDirectory?: string; 3 | } -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .git/ 3 | src/ 4 | tsconfig.json 5 | rollup.config.js 6 | .rpt2_cache/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Dependency directories 9 | node_modules/ 10 | 11 | # Output directories 12 | dist/ 13 | .rpt2_cache/ 14 | -------------------------------------------------------------------------------- /src/rollup-plugin-ts-paths.ts: -------------------------------------------------------------------------------- 1 | 2 | import { Plugin } from 'rollup'; 3 | import { Options } from './options'; 4 | import { getPathsFromTsConfig } from './tsconfig'; 5 | 6 | export default function rollupPluginTsPaths(options: Options = {}): Plugin { 7 | const paths = getPathsFromTsConfig(options); 8 | 9 | return { 10 | name: 'rollup-plugin-ts-paths', 11 | resolveId(importee: string) { 12 | return paths[importee]; 13 | } 14 | }; 15 | } -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import typescript from 'rollup-plugin-typescript2'; 2 | import nodeResolve from '@rollup/plugin-node-resolve'; 3 | import commonjs from '@rollup/plugin-commonjs'; 4 | import pkg from './package.json'; 5 | 6 | export default [{ 7 | input: 'src/rollup-plugin-ts-paths.ts', 8 | output: [{ 9 | file: pkg.main, 10 | format: 'cjs' 11 | }, { 12 | file: pkg.module, 13 | format: 'es' 14 | }], 15 | plugins: [ 16 | typescript(), 17 | nodeResolve(), 18 | commonjs() 19 | ], 20 | external: ['fs', 'path'] 21 | }]; -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "diagnostics": true, 5 | "downlevelIteration": false, 6 | "lib": ["es5", "es6", "dom"], 7 | "noEmitOnError": true, 8 | "noImplicitAny": true, 9 | "noImplicitThis": true, 10 | "noUnusedLocals": true, 11 | "noUnusedParameters": true, 12 | "pretty": true, 13 | "skipLibCheck": true, 14 | "strictPropertyInitialization": true, 15 | "strictNullChecks": true, 16 | "target": "es5", 17 | "declaration": true 18 | }, 19 | "include": [ 20 | "src" 21 | ], 22 | "exclude": [ 23 | "dist", 24 | "node_modules" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 bitshiftza 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-ts-paths", 3 | "version": "1.0.5", 4 | "description": "", 5 | "main": "dist/rollup-plugin-ts-paths.cjs.js", 6 | "module": "dist/rollup-plugin-ts-paths.es.js", 7 | "typings": "dist/rollup-plugin-ts-paths.d.ts", 8 | "scripts": { 9 | "build": "npx rollup -c rollup.config.js", 10 | "test": "" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/bitshiftza/rollup-plugin-ts-paths.git" 15 | }, 16 | "author": "bitshiftza", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/bitshiftza/rollup-plugin-ts-paths/issues" 20 | }, 21 | "homepage": "https://github.com/bitshiftza/rollup-plugin-ts-paths#readme", 22 | "keywords": [ 23 | "rollup", 24 | "plugin", 25 | "typescript", 26 | "paths" 27 | ], 28 | "devDependencies": { 29 | "@rollup/plugin-commonjs": "^11.0.2", 30 | "@rollup/plugin-node-resolve": "^7.1.1", 31 | "rollup": "^1.31.0", 32 | "rollup-plugin-typescript2": "^0.25.3", 33 | "typescript": "^3.7.5" 34 | }, 35 | "dependencies": { 36 | "strip-bom": "^4.0.0", 37 | "strip-json-comments": "^3.0.1" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-ts-paths 2 | 3 | Replace alias with resolved import from paths in tsconfig.json. Let's you use rollup to bundle/transpile code generated by `tsc` like this 4 | 5 | ```js 6 | // src/path/to/foo/foot.ts 7 | import { Foo } from 'foo'; 8 | ``` 9 | where foo is defined in `tsconfig.json` 10 | 11 | ```json 12 | { 13 | "compilerOptions": { 14 | "paths": { 15 | "foo": [ 16 | "src/path/to/foo/foo.ts" 17 | ] 18 | } 19 | } 20 | } 21 | 22 | 23 | ``` 24 | 25 | ## Installation 26 | 27 | ```bash 28 | npm install --save-dev rollup-plugin-ts-paths 29 | ``` 30 | 31 | 32 | ## Usage 33 | 34 | Generally, you need to ensure that rollup-plugin-ts-paths goes *before* other things (like rollup-plugin-commonjs) in your `plugins` array, so that the correc file can be resolved from an import. 35 | 36 | 37 | ```js 38 | // rollup.config.js 39 | import tsConfigPaths from 'rollup-plugin-ts-paths'; 40 | 41 | export default { 42 | // ... 43 | plugins: [ 44 | tsConfigPaths() 45 | ] 46 | }; 47 | ``` 48 | 49 | 50 | ## Options 51 | 52 | ```js 53 | { 54 | // The directory the TS config file can be found in (optional) 55 | tsConfigDirectory: processs.cwd() 56 | } 57 | ``` 58 | 59 | 60 | ## Limitations 61 | 62 | Currently on the first entry of a path specified in a `tsconfig.json` is supported. 63 | 64 | 65 | ## License 66 | 67 | MIT -------------------------------------------------------------------------------- /src/tsconfig.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import path from 'path'; 3 | import stripBom from 'strip-bom'; 4 | import stripComments from 'strip-json-comments'; 5 | import { Options } from './options'; 6 | 7 | function readTsConfig(directory: string) { 8 | const tsConfigPath = path.join(directory, 'tsconfig.json'); 9 | if (!fs.existsSync(tsConfigPath)) { 10 | throw new Error(`[rollup-plugin-ts-paths] Cannot find TS config in "${directory}"`); 11 | } 12 | 13 | const contents = fs.readFileSync(tsConfigPath, 'utf-8'); 14 | const data = stripComments(stripBom(contents)) 15 | 16 | // A tsconfig.json file is permitted to be completely empty. 17 | if (/^\s*$/.test(data)) { 18 | return {} 19 | } 20 | 21 | return JSON.parse(data); 22 | } 23 | 24 | function findTsConfig() { 25 | let root = process.cwd(); 26 | 27 | do { 28 | const tsConfigPath = path.join(root, 'tsconfig.json'); 29 | if (fs.existsSync(tsConfigPath)) { 30 | return root; 31 | } 32 | 33 | root = path.normalize(path.join(root, '..')); 34 | } while (root !== '/'); 35 | 36 | return undefined; 37 | } 38 | 39 | export interface Paths { 40 | [key: string]: string; 41 | } 42 | 43 | export function getPathsFromTsConfig(options: Options): Paths { 44 | const baseDirectory = options.tsConfigDirectory || findTsConfig(); 45 | if (!baseDirectory) { 46 | throw new Error('[rollup-plugin-ts-paths] Cannot resolve TS config file'); 47 | } 48 | 49 | const config = { 50 | baseDirectory, 51 | tsConfig: readTsConfig(baseDirectory) 52 | }; 53 | 54 | const outDir = path.join(config.baseDirectory, config.tsConfig.compilerOptions.outDir); 55 | const tsConfigPaths = config.tsConfig.compilerOptions.paths || {}; 56 | const paths: Paths = {}; 57 | 58 | Object.keys(tsConfigPaths).forEach(key => { 59 | paths[key] = path.join(outDir, tsConfigPaths[key][0].replace('.ts', '.js')) 60 | }); 61 | 62 | return paths; 63 | } 64 | --------------------------------------------------------------------------------