wessberg / DI-compiler

A Custom Transformer for Typescript that enables compile-time Dependency Injection
MIT License
82 stars 7 forks source link

Is there any special compiler options required? #15

Closed abdelrahman-salah closed 3 years ago

abdelrahman-salah commented 3 years ago

Hello @wessberg Thanks for the awesome package πŸ™

I'm probably missing something trivial but I can't get it to work 😞

sample ts file

import { DIContainer } from "@wessberg/di";
import { LoggerServiceContract } from "../domain/contracts/services/logger.service-contract";
import { LoggerService } from "../services/logger.service";

export function registerServices(injector: DIContainer): void {
    injector.registerSingleton<LoggerServiceContract, LoggerService>();
}

package usage

import { di } from "@wessberg/di-compiler";
import {
    createProgram,
    getDefaultCompilerOptions,
    createCompilerHost,
    ImportsNotUsedAsValues,
} from "typescript";

const compilerOptions = getDefaultCompilerOptions();
compilerOptions.outDir = 'lib';
compilerOptions.importsNotUsedAsValues = ImportsNotUsedAsValues.Preserve;
const compilerHost = createCompilerHost(compilerOptions);

const program = createProgram(
    ["./src/index.ts"],
    compilerOptions,
    compilerHost
);

program.emit(undefined, undefined, undefined, undefined, di({ program }));

result

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerServices = void 0;
require("@wessberg/di");
require("../domain/contracts/services/logger.service-contract");
require("../services/logger.service");
function registerServices(injector) {
/* HERE */    injector.registerSingleton(undefined, { identifier: "LoggerServiceContract", implementation: LoggerService });
}
exports.registerServices = registerServices;

when I run the app I get the following error during this call injector.registerSingleton

ReferenceError: LoggerService is not defined

Do you have any hints on what am I doing wrong? Thanks

abdelrahman-salah commented 3 years ago

I forgot to add this to the compiler option compilerOptions.module = ModuleKind.CommonJS;