@monkin/di 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +276 -0
- package/dist/di.d.ts +59 -0
- package/dist/di.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrey Monkin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# @monkin/di
|
|
2
|
+
|
|
3
|
+
[](https://github.com/monkin/di/actions/workflows/test.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@monkin/di)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
`@monkin/di` is a lightweight (472 bytes), type-safe dependency injection container for TypeScript. It leverages TypeScript's advanced type system to provide a fluent API for service registration and resolution with full type safety and autocompletion.
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
- [Features](#features)
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [1. Defining a Service](#1-defining-a-service)
|
|
15
|
+
- [2. Basic Injection](#2-basic-injection)
|
|
16
|
+
- [3. Services with Dependencies](#3-services-with-dependencies)
|
|
17
|
+
- [4. Merging Containers](#4-merging-containers)
|
|
18
|
+
- [5. Lazy](#5-lazy)
|
|
19
|
+
- [6. Duplicate Service Name Protection](#6-duplicate-service-name-protection)
|
|
20
|
+
- [7. Reserved Field Names](#7-reserved-field-names)
|
|
21
|
+
- [8. Circular Dependencies](#8-circular-dependencies)
|
|
22
|
+
- [API Reference](#api-reference)
|
|
23
|
+
- [Development](#development)
|
|
24
|
+
- [License](#license)
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- **Full Type Safety**: Get autocompletion and type checks for all your injected services.
|
|
29
|
+
- **No Decorators**: No need for `reflect-metadata` or experimental decorators. Pure TypeScript.
|
|
30
|
+
- **Fluent API**: Chainable service registration makes it easy to compose your container.
|
|
31
|
+
- **Container Composition**: Merge multiple containers together to share dependencies across different parts of your application.
|
|
32
|
+
- **Lazy**: Services are instantiated only on demand (when first accessed) and reused for subsequent accesses.
|
|
33
|
+
- **Zero Runtime Dependencies**: Extremely lightweight (472 bytes minified / 323 bytes gzipped).
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install @monkin/di
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### 1. Defining a Service
|
|
44
|
+
|
|
45
|
+
A service is a class that implements the `DiService` interface. It must implement a `getServiceName()` method which will be used as the key in the container. Use `as const` to ensure the name is treated as a literal type.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { DiService } from '@monkin/di';
|
|
49
|
+
|
|
50
|
+
export class LoggerService implements DiService<"logger"> {
|
|
51
|
+
getServiceName() {
|
|
52
|
+
return "logger" as const;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
log(message: string) {
|
|
56
|
+
console.log(`[LOG]: ${message}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2. Basic Injection
|
|
62
|
+
|
|
63
|
+
Use `DiContainer` to register and resolve your services. You can register a single service or multiple services in one call. When registering multiple services, the order doesn't matter; they can even depend on each other.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { DiContainer } from '@monkin/di';
|
|
67
|
+
import { LoggerService } from './LoggerService';
|
|
68
|
+
import { ConfigService } from './ConfigService';
|
|
69
|
+
|
|
70
|
+
// Single service
|
|
71
|
+
const container = new DiContainer()
|
|
72
|
+
.inject(LoggerService);
|
|
73
|
+
|
|
74
|
+
// Multiple services in one call (order-independent)
|
|
75
|
+
const multiContainer = new DiContainer()
|
|
76
|
+
.inject(ConfigService, LoggerService);
|
|
77
|
+
|
|
78
|
+
// Access the service directly on the container
|
|
79
|
+
container.logger.log("Service is ready!");
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. Services with Dependencies
|
|
83
|
+
|
|
84
|
+
To inject dependencies into a service, define its constructor to accept the container. You can use the `Di<...T>` type helper to specify which services are required. It supports multiple services passed as separate arguments or as a tuple.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { Di, DiService } from '@monkin/di';
|
|
88
|
+
import { LoggerService } from './LoggerService';
|
|
89
|
+
import { ConfigService } from './ConfigService';
|
|
90
|
+
|
|
91
|
+
export class UserService implements DiService<"user"> {
|
|
92
|
+
getServiceName() {
|
|
93
|
+
return "user" as const;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Single dependency:
|
|
97
|
+
// constructor(private di: Di<LoggerService>) {}
|
|
98
|
+
|
|
99
|
+
// Multiple dependencies:
|
|
100
|
+
constructor(private di: Di<LoggerService, ConfigService>) {}
|
|
101
|
+
|
|
102
|
+
getUser(id: string) {
|
|
103
|
+
const prefix = this.di.config.get("userPrefix");
|
|
104
|
+
this.di.logger.log(`Fetching user: ${prefix}${id}`);
|
|
105
|
+
return { id, name: "User " + id };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const container = new DiContainer()
|
|
110
|
+
.inject(LoggerService)
|
|
111
|
+
.inject(ConfigService)
|
|
112
|
+
.inject(UserService);
|
|
113
|
+
|
|
114
|
+
container.user.getUser("42");
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
When using `inject` with multiple services, they can depend on each other regardless of the order they are passed to the method.
|
|
118
|
+
|
|
119
|
+
### 4. Merging Containers
|
|
120
|
+
|
|
121
|
+
You can create specialized containers and merge them into a main container using `injectContainer`.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const authContainer = new DiContainer().inject(AuthService);
|
|
125
|
+
const apiContainer = new DiContainer().inject(ApiService);
|
|
126
|
+
|
|
127
|
+
const appContainer = new DiContainer()
|
|
128
|
+
.injectContainer(authContainer)
|
|
129
|
+
.injectContainer(apiContainer)
|
|
130
|
+
.inject(MainApp);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 5. Lazy
|
|
134
|
+
|
|
135
|
+
Services registered via `inject` are lazy by default. When you register a service, `@monkin/di` creates a **Proxy** for it on the container. The actual service instance is only created when you first interact with it (e.g., call a method, access a property). Once created, the same instance is reused for all subsequent accesses.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
const container = new DiContainer()
|
|
139
|
+
.inject(ExpensiveService);
|
|
140
|
+
|
|
141
|
+
// ExpensiveService is NOT instantiated yet
|
|
142
|
+
const service = container.expensive;
|
|
143
|
+
// Still NOT instantiated! `service` is a Proxy.
|
|
144
|
+
|
|
145
|
+
console.log("Container ready");
|
|
146
|
+
|
|
147
|
+
// ExpensiveService is instantiated NOW because we access a property/method
|
|
148
|
+
service.doSomething();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 6. Duplicate Service Name Protection
|
|
152
|
+
|
|
153
|
+
`@monkin/di` prevents registering multiple services with the same name. This protection works at both compile-time and runtime:
|
|
154
|
+
|
|
155
|
+
- **Type-level Check**: If you try to `inject` a service with a name that already exists in the container, TypeScript will report an error, and the resulting type will be a string literal describing the error.
|
|
156
|
+
- **Runtime Check**: The `inject` and `injectContainer` methods will throw an `Error` if a duplicate key is detected.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const container = new DiContainer()
|
|
160
|
+
.inject(LoggerService);
|
|
161
|
+
|
|
162
|
+
// TypeScript Error: Type '"Duplicate service name: logger"' ...
|
|
163
|
+
// Runtime Error: Duplicated service name: logger
|
|
164
|
+
container.inject(AnotherLoggerService);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 7. Reserved Field Names
|
|
168
|
+
|
|
169
|
+
Since `DiContainer` uses a fluent API, certain names are reserved for its internal methods and cannot be used as service names:
|
|
170
|
+
|
|
171
|
+
- `inject`
|
|
172
|
+
- `injectContainer`
|
|
173
|
+
|
|
174
|
+
Similar to duplicate names, attempting to use a reserved name will trigger both a **Type-level Check** and a **Runtime Check**.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
class InjectService implements DiService<"inject"> {
|
|
178
|
+
getServiceName() { return "inject" as const; }
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const container = new DiContainer();
|
|
182
|
+
|
|
183
|
+
// TypeScript Error: Type '"Reserved field name: inject"' ...
|
|
184
|
+
// Runtime Error: Reserved service name: inject
|
|
185
|
+
container.inject(InjectService);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 8. Circular Dependencies
|
|
189
|
+
|
|
190
|
+
`@monkin/di` supports circular dependencies between services because it uses **Proxies** for lazy initialization. A service can depend on another service that depends back on it, provided that they don't try to access each other's methods or properties in their constructors.
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
class ServiceA implements DiService<"a"> {
|
|
194
|
+
getServiceName() { return "a" as const; }
|
|
195
|
+
constructor(private di: Di<ServiceB>) {}
|
|
196
|
+
|
|
197
|
+
doA() {
|
|
198
|
+
console.log("A doing something...");
|
|
199
|
+
this.di.b.doB();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
class ServiceB implements DiService<"b"> {
|
|
204
|
+
getServiceName() { return "b" as const; }
|
|
205
|
+
constructor(private di: Di<ServiceA>) {}
|
|
206
|
+
|
|
207
|
+
doB() {
|
|
208
|
+
console.log("B doing something...");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const container = new DiContainer().inject(ServiceA, ServiceB);
|
|
213
|
+
container.a.doA(); // Works fine!
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
> [!IMPORTANT]
|
|
217
|
+
> Do not access circular dependencies in the constructor, as this will trigger a stack overflow during instantiation.
|
|
218
|
+
|
|
219
|
+
## API Reference
|
|
220
|
+
|
|
221
|
+
### `DiContainer`
|
|
222
|
+
|
|
223
|
+
The main class for managing services.
|
|
224
|
+
|
|
225
|
+
- `inject(...ServiceClasses: new (di: any) => any): DiContainer`
|
|
226
|
+
Registers one or more service classes. Returns the container instance, typed with the newly added services. Each service can depend on other services provided in the same call or already present in the container.
|
|
227
|
+
- `injectContainer<DC extends DiContainer>(other: DC): this & DC`
|
|
228
|
+
Copies all services from another container into this one. Returns the container instance, typed with the merged services.
|
|
229
|
+
|
|
230
|
+
### `DiService<Name>`
|
|
231
|
+
|
|
232
|
+
An interface that your service classes must implement.
|
|
233
|
+
|
|
234
|
+
- `getServiceName(this: null): Name`
|
|
235
|
+
Must return the unique name of the service as a string literal type.
|
|
236
|
+
|
|
237
|
+
### `Di<...S>`
|
|
238
|
+
|
|
239
|
+
A utility type to help define dependencies in your service constructors.
|
|
240
|
+
|
|
241
|
+
- `Di<ServiceClass>`: Resolves to an object with the service name as the key and the service instance as the value.
|
|
242
|
+
- `Di<Service1, Service2, ...>`: Resolves to a merged object containing all specified services.
|
|
243
|
+
|
|
244
|
+
## Development
|
|
245
|
+
|
|
246
|
+
### Installation
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm install
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Build
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
npm run build
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Test
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
npm test
|
|
262
|
+
npm run test:watch # Watch mode
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Linting & Formatting
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
npm run lint # Run Biome check (lint, format, and import sorting)
|
|
269
|
+
npm run format # Format code with Biome
|
|
270
|
+
npm run format:check # Check code formatting with Biome
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## License
|
|
274
|
+
|
|
275
|
+
MIT
|
|
276
|
+
|
package/dist/di.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base interface for services in the DI system.
|
|
3
|
+
* The `name` property is used as the key when the service is injected into a DiContainer.
|
|
4
|
+
*/
|
|
5
|
+
export interface DiService<Name extends string> {
|
|
6
|
+
/**
|
|
7
|
+
* The name of the service.
|
|
8
|
+
* This is used as the key when the service is injected into a DiContainer.
|
|
9
|
+
*
|
|
10
|
+
* The method is called without an instance context, so it can be used as a static property.
|
|
11
|
+
*/
|
|
12
|
+
getServiceName(this: null): Name;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A type transformation that converts one or more Services (passed as separate
|
|
16
|
+
* arguments) into a merged mapped object type.
|
|
17
|
+
*
|
|
18
|
+
* Example: Di<LoggerService> -> { logger: LoggerService }
|
|
19
|
+
* Example: Di<ServiceA, ServiceB> -> { a: ServiceA } & { b: ServiceB }
|
|
20
|
+
*/
|
|
21
|
+
export type Di<S1, S2 = never, S3 = never, S4 = never, S5 = never, S6 = never, S7 = never, S8 = never, S9 = never, S10 = never, S11 = never, S12 = never, S13 = never, S14 = never, S15 = never, S16 = never> = ToDi<S1> & ToDi<S2> & ToDi<S3> & ToDi<S4> & ToDi<S5> & ToDi<S6> & ToDi<S7> & ToDi<S8> & ToDi<S9> & ToDi<S10> & ToDi<S11> & ToDi<S12> & ToDi<S13> & ToDi<S14> & ToDi<S15> & ToDi<S16>;
|
|
22
|
+
type ToDi<S> = [S] extends [never] ? unknown : S extends DiService<infer Name> ? {
|
|
23
|
+
[Key in Name]: S;
|
|
24
|
+
} : never;
|
|
25
|
+
type CheckReservedField<Name, T> = Name extends keyof DiContainer ? `Reserved field name: ${Name}` : T;
|
|
26
|
+
type Append<Container, Service extends DiService<string>> = Container extends object ? Service extends DiService<infer Name> ? CheckReservedField<Name, Container extends {
|
|
27
|
+
[Key in Name]: unknown;
|
|
28
|
+
} ? `Duplicate service name: ${Name}` : Container & Di<Service>> : never : Container;
|
|
29
|
+
/**
|
|
30
|
+
* A recursive type transformation that appends multiple services to a container.
|
|
31
|
+
*/
|
|
32
|
+
export type AppendAll<Container, Services extends any[]> = Container extends object ? Services extends [infer Head, ...infer Tail] ? Head extends DiService<string> ? AppendAll<Append<Container, Head>, Tail> : AppendAll<Container, Tail> : Container : Container;
|
|
33
|
+
type Merge<DI1, DI2> = DI1 extends object ? DI2 extends object ? Exclude<keyof DI1, keyof DiContainer> & Exclude<keyof DI2, keyof DiContainer> extends never ? DI1 & DI2 : `Containers have duplicated keys: ${(Exclude<keyof DI1, keyof DiContainer> & Exclude<keyof DI2, keyof DiContainer>) & string}` : DI2 : DI1;
|
|
34
|
+
/**
|
|
35
|
+
* DiContainer manages service instantiation and dependency resolution.
|
|
36
|
+
* It uses a fluent interface to chain service registrations, dynamically
|
|
37
|
+
* extending its own type with each injected service.
|
|
38
|
+
*/
|
|
39
|
+
export declare class DiContainer {
|
|
40
|
+
/**
|
|
41
|
+
* Register services.
|
|
42
|
+
* Each service can depend on all others provided in the same call.
|
|
43
|
+
*/
|
|
44
|
+
inject<S extends DiService<string>[]>(...dependencies: {
|
|
45
|
+
[K in keyof S]: new (dependencies: AppendAll<this, S>) => S[K];
|
|
46
|
+
}): AppendAll<this, S>;
|
|
47
|
+
/**
|
|
48
|
+
* Copies all service properties from another container into this one.
|
|
49
|
+
* Useful for composing containers or providing shared dependencies.
|
|
50
|
+
*
|
|
51
|
+
* @template DC - The type of the other DiContainer.
|
|
52
|
+
* @param other - The source container to copy services from.
|
|
53
|
+
* @returns The current container instance, typed with the merged services.
|
|
54
|
+
* @throws {Error} If any service name from the other container already exists in this container.
|
|
55
|
+
*/
|
|
56
|
+
injectContainer<DC extends DiContainer>(other: DC): Merge<this, DC>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=di.d.ts.map
|
package/dist/di.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"di.d.ts","sourceRoot":"","sources":["../src/di.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,IAAI,SAAS,MAAM;IAC1C;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;CACpC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,EAAE,CACV,EAAE,EACF,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,KAAK,EACV,EAAE,GAAG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G,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;CAStE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class e{inject(...e){return e.reduce((e,t)=>{let r,n=t.prototype,i=(0,n.getServiceName)();if(e[i])throw Error((/^inject(Container)?$/.test(i)?"Reserv":"Duplicat")+"ed service name: "+i);return e[i]=new Proxy(Object.create(n),{get:(n,o)=>{r||=e[i]=new t(e);let a=r[o];return"function"==typeof a?a.bind(r):a}}),e},this)}injectContainer(e){for(let t in e)if(t in this)throw Error("Containers have duplicated keys: "+t);return Object.assign(this,e)}}export{e as DiContainer};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&typeof module<"u"?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=typeof globalThis<"u"?globalThis:e||self).Di={})}(this,function(e){"use strict";e.DiContainer=class{inject(...e){return e.reduce((e,t)=>{let i,n=t.prototype,r=(0,n.getServiceName)();if(e[r])throw Error((/^inject(Container)?$/.test(r)?"Reserv":"Duplicat")+"ed service name: "+r);return e[r]=new Proxy(Object.create(n),{get:(n,o)=>{i||=e[r]=new t(e);let s=i[o];return"function"==typeof s?s.bind(i):s}}),e},this)}injectContainer(e){for(let t in e)if(t in this)throw Error("Containers have duplicated keys: "+t);return Object.assign(this,e)}},Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@monkin/di",
|
|
3
|
+
"version": "0.1.12",
|
|
4
|
+
"description": "Small type-safe dependency injection lib",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/di.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/di.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.umd.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18.0.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "vite build",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"lint": "biome check src",
|
|
27
|
+
"format": "biome format --write src",
|
|
28
|
+
"format:check": "biome format src",
|
|
29
|
+
"prepublishOnly": "npm run lint && npm run test && npm run build"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"dependency-injection",
|
|
33
|
+
"di",
|
|
34
|
+
"typescript",
|
|
35
|
+
"type-safe",
|
|
36
|
+
"container",
|
|
37
|
+
"ioc",
|
|
38
|
+
"lightweight"
|
|
39
|
+
],
|
|
40
|
+
"author": {
|
|
41
|
+
"name": "Andrei Monkin",
|
|
42
|
+
"email": "monkin.andrey@gmail.com",
|
|
43
|
+
"url": "https://github.com/monkin"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/monkin/di.git"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/monkin/di/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/monkin/di#readme",
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@biomejs/biome": "^2.3.15",
|
|
59
|
+
"@types/node": "^25.0.9",
|
|
60
|
+
"terser": "^5.46.0",
|
|
61
|
+
"typescript": "^5.9.3",
|
|
62
|
+
"vite": "^7.3.1",
|
|
63
|
+
"vite-plugin-dts": "^4.5.4",
|
|
64
|
+
"vitest": "^4.0.18"
|
|
65
|
+
}
|
|
66
|
+
}
|