星期三, 10月 01, 2014

[Gulp] html 裡 include 的是 css 檔案,為什麼我們修改 scss 檔案,能夠即時修改輸出結果?而且在 watch 模式下還是把輸出檔案暫存在 .tmp 目錄下??

主要是跟 styles, watch, connect 這幾個 task 有關:

在 styles task 裡,我們先設定要針對哪些 scss 檔案作轉換處理;然後輸出到 .tmp/styles 目錄下
// scss files
var scss_files = [
  // 'app/styles/main.scss',
  'app/styles/**/*.scss',
  // smartadmin common_assets
  'COMMON_ASSETS/SASS_FILES/scss/bootstrap.scss',
  'COMMON_ASSETS/SASS_FILES/scss/smartadmin-production.scss',
  'COMMON_ASSETS/SASS_FILES/scss/library/fontawesome/font-awesome.scss',
  'COMMON_ASSETS/SASS_FILES/scss/smartadmin-skins.scss',
  'COMMON_ASSETS/SASS_FILES/scss/smartadmin-skins.scss'
];
gulp.task('styles', function () {
  return gulp.src(scss_files)
    .pipe($.rubySass({
        style: 'expanded',
        precision: 10
    }))
    .pipe($.autoprefixer('last 1 version'))
    .pipe(gulp.dest('.tmp/styles'))
    .pipe($.size());
});

在 watch task 自動載入下,指定要監看哪些檔案,並做出哪些相對應動作(這裡是利用 dependency 聯動 task 的方式)
gulp.task('watch', ['connect', 'serve'], function () {
    var server = $.livereload();
    // watch for changes
    gulp.watch([
        'app/*.html',
        '.tmp/styles/**/*.css',
        'app/scripts/**/*.js',
        'app/images/**/*'
    ]).on('change', function (file) {
        server.changed(file.path);
    });
    gulp.watch('app/styles/**/*.scss', ['styles']);
    gulp.watch('app/scripts/**/*.js', ['scripts']);
    gulp.watch('app/images/**/*', ['images']);
    gulp.watch('bower.json', ['wiredep']);
});

那放在 .tmp/styles 的檔案,如何能讓 html 連結到?html 裡頭寫的是 <link rel="stylesheet" type="text/css" media="screen" href="styles/main.css">?

這是因為 watch 的 dependency 裡有 connect task。在啟動 server 時,利用 connect.static 來指定「靜態文件服務器的路徑」,也就是 html 裡用到的所有相對路徑的根目錄。這裡預設了兩個靜態文件目錄,一個是 app,另外一個是 .tmp。所以如果我們需要找到 styles/main.css,系統會先去找 app/styles/main.css 是否存在,如果沒有,才會去找 .tmp/styles/main.css。
gulp.task('connect', function () {
    var connect = require('connect');
    var app = connect()
        .use(require('connect-livereload')({ port: 35729 }))
        .use(connect.static('app'))
        .use(connect.static('.tmp'))
        .use(connect.directory('app'));
    require('http').createServer(app)
        .listen(9000)
        .on('listening', function () {
            console.log('Started connect web server on http://localhost:9000');
        });
});

星期二, 9月 30, 2014

[Gulp] 輸出檔案(dest)的資料夾結構保持跟來源檔案(src)一致

gulp 針對某目錄下的所有檔案處理後,如果要能根據目錄結構輸出到另一個目錄,記得要指定 {‘base’: 來源根目錄}:

https://github.com/gulpjs/gulp/blob/master/docs/API.md#optionsbase

// without the 'base' set
gulp.src('app/**/*.scss')        //          file: 'app/css/somefile.scss'
  .pipe(gulp.dest('build'));      //->   file base: 'app/css'
                                              // relative path: 'somefile.scss'
                                              //    write path: 'build/somefile.scss'

// with the 'base' set
var glob = 'app/**/*.scss';
gulp.src(glob, { base: 'app' })  //          file: 'app/css/somefile.scss'
  .pipe(gulp.dest('build'));        //->   file base: 'app'
                                                // relative path: 'css/somefile.scss'
                                                //    write path: 'build/css/somefile.scss'

星期四, 9月 11, 2014

[Gulp 問題] “Possibly unhandled Error: path should be string...” when gulp build

不知怎搞的,才剛 build 沒事,緊接著改了無關緊要的 code,就出現了如下的錯誤訊息:
indiana@indiana-dt:.../program/web/my-project$ gulp build
[10:52:54] Using gulpfile ~/program/web/my-project/gulpfile.js
[10:52:54] Starting 'styles'...
[10:52:54] Starting 'scripts'...
[10:52:54] Starting 'images'...
[10:52:54] Starting 'fonts'...
[10:52:55] Starting 'extras'...
[10:52:55] Starting 'move'...
/home/indiana/program/web/my-project/app/scripts/facebook.js
  line 46  col 9   'alert' is not defined.
  line 63  col 50  'params' is defined but never used.
  line 82  col 52  'widget' is defined but never used.
  line 82  col 46  'href' is defined but never used.
✖ 4 problems
Possibly unhandled Error: path should be string
    at File.Object.defineProperty.set (/home/indiana/program/web/my-project/node_modules/gulp/node_modules/vinyl-fs/node_modules/vinyl/index.js:166:41)
    at Function.assign (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/lodash-node/modern/objects/assign.js:63:21)
    at /home/indiana/program/web/my-project/node_modules/gulp-cache/lib/TaskProxy.js:20:19
    at tryCatch1 (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/util.js:64:19)
    at Promise$_callHandler [as _callHandler] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:708:13)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:724:18)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:896:14)
    at Promise$_fulfillPromises [as _fulfillPromises] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:1041:14)
    at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/async.js:64:12)
    at Async$consumeFunctionBuffer (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/async.js:37:14)
Possibly unhandled Error: path should be string
    at File.Object.defineProperty.set (/home/indiana/program/web/my-project/node_modules/gulp/node_modules/vinyl-fs/node_modules/vinyl/index.js:166:41)
    at Function.assign (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/lodash-node/modern/objects/assign.js:63:21)
    at /home/indiana/program/web/my-project/node_modules/gulp-cache/lib/TaskProxy.js:20:19
    at tryCatch1 (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/util.js:64:19)
    at Promise$_callHandler [as _callHandler] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:708:13)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:724:18)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:896:14)
    at Promise$_fulfillPromises [as _fulfillPromises] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:1041:14)
    at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/async.js:64:12)
    at Async$consumeFunctionBuffer (/home/indiana/program/web/my-project/node_modules/gulp-cache/node_modules/bluebird/js/main/async.js:37:14)

[10:52:55] gulp-size: total 19.5 kB
[10:52:55] Finished 'scripts' after 864 ms
[10:52:55] Finished 'move' after 234 ms
[10:52:55] gulp-size: total 699.12 kB
[10:52:55] Finished 'fonts' after 421 ms
[10:52:55] Finished 'extras' after 258 ms
npm WARN package.json wifi@0.0.0 No description
npm WARN package.json wifi@0.0.0 No repository field.
npm WARN package.json wifi@0.0.0 No README data
[10:52:56] gulp-size: total 130.39 kB
[10:52:56] Finished 'styles' after 2.24 s
[10:52:56] Starting 'html'...
[10:53:15] gulp-size: total 2.08 MB
[10:53:15] Finished 'html' after 19 s
indiana@indiana-dt:.../program/web/my-project$ 

參考文件: https://github.com/jgable/gulp-cache/issues/24

星期六, 8月 30, 2014

[Gulp 問題] Task ‘fonts’ with package ‘gulp-bower-files’ miss some .eot,svg,ttf,woff files

gulp build 時,task ‘fonts’ 應該會透過 package ‘gulp-bower-files’ 的指令 bowerFiles 掃描所有 bower_components 下所有的檔案,並把附檔名為 **/*.{eot,svg,ttf,woff} 的檔案全都複製到 dist/fonts 下才對。但不知道為什麼只有 copy 了 bootstrap-sass-official 下的,卻漏掉 components-font-awesome 下的,造成錯誤。

參考文件: https://github.com/yeoman/generator-gulp-webapp/issues/71#issuecomment-40327323

星期一, 8月 25, 2014

Sublime Text 3 中文輸入問題

Sublime Text 很好用,但是我一直受無法中文輸入所苦。之前想說算了,就強迫自己盡量都用英文,但是現在要搞多國語系,不能輸入中文就麻煩了。所以還是想辦法把他搞定...

星期一, 6月 02, 2014

支線故事

在生命旅途中,你是否也有某幾個時期,因為某些事情而結識的朋友?這些朋友因為各自生活重心的轉移,彼此慢慢地失去了聯繫?彷彿是生命中的支線故事一般,在之後的某個時間點,因為某些人、事、物,讓你有機會回想起,甚至是再次與這些朋友聚在一起,過去的回憶大量湧入心頭,看著眼前既熟悉又陌生的友人,內心激動地很想跟他們說些什麼,卻又什麼都說不出口?