grunt-autowrap

grunt task plugin auto wrap exports or amd for js or coffee

grunt-autowrap

grunt task plugin auto wrap exports or amd for js or coffee

grunt.initConfig({
  autowrap: {
      testJs:{
        options: {
          ext: 'js',
        },
        files: {
          'tmp/testJs.js': ['test/fixtures/A.js', 'test/fixtures/B.js'],
        },
      },
      testCoffee:{
        options: {
          ext: 'coffee',
        },
        files: {
          'tmp/testCoffee.js': ['test/fixtures/*.coffee'],
        },
      },
      testAmd:{
        options:{
          wrapType:'amd',
          ext:'coffee',
        },
        files:{
          'tmp/testAmd.js':'test/fixtures/*.coffee',
        },
      },
    },
})
//A.js 
function A1()
{
  //this is A 
}
var A2 = function()
{
 
};
var _notExport = function()
{
 
};
//B.js 
function B()
{
  //this is B 
}
//output.js 
(function(exports){
function A1()
{
  //this is A 
}
var A2 = function()
{
 
};
var _notExport = function()
{
 
};
function B()
{
  //this is B 
}
exports.A2=A2;
exports.A1=A1;
exports.B=B;
})(exports);
# C.coffee 
class C1
  constructor: () ->
    console.log 'This is C1'
  sayHello:()->
    console.log 'Hello from C1'
 
class C2 extends C1
  constructor: () ->
    console.log 'This is C2'
  sayHello:()->
    console.log 'Hello from C2'
# D.coffee 
class D
  constructor: () ->
    console.log 'This is D'
// output.js 
define(function(requireexportsmodule){
var C1, C2, D,
  __hasProp = {}.hasOwnProperty,
  __extends = function(childparent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
 
C1 = (function() {
  function C1() {
    console.log('This is C1');
  }
 
  C1.prototype.sayHello = function() {
    return console.log('Hello from C1');
  };
 
  return C1;
 
})();
 
C2 = (function(_super) {
  __extends(C2, _super);
 
  function C2() {
    console.log('This is C2');
  }
 
  C2.prototype.sayHello = function() {
    return console.log('Hello from C2');
  };
 
  return C2;
 
})(C1);
 
= (function() {
  function D() {
    console.log('This is D');
  }
 
  return D;
 
})();
exports.C1=C1;
exports.C2=C2;
exports.D=D;
});

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-autowrap --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-autowrap');

In your project's Gruntfile, add a section named autowrap to the data object passed into grunt.initConfig().

grunt.initConfig({
  autowrap: {
      testJs:{
        options: {
          ext: 'js',
        },
        files: {
          'tmp/testJs.js': ['test/fixtures/A.js', 'test/fixtures/B.js'],
        },
      },
      testCoffee:{
        options: {
          ext: 'coffee',
        },
        files: {
          'tmp/testCoffee.js': ['test/fixtures/*.coffee'],
        },
      },
      testAmd:{
        options:{
          wrapType:'amd',
          ext:'coffee',
        },
        files:{
          'tmp/testAmd.js':'test/fixtures/*.coffee',
        },
      },
    },
})

Type: boolean Default value: true

exports symbol.

Type: String Default value: all

  • var exports variable in global scope
  • function exports function in global scope
  • all exports variable and function in global scope
  • filename exports by filename

Type: boolean Default value: true

do not export name starts with _

Type: boolean Default value: true

concatenate files before processing.

Type: String Default value: exports

  • exports add a exports wrapper
  • amd add a amd wrapper
  • null do not add any wrapper

Type: String Default value: js

  • js treat as javascript
  • coffee compile as coffeescript

Type: object Default value: {}

params passed to coffee compiler

Type: String Default value: "\n"

separator used to join files.

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

  • 0.3.0 add amd support
  • 0.2.0 exports by parse source code
  • 0.1.0 exports based on filename