最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript with AMD and require.js - Stack Overflow

programmeradmin1浏览0评论

I am using Typescript with AMD and require.js, but I cannot get the typescript piler to output code which will be executed after loading the modules.

This is main.ts:

import { foo } from './bar';

foo('world');

This is bar.ts:

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

I pile this with the following tsconfig.json file:

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

And include it in my HTML like this:

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

However, the generated JavaScript code looks like this:

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. 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.

How can I get the typescript piler to output main.ts as an executable definition instead of a module definition?

I am using Typescript with AMD and require.js, but I cannot get the typescript piler to output code which will be executed after loading the modules.

This is main.ts:

import { foo } from './bar';

foo('world');

This is bar.ts:

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

I pile this with the following tsconfig.json file:

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

And include it in my HTML like this:

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

However, the generated JavaScript code looks like this:

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. 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.

How can I get the typescript piler to output main.ts as an executable definition instead of a module definition?

Share Improve this question asked Jan 27, 2017 at 11:26 just.mejust.me 2,2855 gold badges18 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Function define only defines a module, it will never execute the module irrespective of how code is generated by TypeScript.

After all modules have been defined, you have to execute a script, which should contain a statement calling require method.

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...

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

Typescript will not generate such statement as it assumes all modules are in AMD format. However invoking and executing module is function of AMD loader, and caller should manually call and invoke the module.

When you use 'import ...', TypeScript will pile AMD modules like displayed in your question. 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
    }
);
发布评论

评论列表(0)

  1. 暂无评论