我正在使用 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/