Skip to content

Commit 75a7f82

Browse files
committed
initial commit
0 parents  commit 75a7f82

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
indent_style = space
13+
trim_trailing_whitespace = false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
dist

index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { CompilerOptions, findConfigFile, nodeModuleNameResolver, sys } from 'typescript';
2+
3+
export const resolveTypescriptPaths = (options: Options = {}) => {
4+
const compilerOptions = getCompilerOptions(options.tsConfigPath);
5+
6+
return {
7+
name: 'resolve-typescript-paths',
8+
resolveId: (importee: string, importer: string) => {
9+
if (!compilerOptions.paths || !compilerOptions.paths[importee]) {
10+
return null;
11+
}
12+
13+
const { resolvedModule } = nodeModuleNameResolver(importee, importer, compilerOptions, sys);
14+
15+
if (!resolvedModule) {
16+
return null;
17+
}
18+
19+
const { resolvedFileName } = resolvedModule;
20+
21+
if (!resolvedFileName || resolvedFileName.endsWith('.d.ts')) {
22+
return null;
23+
}
24+
25+
return resolvedFileName;
26+
},
27+
};
28+
};
29+
30+
const getCompilerOptions = (configPath = findConfigFile('./', sys.fileExists)): CompilerOptions => {
31+
if (!configPath) {
32+
return {};
33+
}
34+
35+
const configJson = sys.readFile(configPath);
36+
37+
if (!configJson) {
38+
return {};
39+
}
40+
41+
const config: { compilerOptions?: CompilerOptions } = JSON.parse(configJson);
42+
43+
if (!config || !config.compilerOptions) {
44+
return {};
45+
}
46+
47+
return config.compilerOptions;
48+
};
49+
50+
export interface Options {
51+
tsConfigPath?: string;
52+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "rollup-plugin-typescript-paths",
3+
"version": "1.0.0",
4+
"description": "Rollup Plugin to automatically resolve path aliases set in the compilerOptions section of tsconfig.json.",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "tsc",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"author": "Simon Haenisch (https://github.com/simonhaenisch)",
12+
"repository": "simonhaenisch/rollup-plugin-typescript-paths",
13+
"keywords": [
14+
"rollup-plugin",
15+
"typescript"
16+
],
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/simonhaenisch/rollup-plugin-typescript-paths/issues"
20+
},
21+
"homepage": "https://github.com/simonhaenisch/rollup-plugin-typescript-paths#readme",
22+
"devDependencies": {
23+
"typescript": "^3.4.5"
24+
},
25+
"peerDependencies": {
26+
"typescript": ">=3.x"
27+
},
28+
"prettier": {
29+
"printWidth": 120,
30+
"singleQuote": true,
31+
"tabWidth": 2,
32+
"trailingComma": "all",
33+
"useTabs": true,
34+
"bracketSpacing": true
35+
}
36+
}

readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# rollup-plugin-typescript-paths
2+
3+
Rollup Plugin to automatically resolve path aliases set in the `compilerOptions` section of `tsconfig.json`.
4+
5+
For example, if you have
6+
7+
```js
8+
// tsconfig.json
9+
{
10+
"compilerOptions": {
11+
"baseUrl": ".",
12+
"paths": {
13+
"@utils": ["src/helpers/utils"]
14+
}
15+
}
16+
}
17+
```
18+
19+
```js
20+
import { something } from '@utils';
21+
```
22+
23+
Then this plugin will make sure that rollup knows how to resolve `@utils`.
24+
25+
## Installation
26+
27+
```
28+
npm install --save-dev rollup-plugin-typescript-paths
29+
```
30+
31+
## Usage
32+
33+
```js
34+
import { resolveTypescriptPaths } from 'rollup-plugin-typescript-paths';
35+
36+
export default {
37+
// ...
38+
plugins: [
39+
resolveTypescriptPaths()
40+
]
41+
}
42+
```
43+
44+
## Options
45+
46+
* **`tsConfigPath`:** If the plugin has trouble finding your `tsconfig.json`, you can pass the path to it via this option.
47+
48+
## License
49+
50+
[MIT](/license).
51+

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"lib": ["esnext"],
6+
"declaration": true,
7+
"outDir": "dist",
8+
9+
"strict": true,
10+
"noUnusedLocals": true,
11+
"noUnusedParameters": true,
12+
"noImplicitReturns": true,
13+
"noFallthroughCasesInSwitch": true,
14+
"esModuleInterop": true
15+
}
16+
}

0 commit comments

Comments
 (0)