Coder Social home page Coder Social logo

grunt-cmd-combo's Introduction

grunt-cmd-combo

用来打包 SeaJS 模块的 grunt 任务。

特点

grunt-cmd-combo 打包后的文件可以脱离 SeaJS 直接执行。

grunt-cmd-combo 的模块化标准是 SeaJS 的一个子集,规则非常简单:

  • 所有的 define 定义只包含一个参数,即 factory 函数:
  • factory 函数只接受 require 函数以及 exports 对象,不接受 module 对象
  • require 只支持相对路径 require

例子:

/* math.js */
define(function (require) {
    return {
        plus: function (a, b) {
            return a + b;
        }
    };
});

/* main.js */
define(function (require) {
    require('./math').plus(1, 2); // 3
});

使用

安装:

npm install grunt-cmd-combo

Gruntfile.js 中加入如下配置:

grunt.initConfig({
    combo: {
        options: {
            sourceMap: {
                sourceRoot: '/src/'
            }
        },
        build: {
            files: [{
                expand: true,
                cwd: 'test/src/',
                src: '**/*.js',
                dest: 'test/dist',
                ext: '.combo.js'
            }]
        }
    }
});

grunt.loadNpmTasks('grunt-cmd-combo');

会将上面例子中的 main.js 打包成 main.combo.js

打包后的代码只需要一个 <script> 标签来载入,不需要 SeaJS 支持:

    <script src="/dist/main.combo.js" data-main="main" type="text/javascript"></script>

参数

  • files (必须) 参考 grunt files object
  • sourceMap (可选)非空则生成 sourceMap,默认不生成
  • sourceMap.sourceRoot (可选) 生成 sourceMap 中的 sourceRoot (参考 SourceMapGenerator 中的 sourceRoot 参数)

原理

以上面给的例子 math.js 以及 main.js 为例,在对 main.js 打包时的流程如下:

  1. 分析入口模块 main.js,建立主模块的依赖数组 dpes
  2. 将所有的 require 参数由相对于当前文件的路径转为相对于源码根目录的路径:require('./math') 会被转换为 require('math')
  3. 分析所有依赖的模块,将依赖的模块名加入 deps
  4. 对所有依赖的模块,递归地进行 2-4 步
  5. deps 中所有的模块中的代码按顺序合并成一个文件 main.combo.js
  6. loader.js 的内容加到 main.combo.js(提供无 SeaJS 环境下的 define 函数支持)
  7. main.js 的内容加到 main.combo.js 的结尾

loader.js

loader.js 提供了最简单的一个 define 函数实现,以使得代码在打包后可以脱离 SeaJS 执行。

Push Request

欢迎提交 Push Request,提交前请先通过 grunt test 保证代码通过单元测试和 jslint 检查。

发布历史

  • 1.0.1 2013-07-01 固定依赖模块的版本号,更新 Grunt 0.4.1。
  • 1.0.0 2013-02-28 发布第一个正式版本,更新了 README.md 和 testcase 到 Grunt 0.4.0。
  • 0.1.8 2013-02-22 Grunt 0.4 支持。
  • 0.1.6 2013-01-07 保留注释。
  • 0.1.5 2012-12-10 增加 source map 支持。
  • 0.1.4 2012-12-10 文件增加 .combo 后缀。
  • 0.1.0 2012-12-10 首次发布。

License

Copyright (c) 2013 PerfectWorks
Licensed under the MIT license.

grunt-cmd-combo's People

Contributors

e7h4n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-cmd-combo's Issues

require 只支持相对路径 require

在运行的时候读jquery时候报错,找不到文件,我在config.js里配置过,
'jquery': {
src: '/js/libs/jquery/jquery-debug-1.9.1.js',
exports: 'jQuery'
},var $ = require('jquery');,这样的写法会有问题吗?应该怎么写?

关于 seajs plugin-text

我在项目中使用了seajs 的plugin-text,在运行grunt combo的时候,没办法处理这些 require语句(require("text!xxx.html"),我只能将他们改写成 $.get(),我就是想看能不能把这个加载html的功能也做进来就完美啦~
总的来说,这个工具相当不错!

require 的层次多于两层就出问题

在示例自带的test/src 中,如果math.js require 一个本地js(如ex.js) ,用grunt 执行任务,就会出现

warning: Unable to read "test/src/D:\ex.js " file 的错误.ex.js

报错了

(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

util.js:35
var str = String(f).replace(formatRegExp, function(x) {
^
RangeError: Maximum call stack size exceeded

Grunt 0.4 Release

I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.

If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.

If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8" as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0" to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.

Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.

Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!

If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.

Thanks, we really appreciate your work!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.