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 檔案可以被複製並貼到其他的專案目錄中,放入 `