Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arzyu committed Sep 10, 2019
0 parents commit 48c4b7d
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# resolve-ts-aliases

Resolve aliases for integrating webpack and typescript.

## Install

```bash
npm add -D resolve-ts-aliases
```

## Usage

`tsconfig.json`:

```json
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@app/*": ["src/app/*"],
"@components/*": ["src/components/*"]
}
}
}
```

`webpack.config.ts`:

```ts
import * as path from "path";
import * as webpack from "webpack";
import { resolveTsAliases } from "webpack-ts-utils";

const config: webpack.Configuration = {
resolve: {
// These 2 forms are equivalent.
alias: resolveTsAliases(path.resolve("tsconfig.json")),
alias: {
"@app": "/path/to/src/app",
"@components": "/path/to/src/components"
}
},
};

export default config;
```
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "resolve-ts-aliases",
"version": "1.0.0",
"description": "Resolve aliases for integrating webpack and typescript.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "rm -rf dist && tsc",
"prepare": "npm run build",
"release": "npm publish --access public"
},
"devDependencies": {
"@types/node": "^12.7.4",
"typescript": "^3.6.2"
},
"repository": {
"type": "git",
"url": "https://github.com/arzyu/resolve-ts-aliases"
},
"license": "MIT"
}
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { resolve, dirname} from "path";

export const resolveTsAliases = (tsconfigPath: string) => {
const { baseUrl, paths } = require(tsconfigPath).compilerOptions;
const pathPrefix = resolve(dirname(tsconfigPath), baseUrl);
const aliases: { [key: string]: string } = {};

Object.keys(paths).forEach((item) => {
const name = item.replace("/*", "");
const value = resolve(pathPrefix, paths[item][0].replace("/*", ""));

aliases[name] = value;
});

return aliases;
};
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"declaration": true,
"baseUrl": "./",
"outDir": "./dist"
},
"files": ["src/index.ts"]
}

0 comments on commit 48c4b7d

Please sign in to comment.