简体   繁体   English

最小化TypeScript模块的最佳实践

[英]Best practice for minifying TypeScript modules

I'm using requirejs and AMD modules for my TypeScript project, with something like 20 different source files at the moment and likely to grow substantially. 我正在为TypeScript项目使用requirejs和AMD模块,目前有大约20种不同的源文件,并且可能会大幅增长。 All of this works, but it's very slow to load all 20 files, so it would be better to have them minified. 所有这些工作正常,但是加载全部20个文件非常慢,因此最好缩小它们。 But because of how requirejs wants to load everything, it seems like it's going to require that I keep the modules in separate files - I don't think I can just take the generated module1.js and module2.js files and minify them into one file and then have requirejs load those without changing some code. 但是由于requirejs如何加载所有内容,因此似乎需要将模块保存在单独的文件中-我认为我不能只将生成的module1.js和module2.js文件合并为一个文件文件,然后让requirejs加载这些文件而无需更改一些代码。 (I could be wrong on this.) (对此我可能是错的。)

The other way that I see to do this is to use the r.js file that requirejs provides to merge all the different files together in a way that still keeps requirejs happy. 我看到的另一种方式是使用requirejs提供的r.js文件将所有不同的文件合并在一起,同时仍然保持requirejs满意。 But r.js requires node.js, and I'd rather not introduce that as a dependency in my build process if there's any other way to do it. 但是r.js需要node.js,如果有其他方法,我不希望在构建过程中将其作为依赖项引入。

So before I dive into this and try half a dozen different solutions - how are other folks approaching this with big projects? 因此,在我深入研究这个问题并尝试六种不同的解决方案之前,其他人如何通过大型项目来解决这个问题?

What you could do is to implement a thin RequireJS shim to use in a minified build. 您可以做的是实现一个精简的RequireJS填充程序,以在最小化版本中使用。 Depending on how much of the RequireJS API you want to use, you could get by with very little. 根据要使用的RequireJS API的多少,您可以花很少的钱就可以解决。 For simplicity you could also use named modules . 为简单起见,您还可以使用命名模块

Say, while developing you use RequireJS to load your modules. 说,在开发时,您使用RequireJS加载模块。 When you want to make a minified build, you could simply include a simple loader in the minified file. 当您要进行精简构建时,可以在精简文件中仅包含一个简单的加载器。

If you have files app.js, foo.js and bar.js as follows: 如果您具有文件app.js,foo.js和bar.js,如下所示:

//from app.js  
define("app", ["foo", "bar"], function(foo, bar) { 
    return {
        run: function() { alert(foo + bar); }
    }
});

//from foo.js
define("foo", [], function() { 
    return "Hello ";
});

//from bar.js
define("bar", [], function() { 
    return "World!";
});

And let's say you minify all those files together. 假设您将所有这些文件一起缩小。 At the top of the file you include the following shim: 在文件的顶部,包括以下填充程序:

//from your-require-shim.js
(function(exports) {

    var modules = {};

    var define  = function(name, dependencies, func) { 
        modules[name] = {
            name:name,
            dependencies:dependencies,
            func:func,
            result:undefined
        };
    };

    var require = function(name) {

        var module = modules[name];

        //if we have cached result -> return
        if(module.result) { return module.result; }

        var deps = [];
        //resolve all dependencies
        for(var i=0,len=module.dependencies.length;i<len;i++) { 
            var depName = module.dependencies[i];
            var dep = modules[depName];
            if(!dep.result) { 
                //resolve dependency
                require(depName);
            }
            deps.push(dep.result);
        }

        module.result = module.func.apply(this, deps );

        return module.result;
    };
    exports.require = require;
    exports.define = define;
}(window));

And execute the module defined in app.js 并执行app.js中定义的模块

require("app").run();

Like in this fiddle . 就像在这小提琴中一样

It's a crude PoC of course, but I'm sure you get the meaning. 当然,这是一个粗略的PoC,但是我敢肯定,您会明白其中的意思。

If you are using ASP.NET MVC 4, you can make a bundle which will minify everything when you deploy to production in a set of files or in a folder. 如果您使用的是ASP.NET MVC 4,则可以制作一个捆绑包,当您将其部署到生产环境中时,它会压缩文件集或文件夹中的所有内容。 You'll find more info on bundles here . 您可以在此处找到有关捆绑销售商品的更多信息。

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

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