我有一些 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/