From 22cc2e06ce83664e0f86d1cb8c734a3bb113778d Mon Sep 17 00:00:00 2001 From: Peng-Jie Date: Mon, 2 May 2016 10:03:30 +0800 Subject: [PATCH 1/5] translate README and part1: example1-example5 --- README.md | 9 +- part1/README.md | 305 +++++++++++++++++---------------------- part1/example1/README.md | 41 +++--- part1/example2/README.md | 18 +-- part1/example3/README.md | 31 ++-- part1/example4/README.md | 31 ++-- part1/example5/README.md | 39 ++--- 7 files changed, 205 insertions(+), 269 deletions(-) diff --git a/README.md b/README.md index 4f2b460..0a58392 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# Beginner Webpack Tutorial :school: +# Webpack 初學者教學 :school: -Starting off with webpack was hard for me. I want to create a beginner friendly and comprehensive -intro to webpack. +Webpack 剛開始對我來說是很困難的。我想要建立一個對初學者友善,而且對 webpack 有非常全面的介紹。 -- [Part 1](https://github.com/AriaFallah/WebpackTutorial/tree/master/part1) - Webpack Basics and configuration. -- [Part 2](https://github.com/AriaFallah/WebpackTutorial/tree/master/part2) - Babel, linting, and environment variables. +- [Part 1](https://github.com/AriaFallah/WebpackTutorial/tree/master/part1) - Webpack 基礎和設定。 +- [Part 2](https://github.com/AriaFallah/WebpackTutorial/tree/master/part2) - Babel、Lint、以及環境變數。 diff --git a/part1/README.md b/part1/README.md index 0ac096b..d60fea1 100644 --- a/part1/README.md +++ b/part1/README.md @@ -1,133 +1,119 @@ -# Beginner Webpack Tutorial Part 1 - Introduction To Webpack :zap: +# Webpack 初學者教學 Part 1 - Webpack 簡介 :zap: -This is for the people like me whose first intro to webpack was a repository similar to: +對於像我這樣的人來說,第一次接觸到 webpack 是像是這些 repository: * https://github.com/davezuko/react-redux-starter-kit * https://github.com/webpack/react-starter -While these repositories are very well put together, they aren't necessarily the best learning tools. -In my case, I got pretty confused trying to understand what was going on, and scrapped together my -understanding from a lot of scattered resources. +雖然這些 repository 放在一起很棒,但它們不一定是最好的學習工具。 +像我的情況,我試著了解發生了那些事情,但還是很困惑,所以我決定不從這些大量而且分散的資源來理解。 -I hope that this tutorial can make Webpack easy to learn. +我希望這個教學可以讓 webpack 更容易的學習。 -## Requirements +## 需求 -At the very least you are expected to know the basics of node.js and npm. +至少,希望你了解基本的 node.js 和 npm。 -## Contributing +## 貢獻 -I will gladly accept any and all contributions/corrections. If you have any questions, -feel free to leave them as issues. If I made mistakes, please point them out. Finally, if you feel -that I left anything out, or could have explained something better make sure to leave an issue or -make a pull request. +我很樂意接受任何所有的貢獻或是修正。如果你有任何問題, +可以將這些問題發成 issue。如果我有錯誤的話,請將問題指出。最後,如果你覺得我漏了些什麼,或者可以將某些部分解釋的更好,留下一個 issue 或者是發送 Pull Request。 -## Table of Contents +## 內容列表 -* [Why Webpack?](#why-webpack) -* [The Basics](#the-basics) - * [Installation](#installation) +* [為什麼要 Webpack?](#why-webpack) +* [基礎](#the-basics) + * [安裝](#installation) * [Bundling](#bundling) * [Loaders](#loaders) * [Plugins](#plugins) -* [Your Config File](#your-config-file) - * [A Minimal Example](#a-minimal-example) - * [Introducing Plugins](#introducing-plugins) -* [A More Complete Example](#a-more-complete-example) - * [Introducing Loaders](#introducing-loaders) - * [Adding More Plugins](#adding-more-plugins) +* [你的設定檔案](#your-config-file) + * [一個簡單的範例](#a-minimal-example) + * [介紹 Plugins](#introducing-plugins) +* [一個更完整的範例](#a-more-complete-example) + * [介紹 Loaders](#introducing-loaders) + * [加入更多 Plugins](#adding-more-plugins) * [The Development Server](#the-development-server) * [Start Coding](#start-coding) -* [Conclusion](#conclusion) -* [Closing Thoughts](#closing-thoughts) +* [結論](#conclusion) +* [結束後的思考](#closing-thoughts) -## Why Webpack? +## 為什麼要 Webpack? -Because every single react/redux tutorial assumes you know it :cry: +因為每個 react 或 redux 教學都假設你知道什麼是 webpack。:cry: -More realistically here are some reasons you would want to use webpack. +以下這些是更現實的原因,你可能需要使用 webpack。 -Lets you: - * Bundle your js files into a single file - * Use npm packages in your frontend code - * Write ES6/ES7 JavaScript (with help from babel) - * Minify/Optimize code - * Turn LESS/SCSS into CSS - * Use HMR (Hot Module Replacement) - * Include any type of file into your JavaScript - * A lot more advanced stuff, which I won't cover +你可以: + * Bundle 你的 js 檔案變成單一的檔案 + * 在你的前端程式碼中使用 npm packages + * 撰寫 JavaScript ES6 或 ES7(需要透過 babel 來幫助) + * Minify 或優化程式碼 + * 將 LESS 或 SCSS 轉換成 CSS + * 使用 HMR(Hot Module Replacement) + * 包含任何類型的檔案到你的 JavaScript + * 更多進階的東西,暫時不介紹 -##### Why do I want these features? +##### 為什麼我需要這些功能? -* Bundle JS files - Lets you write modular JavaScript, but you do not need to include a separate -` ``` diff --git a/part1/example5/README.md b/part1/example5/README.md index cd9261d..dbfcc85 100644 --- a/part1/example5/README.md +++ b/part1/example5/README.md @@ -88,7 +88,7 @@ button { #### 備註 -我覺得我應該提到 `html-webpack-plugin` 需要謹慎使用。對我來說,如果你真的只是簡單的啟用一個 SPA,webpack 應該只產生 HTML 檔案。這裡只需要一個 HTML 檔案,我不會推薦它來產生 12 HTML 檔案,當作一個學習經驗。這意思不是說你不能使用 html 檔案之類的,像是 angular 指令,只需要 HTML template 檔案。在這個情況下你可以做類似像是: +我覺得我應該提到 `html-webpack-plugin` 需要謹慎使用。對我來說,如果你真的只是簡單的啟用一個 SPA,webpack 應該只產生 HTML 檔案。這裡只需要一個 HTML 檔案,我不會推薦它來產生 12 個 HTML 檔案,當作一個學習經驗。這意思不是說你不能使用 html 檔案之類的,像是 angular 指令,只需要 HTML template 檔案。在這個情況下你可以做類似像是: ```javascript // ...directive stuff @@ -108,4 +108,4 @@ new HtmlWebpackPlugin({ }) ``` -如果我錯了,請任何有這方面經驗的人隨時糾正我。 +如果我這個部份我搞錯了,請任何有這方面經驗的人隨時糾正我。 diff --git a/part1/example6/README.md b/part1/example6/README.md index 795da0f..4d68840 100644 --- a/part1/example6/README.md +++ b/part1/example6/README.md @@ -5,7 +5,7 @@ npm install -g webpack-dev-server npm install --save-dev webpack-dev-server -dev server 可以在瀏覽器看到你的網站是怎麼樣子以及可以更快速的開發,是一個相當有用的資源。預設情況下你可以拜訪 `http://localhost:8080`。不幸的是,像是 hot reloading 的功能並不是內建的,還需要一些其他的設定。 +dev server 可以在瀏覽器看到你的網站是怎麼樣子以及可以更快速的開發,是一個相當有用的資源。預設情況下你可以拜訪 `http://localhost:8080`。不幸的是,像是 hot reload 的功能並不是內建的,還需要一些其他的設定。 這是 webpack 設定檔一個很棒的分離點,意思是說你可以將他分成用於「development」以及「production」。因為我們必須在這個教學中保持簡單的方式,所不會有很大的改變,但是會介紹 webpack 很棒的可設置性。我們稱他們為 `webpack.config.dev.js` 和 `webpack.config.prod.js`。 @@ -95,7 +95,7 @@ module.exports = { } ``` -我也加入一個全新的屬性在 dev 和 prod 設定檔: +我也加入一個全新的屬性在 dev 和 prod 的設定檔: * [devtool](https://webpack.github.io/docs/configuration.html#devtool) - 這是協助 debug 的。基本上,當你得到一個錯誤,它會幫助你找到哪裡發生了錯誤,像是 chrome developer console。`source-map` 和 `cheap-eval-source-map` 之間的差異從文件說明有點難解釋。我可以肯定的是,`source-map` 是用於 production,`cheap-eval-source-map` 是用於 `development`。 diff --git a/part1/example7/README.md b/part1/example7/README.md index 89590e0..004fd7d 100644 --- a/part1/example7/README.md +++ b/part1/example7/README.md @@ -1,30 +1,24 @@ -# Example 7 - Start Coding +# 範例七 - 開始撰寫程式 -The reason most people seem to be flustered by webpack is the fact that they need to go through all -of this to get to the point where they finally write javascript; however, now we have arrived at the -climax of our tutorial. +大多數的人會慌亂的原因似乎是因為:webpack 事實上需要通過這些取得的進入點來撰寫 JavaScript;然而我們現在已經到達了這個教學課程最高潮的部分。 -Just in case you haven't already: do `npm run dev`, and navigate to `http://localhost:8080`. Setting -up that dev server with hot reloading wasn't for show. Every single time you save while editing -any part of your project, the browser will reload to show your changes. +如果你還沒準備好:執行 `npm run dev`,以及導到 `http://localhost:8080`。設定 dev server 是不是可以 hot reload。在你每次儲存你專案所編輯的任何一個檔案部份時,瀏覽器將會重新載入來顯示你的修改。 -We are also going to require a npm package just to demonstrate how you can use them in your -frontend now. +我們也需要一個 npm package,為了來示範如何在前端使用它們。 npm install --save pleasejs -PleaseJS is a random color generator, which we're going to hook up to our button to change the color -of our div. +PleaseJS 是一個隨機色彩的產生器,其中我們需要在按鈕中加入 hook 來改變我們的 div 顏色。 ```javascript // index.js -// Accept hot module reloading +// 接受 hot module reloading if (module.hot) { module.hot.accept() } -require('./styles.css') // The page is now styled +require('./styles.css') // 網頁現在有了樣式 var Please = require('pleasejs') var div = document.getElementById('color') var button = document.getElementById('button') @@ -36,8 +30,7 @@ function changeColor() { button.addEventListener('click', changeColor) ``` -Interestingly, [in order for Hot Module Replacement to work](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html#what-is-needed-to-use-it) -You need to include the code: +有趣的是,[為了讓 Hot Module Replacement 可以執行](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html#what-is-needed-to-use-it),你需要加入下面的程式碼: ```javascript if (module.hot) { @@ -45,4 +38,4 @@ if (module.hot) { } ``` -in a module or a parent of that module. +在一個 module 或是它的父 module。 diff --git a/part1/html-reload/README.md b/part1/html-reload/README.md index f1deaf5..be34c78 100644 --- a/part1/html-reload/README.md +++ b/part1/html-reload/README.md @@ -1,25 +1,17 @@ -# Extra - Make your HTML hot reload +# 番外 - 讓你的 HTML 可以 hot reload -So while I was testing I realized that index.html would not hot reload! After searching for a bit -I finally came across -[this helpful answer](http://stackoverflow.com/questions/33183931/how-to-watch-index-html-using-webpack-dev-server-and-html-webpack-plugin), -which will require -[one more loader](https://github.com/webpack/raw-loader) to make our hacky solution come to life, -and [one more plugin](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin) to prevent -our hacky solution from going into production. +所以,當我測試的時候我才知道 `index.html` 不會被 hot reload!在之後搜尋的結果,我最後找到了[有幫助的答案](http://stackoverflow.com/questions/33183931/how-to-watch-index-html-using-webpack-dev-server-and-html-webpack-plugin), +這需要一個 [loader](https://github.com/webpack/raw-loader) 來讓我們 hacky,也需要另一個 [plugin](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin) 來防止這個 hacky 到 production。 -Basically to make Webpack hot reload our HTML we need to make it part of our dependency tree by -requiring it in one of our files. In order to do this, we will be using the `raw-loader` loader, -which pulls our HTML into javascript as a string, but additionally will do exactly what we need: -add the HTML to the dependency tree. +基本上要讓 webpack 可以 hot reload 我們的 HTML,我們需要 require 我們其中檔案之一,讓它變成我們 dependency tree 的一部份。為了做到這一點,我們將會使用 `raw-loader` loader,它可以 pull 我們的 HTML 變成字串到我們的 JavaScript,但是另外我們真正需要做的是:加入 HTML 到 dependency tree。 -So lets add the loader to our dev config: +所以讓我們加入這個 loader 到我們的 dev 設定檔: -First we install with +首先我們需要安裝: npm install --save-dev raw-loader -Then we can add it: +然我們加入: ```javascript // webpack.config.dev.js @@ -50,7 +42,7 @@ module.exports = { loaders: ['style', 'css'] }, { test: /\.html$/, - loader: "raw-loader" // loaders: ['raw-loader'] is also perfectly acceptable. + loader: "raw-loader" // loaders: ['raw-loader'],這個方式也是可以被接受的。 }] }, devServer: { @@ -60,29 +52,22 @@ module.exports = { } ``` -This time, when you run `webpack`, if we `require` a file that ends in `.html`, then we will apply -the `raw-loader` loader to it, which adds the HTML to the bundle. +這個時候當你執行 `webpack`,如果們 `require` 一個檔案它的附檔名是 `.html`,然後我們將使用 `raw-loader` loader ,其中加入 HTML 到 bundle。 -Now in our currently empty `index.js` file we can do. +現在我們目前空的 `index.js` 檔案我們可以這麼做。 ```javascript // index.js require('./index.html') ``` -If you were to check now, hot reloading should be working, but also you should realize we just -required `index.html` in our `index.js`, and then did absolutely nothing with it. We don't want -to do that in production, which is why we will be using the handy dandy -[DefinePlugin](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin). +如果你現在要檢查 hot reload 是否可以執行,但是你必須要知道我們只是 require `index.html` 到我們的 `index.js`,這麼做絕對與 hot reload 無關。我們不想要在 production 使用 hot reload,這就是為什麼我們要使用 [DefinePlugin](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin)。 -The plugin lets us create a global constant for our entire bundle, which we could name anything, -such as `DONT_USE_IN_PRODUCTION: true`, but more practically, a popular choice that looks a bit more -familiar is `process.env.NODE_ENV: JSON.stringify('production')`. Why JSON.stringify? Because -according to the docs: +這個 plugin 讓我們建立一個全域的常數到我們整個 bundle,我們可以命名任何東西,像是:`DONT_USE_IN_PRODUCTION: true`,但實際上,更普遍的做法會像是 `process.env.NODE_ENV: JSON.stringify('production')`。為什麼要 `JSON.stringify`?根據文件的解釋: -> If the value is a string it will be used as a code fragment. +> 如果值是一個字串,它會被作為一個程式碼片段。 -Now that we know that, we'll be adding this plugin to our production config: +現在我們已經了解了,我們可以加入這個 plugin 到我們的 production 設定檔: ```javascript // webpack.config.prod.js @@ -120,7 +105,7 @@ module.exports = { } ``` -and in our `index.js` we will add a condition +並在我們的 `index.js` 加入一個條件判斷: ```javascript if (process.env.NODE_ENV !== 'production') { @@ -128,5 +113,4 @@ if (process.env.NODE_ENV !== 'production') { } ``` -Phew problem solved. In a production build, when we don't need the `index.html` as part of the -dependency tree, we won't require `index.html`, which also means we don't need the `raw-loader`. +解決問題啦。在 production build 中,我們不需要 `index.html` 做為 dependency tree 的一部份,我們不需要 `index.html`,這也意味著我們不需要 `raw-loader`。 From 6351938720e1f2d58a46ca69b80d442a1b7f1ab5 Mon Sep 17 00:00:00 2001 From: Peng-Jie Date: Wed, 4 May 2016 08:38:25 +0800 Subject: [PATCH 5/5] move translate file to new folder. save original file --- part1/README.md | 426 ++++++----- part1/css-extract/README.md | 19 +- part1/example1/README.md | 42 +- part1/example2/README.md | 18 +- part1/example3/README.md | 31 +- part1/example4/README.md | 31 +- part1/example5/README.md | 40 +- part1/example6/README.md | 60 +- part1/example7/README.md | 25 +- part1/html-reload/README.md | 48 +- zh-TW/part1/README.md | 695 ++++++++++++++++++ zh-TW/part1/css-extract/README.md | 61 ++ zh-TW/part1/css-extract/package.json | 24 + zh-TW/part1/css-extract/src/changeColor.js | 10 + zh-TW/part1/css-extract/src/index.html | 10 + zh-TW/part1/css-extract/src/index.js | 2 + zh-TW/part1/css-extract/src/styles.css | 19 + zh-TW/part1/css-extract/webpack.config.dev.js | 32 + .../part1/css-extract/webpack.config.prod.js | 31 + zh-TW/part1/example1/README.md | 81 ++ zh-TW/part1/example1/package.json | 21 + zh-TW/part1/example1/src/APIStuff.js | 2 + zh-TW/part1/example1/src/UIStuff.js | 4 + zh-TW/part1/example1/src/extraFile.js | 1 + zh-TW/part1/example1/src/index.js | 3 + zh-TW/part1/example1/src/styles.css | 3 + zh-TW/part1/example1/webpack.config.js | 24 + zh-TW/part1/example2/README.md | 37 + zh-TW/part1/example2/package.json | 12 + zh-TW/part1/example2/src/index.js | 1 + zh-TW/part1/example2/webpack.config.js | 9 + zh-TW/part1/example3/README.md | 67 ++ zh-TW/part1/example3/package.json | 15 + zh-TW/part1/example3/src/index.js | 1 + zh-TW/part1/example3/webpack.config.js | 18 + zh-TW/part1/example4/README.md | 70 ++ zh-TW/part1/example4/package.json | 15 + zh-TW/part1/example4/src/index.html | 0 zh-TW/part1/example4/src/index.js | 1 + zh-TW/part1/example4/src/styles.css | 0 zh-TW/part1/example4/webpack.config.js | 24 + zh-TW/part1/example5/README.md | 111 +++ zh-TW/part1/example5/package.json | 16 + zh-TW/part1/example5/src/index.html | 11 + zh-TW/part1/example5/src/index.js | 1 + zh-TW/part1/example5/src/styles.css | 19 + zh-TW/part1/example5/webpack.config.js | 28 + zh-TW/part1/example6/README.md | 136 ++++ zh-TW/part1/example6/package.json | 18 + zh-TW/part1/example6/src/index.html | 11 + zh-TW/part1/example6/src/index.js | 1 + zh-TW/part1/example6/src/styles.css | 19 + zh-TW/part1/example6/webpack.config.dev.js | 32 + zh-TW/part1/example6/webpack.config.prod.js | 29 + zh-TW/part1/example7/README.md | 41 ++ zh-TW/part1/example7/package.json | 23 + zh-TW/part1/example7/src/index.html | 10 + zh-TW/part1/example7/src/index.js | 16 + zh-TW/part1/example7/src/styles.css | 19 + zh-TW/part1/example7/webpack.config.dev.js | 32 + zh-TW/part1/example7/webpack.config.prod.js | 29 + zh-TW/part1/html-reload/README.md | 116 +++ zh-TW/part1/html-reload/package.json | 19 + zh-TW/part1/html-reload/src/index.html | 10 + zh-TW/part1/html-reload/src/index.js | 3 + zh-TW/part1/html-reload/src/styles.css | 20 + zh-TW/part1/html-reload/webpack.config.dev.js | 35 + .../part1/html-reload/webpack.config.prod.js | 32 + zh-TW/part2/README.md | 463 ++++++++++++ zh-TW/part2/example1/.babelrc | 3 + zh-TW/part2/example1/.eslintrc | 20 + zh-TW/part2/example1/package.json | 32 + zh-TW/part2/example1/src/index.html | 10 + zh-TW/part2/example1/src/index.js | 15 + zh-TW/part2/example1/src/styles.css | 19 + zh-TW/part2/example1/webpack.config.dev.js | 38 + zh-TW/part2/example1/webpack.config.prod.js | 38 + 77 files changed, 3210 insertions(+), 298 deletions(-) create mode 100644 zh-TW/part1/README.md create mode 100644 zh-TW/part1/css-extract/README.md create mode 100644 zh-TW/part1/css-extract/package.json create mode 100644 zh-TW/part1/css-extract/src/changeColor.js create mode 100644 zh-TW/part1/css-extract/src/index.html create mode 100644 zh-TW/part1/css-extract/src/index.js create mode 100644 zh-TW/part1/css-extract/src/styles.css create mode 100644 zh-TW/part1/css-extract/webpack.config.dev.js create mode 100644 zh-TW/part1/css-extract/webpack.config.prod.js create mode 100644 zh-TW/part1/example1/README.md create mode 100644 zh-TW/part1/example1/package.json create mode 100644 zh-TW/part1/example1/src/APIStuff.js create mode 100644 zh-TW/part1/example1/src/UIStuff.js create mode 100644 zh-TW/part1/example1/src/extraFile.js create mode 100644 zh-TW/part1/example1/src/index.js create mode 100644 zh-TW/part1/example1/src/styles.css create mode 100644 zh-TW/part1/example1/webpack.config.js create mode 100644 zh-TW/part1/example2/README.md create mode 100644 zh-TW/part1/example2/package.json create mode 100644 zh-TW/part1/example2/src/index.js create mode 100644 zh-TW/part1/example2/webpack.config.js create mode 100644 zh-TW/part1/example3/README.md create mode 100644 zh-TW/part1/example3/package.json create mode 100644 zh-TW/part1/example3/src/index.js create mode 100644 zh-TW/part1/example3/webpack.config.js create mode 100644 zh-TW/part1/example4/README.md create mode 100644 zh-TW/part1/example4/package.json create mode 100644 zh-TW/part1/example4/src/index.html create mode 100644 zh-TW/part1/example4/src/index.js create mode 100644 zh-TW/part1/example4/src/styles.css create mode 100644 zh-TW/part1/example4/webpack.config.js create mode 100644 zh-TW/part1/example5/README.md create mode 100644 zh-TW/part1/example5/package.json create mode 100644 zh-TW/part1/example5/src/index.html create mode 100644 zh-TW/part1/example5/src/index.js create mode 100644 zh-TW/part1/example5/src/styles.css create mode 100644 zh-TW/part1/example5/webpack.config.js create mode 100644 zh-TW/part1/example6/README.md create mode 100644 zh-TW/part1/example6/package.json create mode 100644 zh-TW/part1/example6/src/index.html create mode 100644 zh-TW/part1/example6/src/index.js create mode 100644 zh-TW/part1/example6/src/styles.css create mode 100644 zh-TW/part1/example6/webpack.config.dev.js create mode 100644 zh-TW/part1/example6/webpack.config.prod.js create mode 100644 zh-TW/part1/example7/README.md create mode 100644 zh-TW/part1/example7/package.json create mode 100644 zh-TW/part1/example7/src/index.html create mode 100644 zh-TW/part1/example7/src/index.js create mode 100644 zh-TW/part1/example7/src/styles.css create mode 100644 zh-TW/part1/example7/webpack.config.dev.js create mode 100644 zh-TW/part1/example7/webpack.config.prod.js create mode 100644 zh-TW/part1/html-reload/README.md create mode 100644 zh-TW/part1/html-reload/package.json create mode 100644 zh-TW/part1/html-reload/src/index.html create mode 100644 zh-TW/part1/html-reload/src/index.js create mode 100644 zh-TW/part1/html-reload/src/styles.css create mode 100644 zh-TW/part1/html-reload/webpack.config.dev.js create mode 100644 zh-TW/part1/html-reload/webpack.config.prod.js create mode 100644 zh-TW/part2/README.md create mode 100644 zh-TW/part2/example1/.babelrc create mode 100644 zh-TW/part2/example1/.eslintrc create mode 100644 zh-TW/part2/example1/package.json create mode 100644 zh-TW/part2/example1/src/index.html create mode 100644 zh-TW/part2/example1/src/index.js create mode 100644 zh-TW/part2/example1/src/styles.css create mode 100644 zh-TW/part2/example1/webpack.config.dev.js create mode 100644 zh-TW/part2/example1/webpack.config.prod.js diff --git a/part1/README.md b/part1/README.md index b89869c..0ac096b 100644 --- a/part1/README.md +++ b/part1/README.md @@ -1,118 +1,133 @@ -# Webpack 初學者教學課程 Part 1 - Webpack 簡介 :zap: +# Beginner Webpack Tutorial Part 1 - Introduction To Webpack :zap: -對於像我這樣的人來說,第一次接觸到 webpack 是像是這些 repository: +This is for the people like me whose first intro to webpack was a repository similar to: * https://github.com/davezuko/react-redux-starter-kit * https://github.com/webpack/react-starter -雖然這些 repository 放在一起很棒,但它們不一定是最好的學習工具。 -像我的情況,我試著了解發生了那些事情,但還是很困惑,所以我決定不從這些大量而且分散的資源來理解。 +While these repositories are very well put together, they aren't necessarily the best learning tools. +In my case, I got pretty confused trying to understand what was going on, and scrapped together my +understanding from a lot of scattered resources. -我希望這個教學課程可以讓 webpack 更容易的學習。 +I hope that this tutorial can make Webpack easy to learn. -## 需求 +## Requirements -至少,希望你了解基本的 node.js 和 npm。 +At the very least you are expected to know the basics of node.js and npm. -## 貢獻 +## Contributing -我很樂意接受任何所有的貢獻或是修正。如果你有任何問題, -可以將這些問題發成 issue。如果我有錯誤的話,請將問題指出。最後,如果你覺得我漏了些什麼,或者可以將某些部分解釋的更好,留下一個 issue 或者是發送 Pull Request。 +I will gladly accept any and all contributions/corrections. If you have any questions, +feel free to leave them as issues. If I made mistakes, please point them out. Finally, if you feel +that I left anything out, or could have explained something better make sure to leave an issue or +make a pull request. -## 內容列表 +## Table of Contents -* [為什麼要 Webpack?](#why-webpack) -* [基礎](#the-basics) - * [安裝](#installation) +* [Why Webpack?](#why-webpack) +* [The Basics](#the-basics) + * [Installation](#installation) * [Bundling](#bundling) * [Loaders](#loaders) * [Plugins](#plugins) -* [你的設定檔案](#your-config-file) - * [一個簡單的範例](#a-minimal-example) - * [介紹 Plugins](#introducing-plugins) -* [一個更完整的範例](#a-more-complete-example) - * [介紹 Loaders](#introducing-loaders) - * [加入更多 Plugins](#adding-more-plugins) - * [開發伺服器](#the-development-server) - * [開始撰寫程式](#start-coding) -* [結論](#conclusion) -* [結束後的思考](#closing-thoughts) +* [Your Config File](#your-config-file) + * [A Minimal Example](#a-minimal-example) + * [Introducing Plugins](#introducing-plugins) +* [A More Complete Example](#a-more-complete-example) + * [Introducing Loaders](#introducing-loaders) + * [Adding More Plugins](#adding-more-plugins) + * [The Development Server](#the-development-server) + * [Start Coding](#start-coding) +* [Conclusion](#conclusion) +* [Closing Thoughts](#closing-thoughts) -## 為什麼要 Webpack? +## Why Webpack? -因為每個 react 或 redux 教學課程都假設你知道什麼是 webpack。:cry: +Because every single react/redux tutorial assumes you know it :cry: -以下這些是更現實的原因,你可能需要使用 webpack。 +More realistically here are some reasons you would want to use webpack. -你可以: - * Bundle 你的 js 檔案變成單一的檔案 - * 在你的前端程式碼中使用 npm packages - * 撰寫 JavaScript ES6 或 ES7(需要透過 babel 來幫助) - * Minify 或優化程式碼 - * 將 LESS 或 SCSS 轉換成 CSS - * 使用 HMR(Hot Module Replacement) - * 包含任何類型的檔案到你的 JavaScript - * 更多進階的東西,暫時不介紹 +Lets you: + * Bundle your js files into a single file + * Use npm packages in your frontend code + * Write ES6/ES7 JavaScript (with help from babel) + * Minify/Optimize code + * Turn LESS/SCSS into CSS + * Use HMR (Hot Module Replacement) + * Include any type of file into your JavaScript + * A lot more advanced stuff, which I won't cover -##### 為什麼我需要這些功能? +##### Why do I want these features? -* Bundle JS 檔案 - 讓你可以撰寫模組化的 JavaScript,但是你不需要 inclue 每個 JavaScript ` ``` diff --git a/part1/example1/README.md b/part1/example1/README.md index 13b46db..9e88c0d 100644 --- a/part1/example1/README.md +++ b/part1/example1/README.md @@ -1,18 +1,20 @@ -# 範例一 - Bundle 和 Loader +# Example 1 - Bundling and Loaders ![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) -Webpack 簡稱為模組的整合工具。如果你想要深入的話,可以拜訪「modules」和「module bundling definitely」這兩篇優秀的解釋文章: -[JavaScript Modules: A Beginner’s Guide](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) -和 [JavaScript Modules Part 2: Module Bundling](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2)。 -我們要保持它的簡單,webpack 運作的方式是透過指定一個單一檔案作為你的進入點。 -這個檔案會是 tree 的 root。然後你每次 `require` 一個檔案從其他檔案並把它加入到 tree。當你執行 `webpack`,所有的檔案和 module 都會被 bundle 成一個檔案。 +Webpack is formally referred to as a module bundler. If you want an in-depth and accessible explanation +on modules and module bundling definitely check out these two great articles: +[here](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) +and [here](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2). +We're gonna keep it simple. The way that it works is that you specify a single file as your entry point. +This file will be the root of your tree. Then every time you `require` a file from another file it's +added to the tree. When you run `webpack`, all these files/modules are bundled into a single file. -這裡是一個簡單的範例: +Here's a simple example: ![Dependency Tree](http://i.imgur.com/dSghwwL.png) -根據這樣的情況,你可以有這樣的目錄: +Given this picture you could have the directory: ``` MyDirectory @@ -23,7 +25,7 @@ MyDirectory |- extraFile.js ``` -這些可能是你檔案的內容: +and this could be the content of your files ```javascript // index.js @@ -49,9 +51,10 @@ body { } ``` -當你執行 `webpack`,你會得到一個這個 tree 的 bundle 內容,雖然 `extraFile.js` 也是在相同的目錄中,但它不是被 bundle 的一部份,因為它在 `index.js` 沒有被 `require`。 +When you run `webpack`, you'll get a bundle with the contents of this tree, but `extraFile.js`, which was in the same directory, will not be part of +the bundle because it is not a part of the dependency tree: -`bundle.js` 看起來會像: +`bundle.js` will look like: ```javascript // contents of styles.css @@ -59,23 +62,26 @@ body { // contents of APIStuff.js + fetch ``` -被 bundle 的這些檔案是你明確所 require 進來的檔案。 +The things that are bundled are only the things that you explicitly required across your files. ### Loaders -你可能會注意到,我在上方的範例做了一些奇怪的事情。我在 JavaScript 檔案中 `require` 一個 css 檔案。 +As you probably noticed, I did something strange in the above example. I `required` a css file in a javascript file. -關於 webpack 真的很酷,有趣的事情是,你可以 `require` 更多不只是 JavaScript 的檔案。 +The really cool and interesting thing about webpack is that you can `require` more than just +javascript files. -在 webpack 這些東西我們稱為 loader。使用這些 loader,你可以 `require` 任何 `.css` 和 `.html` 到 `.png` 檔。 +There is this thing in webpack called a loader. Using these loaders, you can +`require` anything from `.css` and `.html` to `.png` files. -例如在上圖我有: +For example in the diagram above I had ```javascript // index.js require('./styles.css') ``` -如果我在我的 webpack 設定檔中,引入 [style-loader](https://github.com/webpack/style-loader) 和 [css-loader](https://github.com/webpack/css-loader),這不是完全有效的,但是實際上還是會應用 CSS 到我的網頁。 +If I include [the style-loader](https://github.com/webpack/style-loader) and the [the css-loader](https://github.com/webpack/css-loader) in my webpack config, this is not only perfectly +valid, but also will actually apply the CSS to my page. -你可以在 webpack 使用多個 loader,這裡只是一個單一的例子。 +This is just a single example of the many loaders you can use with webpack. diff --git a/part1/example2/README.md b/part1/example2/README.md index d29f900..4df2a6b 100644 --- a/part1/example2/README.md +++ b/part1/example2/README.md @@ -1,6 +1,6 @@ -# 範例二 - 一個簡單的範例 +# Example 2 - A minimal example -你的目錄結構像是這樣: +Say your directory structure looks like this: ``` MyDirectory @@ -11,7 +11,7 @@ MyDirectory ``` -然後這是一個非常簡易的 webpack 設定: +Then a very minimal webpack config you can make is this ```javascript // webpack.config.js @@ -26,12 +26,12 @@ module.exports = { } ``` -我們一個一個複習這些屬性: +Going over the new properties one by one: -* [entry](https://webpack.github.io/docs/configuration.html#entry) - 這是你的 bundle 的進入點,這是我們在討論 [bundling](#bundling) 的部分。`entry` 是一個陣列,根據你的需求,webpack 允許可以有多個進入點,來產生多個 bundle 檔案。 +* [entry](https://webpack.github.io/docs/configuration.html#entry) - The entrypoint of your bundle, which we discussed in the [bundling](#bundling) section. It's an array because webpack allows multiple entry points if you want to generate multiple bundles. -* [output](https://webpack.github.io/docs/configuration.html#output) - 由 webpack 規定的形式輸出。 - * [path](https://webpack.github.io/docs/configuration.html#output-path) - bundle 檔案位置。 - * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - bundle 檔案名稱。 +* [output](https://webpack.github.io/docs/configuration.html#output) - Dictates the form of the output by webpack + * [path](https://webpack.github.io/docs/configuration.html#output-path) - where to put the bundle + * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - what to call the bundle -當你執行 `webpack`,會在你的 dist 資料夾建立一個叫做 `bundle.js` 的檔案。 +When you run `webpack`, this will create a file called `bundle.js` in the dist folder. diff --git a/part1/example3/README.md b/part1/example3/README.md index 9ebe84b..03338d7 100644 --- a/part1/example3/README.md +++ b/part1/example3/README.md @@ -1,12 +1,15 @@ -# 範例三 - 介紹 Plugins +# Example 3 - Introducing Plugins -想像一下,你使用 webpack 將你的檔案 bundle 在一起,然後你意識到 bundle 後的結果是 900KB。這裡有個問題,但是你可以透過 minify 你的 bundle 檔案來做改善。如果需要做到這一點,你需要使用一個我在前面稍早提到的 [UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) plugin。 +Imagine that you've used webpack to bundle all your files together, and now you've realized that all +together it's 900KB. This is a problem that can be ameliorated by minifying your bundle. To do this +you need to use a plugin I mentioned earlier called the +[UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin). -此外,你需要在本機安裝 webpack 才能實際的去使用這個 plugin。 +Moreover you will need to have webpack installed locally to actually be able to use the plugin. npm install --save-dev webpack -現在你可以 require webpack 並 minify 你的程式碼。 +Now you can require webpack and minify your code. ```javascript // webpack.config.js @@ -29,18 +32,19 @@ module.exports = { ] } ``` -我們一個一個複習這些屬性: +Going over the new properties one by one: -* plugins - 一個可以儲存你的 plugin 的陣列。 - * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - Minify 你的程式碼,並顯示警告訊息。 +* plugins - An array that holds your plugins. + * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - Minify your code, and suppress warning messages. -這個時候,當我們執行 `webpack`,`UglifyJsPlugin` 通過像是移除所有空白的處理,可以將你的檔案減少至 200KB。 +This time, when you run `webpack`, now that you have the `UglifyJsPlugin` this could reduce your +imaginary 900KB file to 200KB by through processes such as removing all the whitespace. -你也可以加入 [OccurenceOrderPlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin)。 +You can also add the [OrderOccurencePlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin) -> 透過發生次數分配 module 和 chunk 的 id。一些常用的 Id 取得較低(短)的 id。這使得 id 可以預測,透過推薦來減少檔案的大小。 +> Assign the module and chunk ids by occurrence count. Ids that are used often get lower (shorter) ids. This make ids predictable, reduces to total file size and is recommended. -老實說,我不太確定底層的機制是如何工作的,但在目前包含 [webpack2 beta 的預設情況下](https://gist.github.com/sokra/27b24881210b56bbaff7),所以我將它包含在內。 +To be honest I'm not sure how the underlying mechanisms work, but in the current [webpack2 beta it's included by default](https://gist.github.com/sokra/27b24881210b56bbaff7) so I include it as well. ```javascript // webpack.config.js @@ -64,4 +68,7 @@ module.exports = { } ``` -所以現在我們寫了一個設定檔讓我們可以 minify 和 bundle 我們的 JavaScript。這個 bundle 檔案可以被複製並貼到其他的專案目錄中,放入 ` + +``` diff --git a/zh-TW/part1/css-extract/package.json b/zh-TW/part1/css-extract/package.json new file mode 100644 index 0000000..62c1bbf --- /dev/null +++ b/zh-TW/part1/css-extract/package.json @@ -0,0 +1,24 @@ +{ + "name": "css-extract", + "version": "1.0.0", + "description": "", + "main": "webpack.config.js", + "scripts": { + "build": "webpack --config webpack.config.prod.js", + "dev": "webpack-dev-server --config webpack.config.dev.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "devDependencies": { + "css-loader": "^0.23.1", + "extract-text-webpack-plugin": "^1.0.1", + "html-webpack-plugin": "^2.8.1", + "style-loader": "^0.13.0", + "webpack": "^1.12.13", + "webpack-dev-server": "^1.14.1" + }, + "dependencies": { + "pleasejs": "^0.4.2" + } +} diff --git a/zh-TW/part1/css-extract/src/changeColor.js b/zh-TW/part1/css-extract/src/changeColor.js new file mode 100644 index 0000000..021d194 --- /dev/null +++ b/zh-TW/part1/css-extract/src/changeColor.js @@ -0,0 +1,10 @@ +var Please = require('pleasejs') +var div = document.getElementById('color') +var button = document.getElementById('button') + +function changeColor() { + div.style.backgroundColor = Please.make_color() + console.log('pls do work') +} + +button.addEventListener('click', changeColor) diff --git a/zh-TW/part1/css-extract/src/index.html b/zh-TW/part1/css-extract/src/index.html new file mode 100644 index 0000000..55fff75 --- /dev/null +++ b/zh-TW/part1/css-extract/src/index.html @@ -0,0 +1,10 @@ + + + Webpack Tutorial + + +

Very Website

+
+ + + diff --git a/zh-TW/part1/css-extract/src/index.js b/zh-TW/part1/css-extract/src/index.js new file mode 100644 index 0000000..9daf7cf --- /dev/null +++ b/zh-TW/part1/css-extract/src/index.js @@ -0,0 +1,2 @@ +require('./styles.css') +require('./changeColor.js') diff --git a/zh-TW/part1/css-extract/src/styles.css b/zh-TW/part1/css-extract/src/styles.css new file mode 100644 index 0000000..1982ab4 --- /dev/null +++ b/zh-TW/part1/css-extract/src/styles.css @@ -0,0 +1,19 @@ +h1 { + color: rgb(114, 191, 190); + text-align: center; +} + +#color { + width: 300px; + height: 300px; + margin: 0 auto; +} + +button { + cursor: pointer; + display: block; + width: 100px; + outline: 0; + border: 0; + margin: 20px auto; +} diff --git a/zh-TW/part1/css-extract/webpack.config.dev.js b/zh-TW/part1/css-extract/webpack.config.dev.js new file mode 100644 index 0000000..db17445 --- /dev/null +++ b/zh-TW/part1/css-extract/webpack.config.dev.js @@ -0,0 +1,32 @@ +var path = require('path') +var webpack = require('webpack') +var HtmlWebpackPlugin = require('html-webpack-plugin') + +module.exports = { + devtool: 'cheap-eval-source-map', + entry: [ + 'webpack-dev-server/client?http://localhost:8080', + 'webpack/hot/dev-server', + './src/index' + ], + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + }, + plugins: [ + new webpack.HotModuleReplacementPlugin(), + new HtmlWebpackPlugin({ + template: './src/index.html' + }) + ], + module: { + loaders: [{ + test: /\.css$/, + loaders: ['style', 'css'] + }] + }, + devServer: { + contentBase: './dist', + hot: true + } +} diff --git a/zh-TW/part1/css-extract/webpack.config.prod.js b/zh-TW/part1/css-extract/webpack.config.prod.js new file mode 100644 index 0000000..e6078a4 --- /dev/null +++ b/zh-TW/part1/css-extract/webpack.config.prod.js @@ -0,0 +1,31 @@ +var path = require('path') +var webpack = require('webpack') +var HtmlWebpackPlugin = require('html-webpack-plugin') +var ExtractTextPlugin = require("extract-text-webpack-plugin"); + +module.exports = { + devtool: 'source-map', + entry: ['./src/index'], + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compressor: { + warnings: false, + }, + }), + new webpack.optimize.OccurenceOrderPlugin(), + new HtmlWebpackPlugin({ + template: './src/index.html' + }), + new ExtractTextPlugin("styles.css") + ], + module: { + loaders: [{ + test: /\.css$/, + loader: ExtractTextPlugin.extract("style-loader", "css-loader") + }] + } +} diff --git a/zh-TW/part1/example1/README.md b/zh-TW/part1/example1/README.md new file mode 100644 index 0000000..13b46db --- /dev/null +++ b/zh-TW/part1/example1/README.md @@ -0,0 +1,81 @@ +# 範例一 - Bundle 和 Loader + +![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) + +Webpack 簡稱為模組的整合工具。如果你想要深入的話,可以拜訪「modules」和「module bundling definitely」這兩篇優秀的解釋文章: +[JavaScript Modules: A Beginner’s Guide](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) +和 [JavaScript Modules Part 2: Module Bundling](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2)。 +我們要保持它的簡單,webpack 運作的方式是透過指定一個單一檔案作為你的進入點。 +這個檔案會是 tree 的 root。然後你每次 `require` 一個檔案從其他檔案並把它加入到 tree。當你執行 `webpack`,所有的檔案和 module 都會被 bundle 成一個檔案。 + +這裡是一個簡單的範例: + +![Dependency Tree](http://i.imgur.com/dSghwwL.png) + +根據這樣的情況,你可以有這樣的目錄: + +``` +MyDirectory +|- index.js +|- UIStuff.js +|- APIStuff.js +|- styles.css +|- extraFile.js +``` + +這些可能是你檔案的內容: + +```javascript +// index.js +require('./styles.css') +require('./UIStuff.js') +require('./APIStuff.js') + +// UIStuff.js +var React = require('React') +React.createClass({ + // stuff +}) + +// APIStuff.js +var fetch = require('fetch') // fetch polyfill +fetch('https://google.com') +``` + +```css +/* styles.css */ +body { + background-color: rgb(200, 56, 97); +} +``` + +當你執行 `webpack`,你會得到一個這個 tree 的 bundle 內容,雖然 `extraFile.js` 也是在相同的目錄中,但它不是被 bundle 的一部份,因為它在 `index.js` 沒有被 `require`。 + +`bundle.js` 看起來會像: + +```javascript +// contents of styles.css +// contents of UIStuff.js + React +// contents of APIStuff.js + fetch +``` + +被 bundle 的這些檔案是你明確所 require 進來的檔案。 + +### Loaders + +你可能會注意到,我在上方的範例做了一些奇怪的事情。我在 JavaScript 檔案中 `require` 一個 css 檔案。 + +關於 webpack 真的很酷,有趣的事情是,你可以 `require` 更多不只是 JavaScript 的檔案。 + +在 webpack 這些東西我們稱為 loader。使用這些 loader,你可以 `require` 任何 `.css` 和 `.html` 到 `.png` 檔。 + +例如在上圖我有: + +```javascript +// index.js +require('./styles.css') +``` + +如果我在我的 webpack 設定檔中,引入 [style-loader](https://github.com/webpack/style-loader) 和 [css-loader](https://github.com/webpack/css-loader),這不是完全有效的,但是實際上還是會應用 CSS 到我的網頁。 + +你可以在 webpack 使用多個 loader,這裡只是一個單一的例子。 diff --git a/zh-TW/part1/example1/package.json b/zh-TW/part1/example1/package.json new file mode 100644 index 0000000..ec3e2c0 --- /dev/null +++ b/zh-TW/part1/example1/package.json @@ -0,0 +1,21 @@ +{ + "name": "example1", + "version": "1.0.0", + "description": "![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png)", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "MIT", + "dependencies": { + "fetch": "^1.0.0", + "react": "^0.14.7" + }, + "devDependencies": { + "css-loader": "^0.23.1", + "style-loader": "^0.13.0", + "webpack": "^1.12.14" + } +} diff --git a/zh-TW/part1/example1/src/APIStuff.js b/zh-TW/part1/example1/src/APIStuff.js new file mode 100644 index 0000000..fc52488 --- /dev/null +++ b/zh-TW/part1/example1/src/APIStuff.js @@ -0,0 +1,2 @@ +var fetch = require('fetch') // fetch polyfill +fetch('https://google.com') diff --git a/zh-TW/part1/example1/src/UIStuff.js b/zh-TW/part1/example1/src/UIStuff.js new file mode 100644 index 0000000..3b28df6 --- /dev/null +++ b/zh-TW/part1/example1/src/UIStuff.js @@ -0,0 +1,4 @@ +var React = require('React') +React.createClass({ + // stuff +}) diff --git a/zh-TW/part1/example1/src/extraFile.js b/zh-TW/part1/example1/src/extraFile.js new file mode 100644 index 0000000..eb8c6f1 --- /dev/null +++ b/zh-TW/part1/example1/src/extraFile.js @@ -0,0 +1 @@ +console.log('No one likes me') diff --git a/zh-TW/part1/example1/src/index.js b/zh-TW/part1/example1/src/index.js new file mode 100644 index 0000000..7f20fdc --- /dev/null +++ b/zh-TW/part1/example1/src/index.js @@ -0,0 +1,3 @@ +require('./styles.css') +require('./UIStuff.js') +require('./APIStuff.js') diff --git a/zh-TW/part1/example1/src/styles.css b/zh-TW/part1/example1/src/styles.css new file mode 100644 index 0000000..481b3b9 --- /dev/null +++ b/zh-TW/part1/example1/src/styles.css @@ -0,0 +1,3 @@ +body { + background-color: rgb(200, 56, 97); +} diff --git a/zh-TW/part1/example1/webpack.config.js b/zh-TW/part1/example1/webpack.config.js new file mode 100644 index 0000000..04c29eb --- /dev/null +++ b/zh-TW/part1/example1/webpack.config.js @@ -0,0 +1,24 @@ +var path = require('path') +var webpack = require('webpack') + +module.exports = { + entry: ['./src/index'], // .js after index is optional + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compressor: { + warnings: false, + }, + }), + new webpack.optimize.OccurenceOrderPlugin() + ], + module: { + loaders: [{ + test: /\.css$/, + loaders: ['style', 'css'] + }] + } +} diff --git a/zh-TW/part1/example2/README.md b/zh-TW/part1/example2/README.md new file mode 100644 index 0000000..d29f900 --- /dev/null +++ b/zh-TW/part1/example2/README.md @@ -0,0 +1,37 @@ +# 範例二 - 一個簡單的範例 + +你的目錄結構像是這樣: + +``` +MyDirectory +|- dist +|- src + |- index.js +|- webpack.config.js + +``` + +然後這是一個非常簡易的 webpack 設定: + +```javascript +// webpack.config.js +var path = require('path') + +module.exports = { + entry: ['./src/index'], // .js after index is optional + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + } +} +``` + +我們一個一個複習這些屬性: + +* [entry](https://webpack.github.io/docs/configuration.html#entry) - 這是你的 bundle 的進入點,這是我們在討論 [bundling](#bundling) 的部分。`entry` 是一個陣列,根據你的需求,webpack 允許可以有多個進入點,來產生多個 bundle 檔案。 + +* [output](https://webpack.github.io/docs/configuration.html#output) - 由 webpack 規定的形式輸出。 + * [path](https://webpack.github.io/docs/configuration.html#output-path) - bundle 檔案位置。 + * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - bundle 檔案名稱。 + +當你執行 `webpack`,會在你的 dist 資料夾建立一個叫做 `bundle.js` 的檔案。 diff --git a/zh-TW/part1/example2/package.json b/zh-TW/part1/example2/package.json new file mode 100644 index 0000000..91d0f15 --- /dev/null +++ b/zh-TW/part1/example2/package.json @@ -0,0 +1,12 @@ +{ + "name": "example2", + "version": "1.0.0", + "description": "", + "main": "webpack.config.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "MIT" +} diff --git a/zh-TW/part1/example2/src/index.js b/zh-TW/part1/example2/src/index.js new file mode 100644 index 0000000..bf6dbff --- /dev/null +++ b/zh-TW/part1/example2/src/index.js @@ -0,0 +1 @@ +console.log('It works!') diff --git a/zh-TW/part1/example2/webpack.config.js b/zh-TW/part1/example2/webpack.config.js new file mode 100644 index 0000000..41c465f --- /dev/null +++ b/zh-TW/part1/example2/webpack.config.js @@ -0,0 +1,9 @@ +var path = require('path') + +module.exports = { + entry: ['./src/index'], // .js after index is optional + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + } +} diff --git a/zh-TW/part1/example3/README.md b/zh-TW/part1/example3/README.md new file mode 100644 index 0000000..9ebe84b --- /dev/null +++ b/zh-TW/part1/example3/README.md @@ -0,0 +1,67 @@ +# 範例三 - 介紹 Plugins + +想像一下,你使用 webpack 將你的檔案 bundle 在一起,然後你意識到 bundle 後的結果是 900KB。這裡有個問題,但是你可以透過 minify 你的 bundle 檔案來做改善。如果需要做到這一點,你需要使用一個我在前面稍早提到的 [UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) plugin。 + +此外,你需要在本機安裝 webpack 才能實際的去使用這個 plugin。 + + npm install --save-dev webpack + +現在你可以 require webpack 並 minify 你的程式碼。 + +```javascript +// webpack.config.js +var path = require('path') +var webpack = require('webpack') + +module.exports = { + entry: ['./src/index'], + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + }, + + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compressor: { + warnings: false, + }, + }) + ] +} +``` +我們一個一個複習這些屬性: + +* plugins - 一個可以儲存你的 plugin 的陣列。 + * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - Minify 你的程式碼,並顯示警告訊息。 + +這個時候,當我們執行 `webpack`,`UglifyJsPlugin` 通過像是移除所有空白的處理,可以將你的檔案減少至 200KB。 + +你也可以加入 [OccurenceOrderPlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin)。 + +> 透過發生次數分配 module 和 chunk 的 id。一些常用的 Id 取得較低(短)的 id。這使得 id 可以預測,透過推薦來減少檔案的大小。 + +老實說,我不太確定底層的機制是如何工作的,但在目前包含 [webpack2 beta 的預設情況下](https://gist.github.com/sokra/27b24881210b56bbaff7),所以我將它包含在內。 + +```javascript +// webpack.config.js +var path = require('path') +var webpack = require('webpack') + +module.exports = { + entry: ['./src/index'], + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compressor: { + warnings: false, + }, + }), + new webpack.optimize.OccurenceOrderPlugin() + ] +} +``` + +所以現在我們寫了一個設定檔讓我們可以 minify 和 bundle 我們的 JavaScript。這個 bundle 檔案可以被複製並貼到其他的專案目錄中,放入 `