javascript - TypeScript AMD 到浏览器上的单个文件

标签 javascript typescript gruntjs requirejs amd

我正在使用 TypeScript 并使用 grunt 将它编译成一个文件到 ES5 和 AMD 模块。

例如我有 app.ts :

import {CONFIG} from './config';
console.log(CONFIG.app)

config.ts:

 export const CONFIG = {
    app      : 'MyApp'
}

App是最终需要执行的主脚本,我的构建输出如下:

define("config", ["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.CONFIG = {
        app: 'MyApp'
    };
});
define("main", ["require", "exports", "config"], function (require, exports, config_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    console.log(config_1.CONFIG.app);
});

我应该如何让它在浏览器上运行?

我尝试包含 require.js 但没有成功。

我需要将它编译成一个文件,并且所有配置都需要放在 gruntfile.js 上,因为它将是一个大型项目,将作为一个单个文件

此外,是否有一种更有效的方法不需要第三方库将其全部编译到一个文件中?

最佳答案

How am I supposed to make it runnable on a browser ?

您需要使用 require.js 运行时。 AMD 需要 AMD 加载程序,require.js 是最流行的选项。

更多

文档 http://requirejs.org/docs/start.html

个人

就我个人而言,我只会将 commonjs 与 webpack 一起使用:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

关于javascript - TypeScript AMD 到浏览器上的单个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47295039/

相关文章:

javascript - 如何(本地)从 PHP(在 Raspberry Pi 上)调用 JavaScript 函数

javascript - 尝试替换输入值会导致 TypeError : input. 替换不是函数

Angular:将两个表单控件绑定(bind)到同一个模型

variables - 在 Gruntfile.js 中使用变量

javascript - 需要设置内联style="width: window.innerWidth"

javascript - 超出最大调用堆栈大小

javascript - 如何在 Typescript 中缩短这个条件?

javascript - Typescript - 检查对象是否具有所有接口(interface)属性

javascript - 了解 gruntjs registerTask 冒号

gruntjs - Yeoman Grunt imagemin fatal error : Cannot read property 'contents' of undefined