admin管理员组文章数量:1026062
I've got the following which points to a backbone.d.ts definition.
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
With --module amd
it generates the following.
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
I've shimmed backbone with my RequireJS configuration file because of the global. I'd like for my define to just say backbone
instead of the relative path. Is there any workaround besides a post-build process to manipulate the define?
I've got the following which points to a backbone.d.ts definition.
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
With --module amd
it generates the following.
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
I've shimmed backbone with my RequireJS configuration file because of the global. I'd like for my define to just say backbone
instead of the relative path. Is there any workaround besides a post-build process to manipulate the define?
3 Answers
Reset to default 3I think the solution to this issue is to put the Ambient Declaration in the same location as the eventual real module, so you can reference it with the same path.
So backbone.d.ts
needs to be in the same location as backbone.js
.
Instead of using a relative path, I always use the path from the root. Something like:
import underscore = module('libs/underscore/underscore');
and the hack is that you can define the same name in your shim. Something like this:
require.config({
paths: {
'libs/underscore/underscore': 'libs/underscore/underscore'
},
shim: {
'libs/underscore/underscore': {
exports: '_'
}
}
});
By always using a path from the root, the path stays the same as opposed as if you were using the relative path. It's a hack, but it works for me (GitHub).
I have just put together a blog on how to use AMD modules in TypeScript.
Firstly, just use a reference path to backbone as follows:
/// <reference path="../../modules/Backbone.d.ts" />
Then define your class without an import:
export class MTodoCollectionView extends Backbone.View {
...
}
Then your require.config, and apploader:
require.config({
baseUrl: '../',
paths: {
'underscore': 'lib/underscore',
'backbone': 'lib/backbone'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
}
}
});
require(['jquery','underscore','backbone','console','app/AppMain',
],
($, _, Backbone, console, main) => {
// code from window.onload
var appMain = new main.AppMain();
appMain.run();
});
Once the app hits the require block of code, Backbone is globally defined.
Have fun,
I've got the following which points to a backbone.d.ts definition.
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
With --module amd
it generates the following.
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
I've shimmed backbone with my RequireJS configuration file because of the global. I'd like for my define to just say backbone
instead of the relative path. Is there any workaround besides a post-build process to manipulate the define?
I've got the following which points to a backbone.d.ts definition.
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
With --module amd
it generates the following.
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
I've shimmed backbone with my RequireJS configuration file because of the global. I'd like for my define to just say backbone
instead of the relative path. Is there any workaround besides a post-build process to manipulate the define?
3 Answers
Reset to default 3I think the solution to this issue is to put the Ambient Declaration in the same location as the eventual real module, so you can reference it with the same path.
So backbone.d.ts
needs to be in the same location as backbone.js
.
Instead of using a relative path, I always use the path from the root. Something like:
import underscore = module('libs/underscore/underscore');
and the hack is that you can define the same name in your shim. Something like this:
require.config({
paths: {
'libs/underscore/underscore': 'libs/underscore/underscore'
},
shim: {
'libs/underscore/underscore': {
exports: '_'
}
}
});
By always using a path from the root, the path stays the same as opposed as if you were using the relative path. It's a hack, but it works for me (GitHub).
I have just put together a blog on how to use AMD modules in TypeScript.
Firstly, just use a reference path to backbone as follows:
/// <reference path="../../modules/Backbone.d.ts" />
Then define your class without an import:
export class MTodoCollectionView extends Backbone.View {
...
}
Then your require.config, and apploader:
require.config({
baseUrl: '../',
paths: {
'underscore': 'lib/underscore',
'backbone': 'lib/backbone'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
}
}
});
require(['jquery','underscore','backbone','console','app/AppMain',
],
($, _, Backbone, console, main) => {
// code from window.onload
var appMain = new main.AppMain();
appMain.run();
});
Once the app hits the require block of code, Backbone is globally defined.
Have fun,
本文标签: javascriptTypeScriptRequireJS shimambient module declarationStack Overflow
版权声明:本文标题:javascript - TypeScript, RequireJS shim, ambient module declaration - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632617a2160253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论