简体   繁体   English

AngularJS + TypeScript + ES6模块:如何捆绑浏览器?

[英]AngularJS + TypeScript + ES6 modules: how to bundle for browser?

I'm using Angular, TypeScript, and the ES6 module syntax. 我正在使用Angular,TypeScript和ES6模块语法。 Let's suppose I have a module defined as follows: 我们假设我有一个模块定义如下:

// SubModule.ts
import MyService from 'services/MyService';

export default angular.module('subModule', [])
    .service('myService', MyService);

The compiled JavaScript would be: 编译的JavaScript将是:

var MyService_1 = require('services/MyService');
exports.default = angular.module('subModule', [])
    .service('myService', MyService_1.default);

My app depends on that module. 我的应用依赖于该模块。 So I have the following in the App.ts file: 所以我在App.ts文件中有以下内容:

// App.ts
import SubModule from 'modules/SubModule';

angular.module('app', [SubModule.name]);

With the following compiled JavaScript: 使用以下编译的JavaScript:

var SubModule_1 = require('modules/SubModule');
angular.module('app', [SubModule_1.default.name]);

So, now I'm trying to bundle this for the browser. 所以,现在我正在尝试将其捆绑到浏览器中。 I'm currently trying to use Browserify, but I'm willing to use any bundling tool available. 我目前正在尝试使用Browserify,但我愿意使用任何可用的捆绑工具。 Browserify gives me an error such as: Browserify给我一个错误,例如:

Cannot find module 'modules/SubModule' from 'C:\\the\\path\\to\\my\\app' 找不到'C:\\ the \\ path \\到\\ my \\ app'模块'modules / SubModule'

So how do I make Browserify work? 那么我如何让Browserify工作呢? Or is there another tool that would work better? 或者还有其他工具可以更好地工作吗? I found this issue on Github that seems to say that default exports from TypeScript aren't interoperable with CommonJS . 我在Github上发现了这个问题,似乎说TypeScript的默认导出不能与CommonJS互操作 So what do I do? 那我该怎么办?


EDIT So I think I've found the problem. 编辑所以我想我已经找到了问题。 Browserify requires that local paths start with ./ . Browserify要求本地路径以./开头。 So, for example, I would need to reference my sub-module as follows: 因此,例如,我需要引用我的子模块,如下所示:

import SubModule from './modules/SubModule';

Does anybody know of a Browserify plugin that will automatically look for a relative path even if I don't specify ./ ? 有没有人知道Browserify插件会自动查找相对路径,即使我没有指定./ My app is rather large, and I don't want to have to go through and modify all import statements. 我的应用程序相当大,我不想通过修改所有import语句。

Or, on the other hand, is there a TypeScript compiler option to emit './modules/SubModule' for 'modules/SubModule' ? 或者,另一方面,是否有一个TypeScript编译器选项为'modules/SubModule'发出'./modules/SubModule' 'modules/SubModule'

You can use JSPM/Systemjs to load modules asynchronously like: 您可以使用JSPM / Systemjs异步加载模块,如:

System.import('./packageName/ModuleName')

and systemjs-builder to create bundles with commandline tool (jspm-cli) 和systemjs-builder使用命令行工具创建包(jspm-cli)

jspm bundle ./packageName/ModuleName dist/app.js

or with gulp task using: 或使用gulp任务:

var Builder = require('jspm').Builder;
var builder = new Builder();
builder.loadConfig('jspm.conf.js')
    .then(function() {
        var buildConfig = {
            sfxFormat: 'amd',
            minify: true,
            sourceMaps: true
        };
        return builder.buildSFX('./packageName/ModuleName', outFile, buildConfig);
    });

Here you can see full working example: https://github.com/b091/ts-skeleton 在这里你可以看到完整的工作示例: https//github.com/b091/ts-skeleton

If you do not specify it, browserify will look into NPM packages (from node_modules forlder). 如果您不指定它,browserify将查看NPM包(来自node_modules forlder)。 The ./ is needed if you want to say "It is one of my files, not a module." 如果你想说“这是我的一个文件,而不是一个模块”,则需要./

NOTE: 注意:

For the problem of a big project, I would do a simple BASH script like this : 对于大项目的问题,我会做一个简单的BASH脚本,如下所示:

files=`find . -type f`
from="from 'modules/"
to="from './modules/"

for file in $files
do 
    sed -i 's/$from/$to/g' $file
done

IMO, the whole point of Browserify was to "ify" the NodeJS / CommonJS modular structure on the server, over to the browser / client-side. IMO,Browserify的重点是“ify”服务器上的NodeJS / CommonJS模块化结构,直到浏览器/客户端。 If you think of it like that, then you wouldn't want to change the behavior of the import/require statement, keep the "./", so you maintain the symmetry as it should be inline with the common require syntax in NodeJS. 如果您这样想,那么您不希望更改import / require语句的行为,保留“./”,因此您应保持对称性,因为它应该与NodeJS中的common require语法内联。 I think it's just good coding practice. 我认为这只是很好的编码实践。

I created a gulp adaptor for exactly this purpose. 我为这个目的创建了一个gulp适配器。 It builds module bundles from Typescript ES6 module syntax using extensions in the tsconfig.json typescript project file. 它使用tscript.json打字稿项目文件中的扩展,从Typescript ES6模块语法构建模块包。 The gulp build then puts all the components together into a single combined javascript bundle. 然后,gulp构建将所有组件放在一个组合的javascript包中。 I've recently ported the Typescript-Angular todoMVC app to use typescript ES6 syntax which is compiled and build with TsProject. 我最近移植了Typescript-Angular todoMVC应用程序以使用typescript ES6语法,该语法是使用TsProject编译和构建的。 It is fully documented on the TsProject TodoMVC github site. 它在TsProject TodoMVC github网站上完整记录。 Please see if it meets your needs. 请查看它是否符合您的需求。

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

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