前端自动化构建工具 - FIS3 - 第七节:cmd模块化支持

前言

标准化输出 cmd 代码

安装插件

$ sudo npm install -g fis3-hook-cmd
$ sudo npm install -g fis3-hook-commonjs

说明
https://github.com/fex-team/fis3-hook-cmd

实例

项目准备

目录

/src/app/main.js
/src/app/webBanner.js
/src/jquery/jquery-debug.js
/static/sea.js
/index.html
/fis-conf.js

seajs下载
http://seajs.org/docs/#downloads

编写代码

/src/app/main.js

define(function(require) {
    var WebBanner = require('./webBanner.js');

    var banner = new WebBanner('#userName');
    banner.render();
});

/src/app/webBanner.js

define(function(require, exports, module) {

    var $ = require('jquery');

    function WebBanner(container){
        this.container = $(container);
        this.username = "";
    }

    module.exports = WebBanner;

    WebBanner.prototype.render = function() {
        this._init();
        this.container.html(this.username);
        console.log(this.username);
    }

    WebBanner.prototype._init = function() {
        this.username = "hans";
    }

});

/index.html


<script src="./static/sea.js"></script>

<script>
// seajs.config({
//     base: "./src/",
//     alias: {
//         "jquery": "jquery/jquery-debug.js",
//         "$": "jquery/jquery-debug.js"
//     }
// });
seajs.use("app/main");
</script>

<h1 id="userName"></h1>

编译的时候 需要把 seajs.config 注释掉,否则会影响fis编译

配置fis-conf.js文件

// 只需要编译 html 文件,以及其用到的资源。
fis.set('project.files', ['*.html', 'map.json']);

fis.match('/src/**/*.js', {
  isMod: true
});

fis.match('/static/sea.js', {
  isMod: false
});

fis.hook('cmd', {
  baseUrl: './src/',
  paths: {
    "jquery": "jquery/jquery-debug.js",
    "$": "jquery/jquery-debug.js"
  }
});

fis.match('::packager', {
    postpackager: fis.plugin('loader', {
      allInOne: {
        includeAsyncs: true,
        ignore: ['/static/sea.js']
      }
    })
  });

编译运行

$ fis3 release
$ fis3 server start

查看结果

/src/app/main.js

define('src/app/main', ['src/app/webBanner'], function(require) {
    var WebBanner = require('src/app/webBanner');

    var banner = new WebBanner('#userName');
    banner.render();
});

/src/app/webBanner.js

define('src/app/webBanner', ['src/jquery/jquery-debug'], function(require, exports, module) {

    var $ = require('src/jquery/jquery-debug');

    function WebBanner(container){
        this.container = $(container);
        this.username = "";
    }

    module.exports = WebBanner;

    WebBanner.prototype.render = function() {
        this._init();
        this.container.html(this.username);
        console.log(this.username);
    }

    WebBanner.prototype._init = function() {
        this.username = "hans";
    }

});

/index.html

<script src="./static/sea.js"></script>

<script>
seajs.config({
    base: "./",
    alias: {
        "jquery": "jquery/jquery-debug.js",
        "$": "jquery/jquery-debug.js"
    }
});
seajs.use("src/app/main");
</script>

<h1 id="userName"></h1>

这里如果正常运行需要还原配置 seajs.config

代码

https://github.com/hans007/JavaScriptCodes/tree/master/fis3/use-seajs

我的博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • jHipster - 微服务搭建 CC_简书[https://www.jianshu.com/u/be0d56c4...
    quanjj阅读 829评论 0 2
  • 为什么要用Web在线工具呢?有两个原因,第一,它不受限于物理平台,我既可以在自己的电脑上使用,也可以在公司或亲戚朋...
    shenxiaoma阅读 11,698评论 28 424
  • 今天我在老家扫落叶,你知道我为什么要扫落叶吗?因为我看到院子里满地的树叶很凌乱,感觉就像“树叶回收站”一样,所以我...
    舒泽浩阅读 449评论 0 2
  • 这两天间或听一个读书,书的名字叫《活在当下》,也是美国人的书,风格却偏于散文,更像是抒发情感的味道。很多情景,都描...
    冠世墨玉yanzi阅读 234评论 2 2