2021年9月4日 星期六

前端工具筆記 - gulp , babel(之一)

在使用 react 的時候,很多時候在最初配置開發環境的時候,都直接使用 create-react-app 去建立已經自動配置好環境的 project。實在沒有太多的機會去自行配置這些工具,這篇文章就記錄學習這些工具時的一些筆記。


首先先看 gulp,在官方網頁上的描述是 "A toolkit to automate & enhance your workflow",我理解為一個工具可以自動化流程,所做的工作例如會有配合下面會提到的 babel 去 compile js,以及在開發的時候可以觀察檔案有沒有更動,如有的話就再做動作之類等等。


首先跟據這個 Quick Start 的教學 https://gulpjs.com/docs/en/getting-started/quick-start 做到可以在 command line 運行 gulp 進行一個什麼都不做的 task 。


(一)一個可以 copy file 的 gulp task


首先 project 之中創建一個新的資料夾 src,在裡面建立一個新的 index.html, 內容就大約是這樣


<html>

 

<head>

    <title>Index</title>

</head>

 

<body>

    Hello World!

</body>

 

</html>


然後提 gulpfile.js 更改成這樣


const gulp = require("gulp");

 

gulp.task("default", function () {

 

    return gulp.src("src/*")

    .pipe(gulp.dest("dist"));

});


運行 gulp 之後應該會得到以下的結果


PS C:\Sources\gulp-test> gulp

[09:38:17] Using gulpfile C:\Sources\gulp-test\gulpfile.js

[09:38:17] Starting 'default'...

[09:38:17] Finished 'default' after 28 ms


以及會發覺多了一個 dist 的 folder,裡面有的就是剛剛建立的 index.html。


這就是 gulp 的其中一個 API dest() 的使用,當然目前到輸出之間什麼都沒有做,所以看來兩邊輸入輸出都是一模一樣。


(二) 一個可以進行 babel 轉換的 gulp task


Babel 是一個 JavaScript 的編譯器,目的是就算是用新式的 javascript 寫的程式經過編譯後也可以在一些較舊瀏覽器上運行,例如:IE。至於有什麼新式的 javascript 會被轉換,這裡有一個網站 http://es6-features.org/ ,介紹了 es6 種種的增加的功能。


首先要用 npm 安裝 babel。

npm install --save-dev gulp-babel @babel/core


然後把 index.html 更改為這樣


<html>

 

<head>

    <title>Index</title>

</head>

 

<body>

    Hello World!

 

    <div id="container"></div>

 

    <script src="app.js"></script>

</body>

 

</html>

 


見到新增了一個 app.js 的文件,所以我們在同層之中新建一個 app.js ,內容如下:


class student {

    constructor(name, subject){

        this.name = name;

        this.subject = subject;

    }

}

 

let bob = new student("bob","math");

 

document.querySelector("#container").innerHTML = `Student Name: ${bob.name}, Subject: ${bob.subject}`;


在這個時候,如果直接用 chrome 開的話會見到到以下的結果:

Hello World!

Student Name: bob, Subject: math


而使用 IE 11 的話則會只顯示

Hello World!


這時,在 IE 11 按 F12 呼叫 Developer tool 的話,就會發覺 IE 11 有一個 Syntax error 的錯誤,正是 class 這個寫法未支援。


在這個時候回到 gulpfile.js, 更改成以下的模樣,也即時在 Babel 官網後按 Setup 後的 Usage (https://babeljs.io/setup#installation) 再加上一些更改:


const gulp = require("gulp");

const babel = require("gulp-babel");

 

gulp.task("default", function () {

 

    gulp.src("src/index.html")

    .pipe(gulp.dest("dist"));

 

    return gulp.src("src/*.js")

    .pipe(babel({

      presets: ["@babel/preset-env"]

    }))

    .pipe(gulp.dest("dist"));

});


然後再在 command line 運行 gulp,這時在 dist 資料夾之中應該會有 app.js 和 index.html。使用 Chrome 直接開啟 index.html 還是會顯示:


Hello World!

Student Name: bob, Subject: math


再使用 IE11 去開啟 index.html, 就發現 IE11 也能顯示上述的字句了! 


如果在這時打開 dist 的 app.js,就會發現下面的程式碼:


"use strict";

 

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

 

var student = function student(name, subject) {

  _classCallCheck(this, student);

 

  this.name = name;

  this.subject = subject;

};

 

var bob = new student("bob", "math");

document.querySelector("#container").innerHTML = "Student Name: ".concat(bob.name, ", Subject: ").concat(bob.subject);


就是透過 babel 造出這樣的轉換,使到舊式的瀏覽器也能運行最新的代碼。


2021年8月12日 星期四

太鼓六段挑戰 (一)

玩太鼓的其中一個挑戰就是給自己去考段位,當初想考的是四段,但一直卡左 combo 數的那關,於是把心一橫,嘗試一下直接考六段。

在經歷一陣子的練習之後,下面三首歌都是發揮得最好的時候的成積:



計一計數之後,好像有機會可以過到耶,但是有一個重大的問題就是連打... 上面三首獨立看的話都是 fail 連打數的

但不管了,先考一次看看能到那裡,於是 ->



前面兩首的連打有拼命打的情況之下也可以僅僅合格過關,但第三首要求的約 17打/秒 的連打目前還是一大挑戰...

之後也有嘗試再考過但都是第一首歌就 fail 了連打

接下來的訓練是增加連打力和減少在非難位的失誤希望穩定壓不可在 20 以下

待續...

2021年8月10日 星期二

太鼓分數自我記錄 03/08/2021

 

自五月尾開始,香港開始引進了太鼓之達人的最新版本 ニジイロ Version,於是重新踏上太鼓之路,下面記錄了一些星 10 合格圖 (pass 一格也是 pass) 和其他... 的相片。






















2021年1月19日 星期二

React - 為什麼在 Render 之中的同一句 console.log 會出兩次

 有時在 return 的時候想使用 console.log 去觀察一些 value 的時候,都會發現會重覆做多了一次。

例如在新的 create-react-app template 之中加一個沒有用的 useState 和在 return 的時候加一句 console.log。

function App() {
  
  const [count , setCount] = useState(0)

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
      {console.log('testing')}
    </div>
  );
}

在 npm start 後使用 F12 功能查看,還是會有兩句的 testing 出現。




一直以來都沒有理會這一個現象...

直到看到這篇文章

https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects

當中有一段令我恍然大悟,

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

原來如此... 在 Strict mode 之中把我的行動重覆兩次,這樣就能解釋為什麼我的 console.log 永遠都是會顯示兩行了。

在文中之中所說的 Strict mode 這個限制只會在 development 之中發生,我把以上的 script build 一次之後再用 local server 運行,果然只會顯示只有一行。



不過在文中的最後有以下的提示,在 React 版本 17 之後, console.log() 就不會再顯示第二次由 Strict Mode 所作出的 log 了,但為免日後還是抱有一樣的擬問,還是記錄一下。

Note:

Starting with React 17, React automatically modifies the console methods like console.log() to silence the logs in the second call to lifecycle functions. However, it may cause undesired behavior in certain cases where a workaround can be used.