javascript - 是否有像 BackboneJS 那样将代码包装到自执行匿名函数中的 grunt 插件?

标签 javascript backbone.js gruntjs

我有一些 javascript 代码,我需要像 BackboneJS 那样重叠到自执行匿名函数中:

(function(root, factory) {

    // Set up Backbone appropriately for the environment. Start with AMD.
    if (typeof define === 'function' && define.amd) {
       define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
       // Export global even in AMD case in case this script is loaded with
       // others that may still expect a global Backbone.
       root.Backbone = factory(root, exports, _, $);
    });

    // Next for Node.js or CommonJS. jQuery may not be needed as a module.
    } else if (typeof exports !== 'undefined') {
        var _ = require('underscore');
        factory(root, exports, _);

        // Finally, as a browser global.
    } else {
        root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$));
    }

 }(this, function(root, Backbone, _, $) {

最佳答案

答案是肯定的 如果你想将文件分成几部分,然后在连接后将它放在函数内部,就像这样

(function(){
 // file 1 content
 // file 2 content 
})();

你需要 grunt.loadNpmTasks('grunt-contrib-concat'); 并定义横幅页脚:

concat: {   
      app: {
        options: {
          separator: ';\n',
          banner: '(function(){\n',
          footer: '\n})();'
        },

        src: [
        'src/*.js'
        ],
        dest: 'app.js'
      }
    },

关于javascript - 是否有像 BackboneJS 那样将代码包装到自执行匿名函数中的 grunt 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489721/

相关文章:

backbone.js - 不推荐使用 Marionette 应用程序区域后的用途

rest - 使用 REST 删除多条记录

javascript - 如何调试 Gruntfile.js?

JavaScript 链接点击计数器

javascript - TypeORM:只读连接(Oracle)

javascript - 使用 React JS 右键单击​​菜单

javascript - 如何将参数传递给集合解析?主干.js

javascript - 如何将 browserify 添加到 yeoman 项目中?

javascript - 用于渲染下划线模板的 grunt contrib-jst 格式不正确

javascript - Ajax 高分安全