Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript AMD unable to resolve external module with custom require.config baseUrl

I have some troubles with typescript + requirejs. I have two projects (the main one and the one with unit tests). It looks like this:

solution with two projects
moduleA.ts:

export class A {
    constructor(someThing: string) {
        this.someThing = someThing;
    }
    someThing: string;
}
export var aInst = new A("hello from module A");


moduleB.ts:

import moduleA = require('moduleA');

export class B {
    private a: moduleA.A;

    constructor(a: moduleA.A) {
        this.a = a;
    }

    getSomeThing() {
        return this.a.someThing;
    }
}

export var bInst = new B(moduleA.aInst);


requireConfig.js:

require.config({
    baseUrl: "/app",
});


myTest.ts:

import moduleB = require('moduleB');

QUnit.test('my test', () => {
    QUnit.equal("hello from module A", moduleB.bInst.getSomeThing());
});


testRequreConfig.js:

require.config({
    baseUrl: "../Base/app",
});


index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="Content/qunit.css" type="text/css" />
    <script type="text/javascript" src="Scripts/qunit.js"></script>

    <script src="Scripts/require.js"></script>
    <script src="test/testRequreConfig.js"></script>
</head>
<body>
    <h1 id="qunit-header">Unit Tests</h1>
    <h2 id="qunit-banner"></h2>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
    <div id="qunit-fixture">test markup, will be hidden</div>

    <script type="text/javascript">
        (function () {
            QUnit.config.autostart = false;
            require(['test/myTest.js'], QUnit.start);
        }());
    </script>
</body>
</html>

We are not able to change Base project. We want be able to use moduleB in Test project (loading by requirejs by 'moduleB' string). What we have:
unable to load
TypeScript compiler unable to resolve external module (because it have no idea where to look for it). If we put

declare module "moduleB" {
 
    export  ...
}


in *.d.ts file - then we would be able to use it. But in real project we have a lot of typescript files and it's not possible to manually write 'declare' for each of them. Is there any approach to handle it? The main problem is the fact, that we cant edit require.config (for some reasons).

Is it possible to get tsc.exe to know about require.config?

like image 728
Valery Petrov Avatar asked Oct 20 '22 10:10

Valery Petrov


1 Answers

Is it possible to get tsc.exe to know about require.config?

Unfortunately no. Your two options are to create the def to tell typescript about the config or use full relative paths.

like image 144
basarat Avatar answered Oct 23 '22 05:10

basarat