前端自動化構建工具 - 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_簡書[http://www.lxweimin.com/u/be0d56c4...
    quanjj閱讀 843評論 0 2
  • 為什么要用Web在線工具呢?有兩個原因,第一,它不受限于物理平臺,我既可以在自己的電腦上使用,也可以在公司或親戚朋...
    shenxiaoma閱讀 11,727評論 28 424
  • 今天我在老家掃落葉,你知道我為什么要掃落葉嗎?因為我看到院子里滿地的樹葉很凌亂,感覺就像“樹葉回收站”一樣,所以我...
    舒澤浩閱讀 451評論 0 2
  • 這兩天間或聽一個讀書,書的名字叫《活在當下》,也是美國人的書,風格卻偏于散文,更像是抒發情感的味道。很多情景,都描...
    冠世墨玉yanzi閱讀 237評論 2 2