1

Let's say I have the following TypeScript file:

import module1 = require( 'module1' );
import module2 = require( 'module2' );

...some code...

This would generate the following JavaScript on compilation:

define( [ 'require', 'exports', 'module1', 'module2' ], function ( require, exports, module1, module2 ) {
...
}

However, what if I want to include a file that's NOT a TypeScript module? I want it to be loaded by RequireJS, as it has dependencies that are also loaded by RequireJS. If I try to add e.g. this:

import someJsFile = require ( 'someJsFile' );

I just get a "Module cannot be aliased to a non-module type" error on compilation. If I skip the import part and just do require( 'someJsFile' ), it compiles, but the JavaScript it generates is a non-functional nested define instead:

define( [ 'require', 'exports', 'module1', 'module2' ], function ( require, exports, module1, module2 ) {
    define( [ 'someJsFile' ], function ( someJsFile ) {
    ...
    }
}

Is there any way of including JavaScript files in the outer, "correct" RequireJS define without having to convert the JS files to TS modules myself? Or do I have to manually create a RequireJS define and drop the import x = require( 'x' ) statements altogether?

1 Answer 1

2

Use amd-dependency. In your case :

/// <amd-dependency path="someJsFile" />
declare var someVarFromJsFile: any;
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, wow. I had tried that before, but I always put it after the import statements, which didn't have any effect, so I assumed that approach was fruitless. Tried putting it at the top of the file now - works like a charm. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.