Skip to content

Commit 214e4be

Browse files
committed
feat: omitIndex option
closes #16
1 parent 6bb6bbe commit 214e4be

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,23 @@ import { MyService } from "/src/services";
8080
```
8181

8282
This is only in terms of resolving a dependency's name, it doesn't actually update the import path.
83+
84+
### omitIndex
85+
86+
```ts
87+
boolean;
88+
```
89+
90+
When registering a dependency in an index file, the index will be omitted.
91+
92+
For example:
93+
94+
```ts
95+
// /src/services/index.ts
96+
97+
type MyService = any;
98+
99+
jpex.service<MyService>(myService);
100+
```
101+
102+
This would normally produce a type name of `type:/src/services/index/MyService`. With omitIndex it will create `type:/src/services/MyService`.

src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const mainVisitor: Visitor<{
2020
opts: {
2121
identifier: string[],
2222
publicPath: string | boolean,
23+
omitIndex: boolean,
2324
pathAlias: {
2425
[key: string]: string,
2526
},
@@ -31,22 +32,25 @@ const mainVisitor: Visitor<{
3132
opts: {
3233
identifier = 'jpex',
3334
publicPath,
34-
pathAlias,
35+
// eslint-disable-next-line prefer-const
36+
pathAlias = {},
37+
// eslint-disable-next-line prefer-const
38+
omitIndex,
3539
} = {},
3640
} = state;
37-
const filename = this
41+
let filename = this
3842
.filename
3943
.split('.')
4044
.slice(0, -1)
4145
.join('.')
4246
.replace(process.cwd(), '');
47+
if (omitIndex && filename.endsWith('/index')) {
48+
filename = filename.replace('/index', '');
49+
}
4350
identifier = [].concat(identifier);
4451
if (publicPath === true) {
4552
publicPath = require(resolve('./package.json')).name;
4653
}
47-
if (pathAlias == null) {
48-
pathAlias = {};
49-
}
5054
const opts = {
5155
identifier,
5256
filename,

tests/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava';
2+
import { transformAsync } from '@babel/core';
3+
4+
test('index', async(t) => {
5+
const code = `
6+
// transforms code
7+
import jpex from 'jpex';
8+
9+
type Foo = string;
10+
type Baz = () => string;
11+
type Bar = number;
12+
13+
jpex.factory<Foo>(() => 'foo');
14+
jpex.constant<Bar>(44);
15+
jpex.factory<Baz>((foo: Foo, bar: Bar) => () => \`\${foo}\${bar}\`);
16+
17+
const result = jpex.resolve<Baz>();
18+
`;
19+
const { code: actual } = await transformAsync(code, {
20+
filename: './code/index.ts',
21+
babelrc: false,
22+
configFile: false,
23+
presets: [
24+
'@babel/preset-typescript',
25+
],
26+
plugins: [
27+
[
28+
'./dist',
29+
{
30+
omitIndex: true,
31+
},
32+
],
33+
],
34+
});
35+
36+
t.snapshot(actual);
37+
});

tests/snapshots/index.ts.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Snapshot report for `tests/index.ts`
2+
3+
The actual snapshot is saved in `index.ts.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## index
8+
9+
> Snapshot 1
10+
11+
`// transforms code␊
12+
import jpex from 'jpex';␊
13+
jpex.factory("type:/code/Foo", [], () => 'foo');␊
14+
jpex.constant("type:/code/Bar", 44);␊
15+
jpex.factory("type:/code/Baz", ["type:/code/Foo", "type:/code/Bar"], (foo, bar) => () => `${foo}${bar}`);␊
16+
const result = jpex.resolve("type:/code/Baz");`

tests/snapshots/index.ts.snap

263 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)