Question : Is there a way to import jquery into a TypeScript module using the AMD support (via the compiler) so it includes jquery as a dependency?
The key is to get the import statement, which makes the module a dependency in the define statement (see below).
define(["require", "exports", 'dataservice', 'jquery', 'knockout'],
function(require, exports, __ds__, $ , ko) {
...
}
)
Details : I want to import jquery (and other 3rd party libraries) as a TypeScript modules with AMD. The goal is to make them appears as a dependency in the require
list. However, the only way to make TypeScript to do this appears to be to have an import
statement. And to have an import you need a module to import. But ... there is no jquery module to point to. to.
Workarounds :
So for now I am just preloading jquery in the main.js. but again, but this is less than ideal. Would have to do that for any library like knockout, backbone, etc that has no module.
Any better suggestions or something I am missing?
Update/Clarification:
I can also use shims in the config for dependencies amongst the libraries. But this still preloads the 3rd party ones. Example:
require.config({
baseUrl: '../',
paths: {
'jquery': 'lib/jquery-1.7.2',
'underscore': 'lib/underscore'
},
shim: {
jquery: {
exports: '$'
},
underscore: {
exports: '_'
}
}
});
One other work around would be use a type definition for requirejs and use your own require
statements, rather than an import
statement.
The downside to this is that the TypeScript import
can be used with AMD or CommonJS with just a compiler change, so you would be marrying requirejs in your program more than you would be with an import
.
There is an existing definition for requirejs on Definitely Typed .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.