Skip to content

Commit

Permalink
Merge pull request #2 from frankleng/support_tsc_comments
Browse files Browse the repository at this point in the history
support comments in tsconfig.json
  • Loading branch information
frankleng authored Feb 2, 2022
2 parents f054af9 + d6d4336 commit 80a4135
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# esbuild-ts-paths
Transform TS path alias to absolute paths for esbuild
###Example
```javascript
// tsconfig.json

"compilerOptions": {
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"@common/*": ["../common/*"], // asterisks are important
"@shared/*": ["./src/shared/*"]
}
}

```

```javascript
// esbuild
const tsPaths = require("esbuild-ts-paths")
esbuild.build({
//...
Expand All @@ -12,3 +27,4 @@ esbuild.build({
]
})
```

14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const { sync: glob } = require("fast-glob");
const path = require("path");
const fs = require('fs');

function stripJsonComments (data) {
const re = new RegExp("\/\/(.*)","g");
return data.replace(re,'');
}

module.exports = (relativeTsconfigPath = './tsconfig.json') => {
const absTsconfigPath = path.resolve(process.cwd(), relativeTsconfigPath);
let tsconfigData = fs.readFileSync(absTsconfigPath, 'utf8');
tsconfigData = stripJsonComments(tsconfigData);
const {compilerOptions} = JSON.parse(tsconfigData);

module.exports = (tsconfigPath = './tsconfig.json') => {
const { compilerOptions } = require(path.resolve(process.cwd(), tsconfigPath));
const pathKeys = Object.keys(compilerOptions.paths);
const re = new RegExp(`^(${pathKeys.join("|")})`);
return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esbuild-ts-paths",
"version": "1.0.3",
"version": "1.0.4",
"description": "Transform TS path alias to absolute paths for esbuild",
"main": "index.js",
"repository": "git@github.com:frankleng/esbuild-ts-paths.git",
Expand Down

0 comments on commit 80a4135

Please sign in to comment.