Skip to content

Commit

Permalink
feat: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Aug 30, 2021
0 parents commit c1e244d
Show file tree
Hide file tree
Showing 36 changed files with 30,883 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
["@babel/preset-env", { "targets": "maintained node versions" }],
"@babel/preset-typescript"
]
}
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "react-app"
}
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Release
on:
push:
branches:
- main
- alpha

jobs:
release:
name: Release
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'

- name: Upgrade NPM
run: npm install -g npm@latest

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint

- name: Run tests
run: npm t -- --coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test
on:
- push

jobs:
release:
name: Test
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'

- name: Upgrade NPM
run: npm install -g npm@latest

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint

- name: Run tests
run: npm t -- --coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
/coverage
/dist
/examples/**/dist
17 changes: 17 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.github
.DS_Store
coverage
examples
node_modules
scripts
src
.babelrc
.eslintrc
.gitignore
.npmignore
.releaserc.json
jest.config.js
package-lock.json
renovate.json
tsconfig-build.json
tsconfig.json
3 changes: 3 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"branches": ["main", { "name": "alpha", "prerelease": true }]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Rico Kahler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
129 changes: 129 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# babel-plugin-tsconfig-paths-module-resolver

[![npm](https://badgen.net/npm/v/babel-plugin-tsconfig-paths-module-resolver)](https://www.npmjs.com/package/babel-plugin-tsconfig-paths-module-resolver) [![Github Actions](https://badgen.net/github/checks/ricokahler/babel-plugin-tsconfig-paths-module-resolver)](https://github.com/ricokahler/babel-plugin-tsconfig-paths-module-resolver/actions) [![codecov](https://codecov.io/gh/ricokahler/babel-plugin-tsconfig-paths-module-resolver/branch/main/graph/badge.svg?token=2cQuXwu8Gq)](https://codecov.io/gh/ricokahler/babel-plugin-tsconfig-paths-module-resolver) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

> Combines [`babel-plugin-module-resolver`][0] and [`tsconfig-paths`][1] to make a babel plugin that resolves [tsconfig paths][2].
This library is a re-export of [`babel-plugin-module-resolver`](https://github.com/tleunen/babel-plugin-module-resolver) pre-configured with [tsconfig paths][2] support via the package [`tsconfig-paths`][1].

It aims to be stable by relying on these already widely-used packages to power the heavy logic:

| dependency | weekly downloads |
| ----------------------------------- | -------------------------------------------------------- |
| [`babel-plugin-module-resolver`][0] | [![babel plugin module resolver weekly downloads][3]][4] |
| [`tsconfig-paths`][1] | [![tsconfig-paths weekly downloads][5]][6] |

These dependencies are automatically updated via [renovate bot](https://github.com/renovatebot/renovate) and [semantic release](https://github.com/semantic-release/semantic-release).

## Installation

```
npm install --save-dev babel-plugin-tsconfig-paths-module-resolver
```

or

```
yarn add --dev babel-plugin-tsconfig-paths-module-resolver
```

Specify the plugin in your `.babelrc` (or [equivalent configuration file](https://babeljs.io/docs/en/config-files#configuration-file-types)).

```js
{
"presets": [
// ...
"@babel/preset-typescript",
// ...
],
"plugins": [
// add this to your babel config file in `plugins`
// 👇👇👇
"tsconfig-paths-module-resolver"
// 👆👆👆
// ...
]
}
```

## Advanced usage

`babel-plugin-tsconfig-paths-module-resolver` accepts the same options as [`babel-plugin-module-resolver`](https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md).

You can supply those extra options in your babel configuration file like so:

```js
{
"presets": [
// ...
"@babel/preset-typescript",
// ...
],
"plugins": [
// ...
[
"tsconfig-paths-module-resolver",
// add extra options here
// 👇👇👇
{
// see here:
// https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md
}
// 👆👆👆
]
]
};
```

### `resolvePath` and `createResolvePath`

[`babel-plugin-module-resolver`][0] includes [a configuration option](https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath) to allow you to programmatically resolve your imports.

This plugin provides a `resolvePath` implementation powered by [`tsconfig-paths`][1]. If you'd like to implement your own `resolvePath` implementation while still utilizing this plugin's default implementation, you can separately import `createResolvePath` that returns a `resolvePath` implementation.

```js
const createResolvePath = require('babel-plugin-tsconfig-paths-module-resolver/create-resolve-path');
const defaultResolvePath = createResolvePath();

/**
* @param sourceFile {string} the input source path
* @param currentFile {string} the absolute path of the current file
* @param opts {any} the options as passed to the Babel config
* @return {string}
*/
function customResolvePath(sourceFile, currentFile, opts) {
// ...
const result = defaultResolvePath(sourceFile, currentFile, opts);
// ...

return result;
}

// .babelrc.js
module.exports = {
presets: [
// ...
'@babel/preset-typescript',
// ...
],
plugins: [
// ...
[
'tsconfig-paths-module-resolver',
{
// 👇👇👇
resolvePath: customResolvePath,
// 👆👆👆
},
],
],
};
```

[0]: https://github.com/tleunen/babel-plugin-module-resolver
[1]: https://github.com/dividab/tsconfig-paths
[2]: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
[3]: https://badgen.net/npm/dw/babel-plugin-module-resolver
[4]: https://www.npmjs.com/package/babel-plugin-module-resolver
[5]: https://badgen.net/npm/dw/tsconfig-paths
[6]: https://www.npmjs.com/package/tsconfig-paths
2 changes: 2 additions & 0 deletions create-resolve.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createResolvePath } from './dist';
export = createResolvePath;
2 changes: 2 additions & 0 deletions create-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { createResolvePath } = require('./dist');
module.exports = createResolvePath;
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this folder was made to hold examples used primarily for testing.

the `index.test.ts` file compiles each example and imports the finished result.
30 changes: 30 additions & 0 deletions examples/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { exec } from 'child_process';
import { promisify } from 'util';

const execute = promisify(exec);

jest.setTimeout(30 * 1000);

beforeAll(async () => {
const build = await execute('npm run build');
if (build.stderr) console.error(build.stderr);

const buildExamples = await execute('npm run build-examples');
if (buildExamples.stderr) console.error(buildExamples.stderr);
});

describe('examples', () => {
test('standard', () => {
const standard = jest.requireActual(
'@babel-plugin-tsconfig-paths-module-resolver/standard',
).default;

expect(standard).toMatchInlineSnapshot(`
Array [
"from src/bar/a",
"from src/foo/b",
"from umbrella/baz/c",
]
`);
});
});
7 changes: 7 additions & 0 deletions examples/standard/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [
'@babel/preset-typescript',
['@babel/preset-env', { targets: { node: true } }],
],
plugins: [require.resolve('../..')],
};
10 changes: 10 additions & 0 deletions examples/standard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@babel-plugin-tsconfig-paths-module-resolver/standard",
"private": true,
"version": "1.0.0",
"main": "./dist/index.js",
"scripts": {
"build": "babel -x .ts ./src --out-dir ./dist"
},
"license": "MIT"
}
1 change: 1 addition & 0 deletions examples/standard/src/bar/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from src/bar/a';
1 change: 1 addition & 0 deletions examples/standard/src/foo/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from src/foo/b';
5 changes: 5 additions & 0 deletions examples/standard/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import a from '@/bar/a';
import b from '@/foo/b';
import c from '@/baz/c';

export default [a, b, c];
1 change: 1 addition & 0 deletions examples/standard/src/umbrella/baz/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from umbrella/baz/c';
13 changes: 13 additions & 0 deletions examples/standard/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@/*": ["umbrella/*", "*"]
}
},
"include": ["./src/**/*.ts"],
"exclude": ["**/node_modules"]
}
Loading

0 comments on commit c1e244d

Please sign in to comment.