简体   繁体   English

带有 AMD 和 require.js 的打字稿

[英]Typescript with AMD and require.js

I am using Typescript with AMD and require.js, but I cannot get the typescript compiler to output code which will be executed after loading the modules.我正在将 Typescript 与 AMD 和 require.js 一起使用,但是我无法让 typescript 编译器输出将在加载模块后执行的代码。

This is main.ts :这是main.ts

import { foo } from './bar';

foo('world');

This is bar.ts :这是bar.ts

export function foo(name: string) {
  alert('Hello ' + name);
}

I compile this with the following tsconfig.json file:我使用以下tsconfig.json文件编译它:

{
    "compilerOptions": {
        "alwaysStrict": true,
        "module": "amd",
        "outFile": "client.js",
        "target": "es5"
    },
    "files": [
        "main.ts"
    ]
}

And include it in my HTML like this:并将其包含在我的 HTML 中,如下所示:

<script data-main="client/client.js" src="/static/require.js"></script>

However, the generated JavaScript code looks like this:但是,生成的 JavaScript 代码如下所示:

define("bar", ["require", "exports"], function (require, exports) {
    "use strict";
    function foo(name) {
        alert('Hello ' + name);
    }
    exports.foo = foo;
});
define("main", ["require", "exports", "bar"], function (require, exports, bar) {
    "use strict";
    bar.foo('world');
});

Everything is fine, except from the fact that I would like to execute the code within the main module directly.一切都很好,除了我想直接在main模块中执行代码。 So the last definition should be所以最后的定义应该是

define(["require", "exports", "bar"], ...

instead of代替

define("main", ["require", "exports", "bar"], ...

Currently, I would need a third script written in JavaScript just to load the main module, and I consider it bad style to have the main module as reusable code.目前,我需要用 JavaScript 编写的第三个脚本来加载main模块,我认为将main模块作为可重用代码是不好的风格。

How can I get the typescript compiler to output main.ts as an executable definition instead of a module definition?如何让打字稿编译器将main.ts作为可执行定义而不是模块定义输出?

Function define only defines a module, it will never execute the module irrespective of how code is generated by TypeScript. Function define只定义一个模块,它永远不会执行该模块,无论 TypeScript 是如何生成代码的。

After all modules have been defined, you have to execute a script, which should contain a statement calling require method.定义完所有模块后,您必须执行一个脚本,该脚本应包含调用require方法的语句。

So after your script has been loaded, you have have a script which should not be in AMD format, it should simply contain following statement...所以在你的脚本加载后,你有一个不应该是 AMD 格式的脚本,它应该只包含以下语句......

require(['main'], function (main) {
   // your main has been loaded !!!
});

Typescript will not generate such statement as it assumes all modules are in AMD format. Typescript 不会生成这样的语句,因为它假定所有模块都是 AMD 格式。 However invoking and executing module is function of AMD loader, and caller should manually call and invoke the module.但是调用和执行模块是AMD加载器的功能,调用者应该手动调用和调用模块。

When you use 'import ...', TypeScript will compile AMD modules like displayed in your question.当您使用“import ...”时,TypeScript 将编译您的问题中显示的 AMD 模块。 Could you try the following code ( also check this tutorial ) to verify if it results in the output you ask for?您能否尝试以下代码( 另请查看本教程)来验证它是否会产生您要求的输出?

/// <reference path="[YOUR IMPORT FILE]" />
/// ...

/**
 * Main entry point for RequireJS
 */
require(
    [
        // YOUR IMPORT DEFINITIONS
    ],
    (/* YOUR IMPORT VARIABLES */) => {
        'use strict';

        // YOUR CODE HERE
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM