├── .gitignore ├── LICENSE ├── README.md ├── ko-arahansa ├── README.md ├── part1 │ ├── README.md │ ├── css-extract │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── changeColor.js │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ ├── example1 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── APIStuff.js │ │ │ ├── UIStuff.js │ │ │ ├── extraFile.js │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example2 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ └── index.js │ │ └── webpack.config.js │ ├── example3 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ └── index.js │ │ └── webpack.config.js │ ├── example4 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example5 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example6 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ ├── example7 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ └── html-reload │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js └── part2 │ ├── README.md │ └── example1 │ ├── .babelrc │ ├── .eslintrc │ ├── package.json │ ├── src │ ├── index.html │ ├── index.js │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── part1 ├── README.md ├── css-extract │ ├── README.md │ ├── package.json │ ├── src │ │ ├── changeColor.js │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── example1 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── APIStuff.js │ │ ├── UIStuff.js │ │ ├── extraFile.js │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example2 │ ├── README.md │ ├── package.json │ ├── src │ │ └── index.js │ └── webpack.config.js ├── example3 │ ├── README.md │ ├── package.json │ ├── src │ │ └── index.js │ └── webpack.config.js ├── example4 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example5 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example6 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── example7 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js └── html-reload │ ├── README.md │ ├── package.json │ ├── src │ ├── index.html │ ├── index.js │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── part2 ├── README.md └── example1 │ ├── .babelrc │ ├── .eslintrc │ ├── package.json │ ├── src │ ├── index.html │ ├── index.js │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── zh-TW ├── README.md ├── part1 │ ├── README.md │ ├── css-extract │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── changeColor.js │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ ├── example1 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── APIStuff.js │ │ │ ├── UIStuff.js │ │ │ ├── extraFile.js │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example2 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ └── index.js │ │ └── webpack.config.js │ ├── example3 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ └── index.js │ │ └── webpack.config.js │ ├── example4 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example5 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ └── webpack.config.js │ ├── example6 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ ├── example7 │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js │ └── html-reload │ │ ├── README.md │ │ ├── package.json │ │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ │ ├── webpack.config.dev.js │ │ └── webpack.config.prod.js └── part2 │ ├── README.md │ └── example1 │ ├── .babelrc │ ├── .eslintrc │ ├── package.json │ ├── src │ ├── index.html │ ├── index.js │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js └── zh-cn ├── README.md ├── part1 ├── README.md ├── css-extract │ ├── README.md │ ├── package.json │ ├── src │ │ ├── changeColor.js │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── example1 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── APIStuff.js │ │ ├── UIStuff.js │ │ ├── extraFile.js │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example2 │ ├── README.md │ ├── package.json │ ├── src │ │ └── index.js │ └── webpack.config.js ├── example3 │ ├── README.md │ ├── package.json │ ├── src │ │ └── index.js │ └── webpack.config.js ├── example4 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example5 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ └── webpack.config.js ├── example6 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js ├── example7 │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.html │ │ ├── index.js │ │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js └── html-reload │ ├── README.md │ ├── package.json │ ├── src │ ├── index.html │ ├── index.js │ └── styles.css │ ├── webpack.config.dev.js │ └── webpack.config.prod.js └── part2 ├── README.md └── example1 ├── .babelrc ├── .eslintrc ├── package.json ├── src ├── index.html ├── index.js └── styles.css ├── webpack.config.dev.js └── webpack.config.prod.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | bundle.js 4 | npm-debug.log* 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Aria Fallah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beginner Webpack Tutorial :school: 2 | 3 | Starting off with webpack was hard for me. I want to create a beginner friendly and comprehensive 4 | intro to webpack. 5 | 6 | - [Part 1](https://github.com/AriaFallah/WebpackTutorial/tree/master/part1) - Webpack Basics and configuration. 7 | - [Part 2](https://github.com/AriaFallah/WebpackTutorial/tree/master/part2) - Babel, linting, and environment variables. 8 | 9 | ### Translations 10 | - [Chinese Traditional](https://github.com/AriaFallah/WebpackTutorial/tree/master/zh-TW) 11 | - [简体中文](./zh-cn) 12 | -------------------------------------------------------------------------------- /ko-arahansa/README.md: -------------------------------------------------------------------------------- 1 | # 입문자를 위한 Webpack 튜토리얼 :school: 2 | 3 | 웹팩을 시작하는 것은 나에게 매우 힘들었지. 난 그래서 웹팩 입문자를 위한 친절하고 이해하기 쉬운 자료를 만들고 싶었어. 4 | 5 | - [Part 1](https://github.com/AriaFallah/WebpackTutorial/tree/master/ko-arahansa/part1) - 웹팩 기초와 설정을 다룬다. 6 | - [Part 2](https://github.com/AriaFallah/WebpackTutorial/tree/master/ko-arahansa/part2) - Babel, linting과 환경 변수들 7 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/README.md: -------------------------------------------------------------------------------- 1 | # Extra - Extract Your CSS 2 | 3 | 예. 당신이 마쳤습니다만, production 에서는 css 파일이 javascript안에 들어가기를 원하지 않습니다. 이러한 목적을 위해서 우리는 4 | [Extract Text Plugin](https://github.com/webpack/extract-text-webpack-plugin). 5 | 을 사용할 것입니다. 6 | 7 | npm install --save-dev extract-text-webpack-plugin 8 | 9 | 그리고 우리의 웹팩 production설정을 다음과 같이 바꿔줘야합니다 : 10 | 11 | ```javascript 12 | var path = require('path') 13 | var webpack = require('webpack') 14 | var HtmlWebpackPlugin = require('html-webpack-plugin') 15 | var ExtractTextPlugin = require("extract-text-webpack-plugin"); 16 | 17 | module.exports = { 18 | devtool: 'source-map', 19 | entry: ['./src/index'], 20 | output: { 21 | path: path.join(__dirname, 'dist'), 22 | filename: 'bundle.js' 23 | }, 24 | plugins: [ 25 | new webpack.optimize.UglifyJsPlugin({ 26 | compressor: { 27 | warnings: false, 28 | }, 29 | }), 30 | new webpack.optimize.OccurenceOrderPlugin(), 31 | new HtmlWebpackPlugin({ 32 | template: './src/index.html' 33 | }), 34 | new ExtractTextPlugin("styles.css") 35 | ], 36 | module: { 37 | loaders: [{ 38 | test: /\.css$/, 39 | loader: ExtractTextPlugin.extract("style-loader", "css-loader") 40 | }] 41 | } 42 | } 43 | ``` 44 | 45 | 이러한 변화는 [official-repo](https://github.com/webpack/extract-text-webpack-plugin) 에서 유래했습니다. 어떻게 이것이 동작하는 지 알고 싶다면 한번 살펴보세요. 46 | 47 | > 엔트리 청크에서 모든 require("style.css")들은 분리된 css 아웃풋 파일로 이동합니다. 그러니 당신의 스타일들은 더 이상 자바스크립트 안에 들어가지않고 css 번들파일안에 들어가게 됩니다. 만약 당신의 전체적인 스타일 시트 볼륨이 크다면, 이게 더 빠를 겁니다. 왜냐하면 스타일시트번들은 자바스크립트 번들과 병렬로 불러와지기 때문입니다. 48 | 49 | 이제 당신이 `npm run build`를 하면 설정을 사용하여 CSS는 분리된 파일로 들어가게 되고, 흥미롭게도 이미 `inde.html`에 포함(include)될 것입니다. 50 | ```html 51 | 52 | 53 | Webpack Tutorial 54 | <------ MAGICALLY ADDED 55 | 56 | 57 |

Very Website

58 |
59 | 60 | 61 | 62 | ``` 63 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "css-extract", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "build": "webpack --config webpack.config.prod.js", 8 | "dev": "webpack-dev-server --config webpack.config.dev.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "MIT", 13 | "devDependencies": { 14 | "css-loader": "^0.23.1", 15 | "extract-text-webpack-plugin": "^1.0.1", 16 | "html-webpack-plugin": "^2.8.1", 17 | "style-loader": "^0.13.0", 18 | "webpack": "^1.12.13", 19 | "webpack-dev-server": "^1.14.1" 20 | }, 21 | "dependencies": { 22 | "pleasejs": "^0.4.2" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/src/changeColor.js: -------------------------------------------------------------------------------- 1 | var Please = require('pleasejs') 2 | var div = document.getElementById('color') 3 | var button = document.getElementById('button') 4 | 5 | function changeColor() { 6 | div.style.backgroundColor = Please.make_color() 7 | console.log('pls do work') 8 | } 9 | 10 | button.addEventListener('click', changeColor) 11 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Webpack Tutorial 4 | 5 | 6 |

Very Website

7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./changeColor.js') 3 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/src/styles.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: rgb(114, 191, 190); 3 | text-align: center; 4 | } 5 | 6 | #color { 7 | width: 300px; 8 | height: 300px; 9 | margin: 0 auto; 10 | } 11 | 12 | button { 13 | cursor: pointer; 14 | display: block; 15 | width: 100px; 16 | outline: 0; 17 | border: 0; 18 | margin: 20px auto; 19 | } 20 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | devtool: 'cheap-eval-source-map', 7 | entry: [ 8 | 'webpack-dev-server/client?http://localhost:8080', 9 | 'webpack/hot/dev-server', 10 | './src/index' 11 | ], 12 | output: { 13 | path: path.join(__dirname, 'dist'), 14 | filename: 'bundle.js' 15 | }, 16 | plugins: [ 17 | new webpack.HotModuleReplacementPlugin(), 18 | new HtmlWebpackPlugin({ 19 | template: './src/index.html' 20 | }) 21 | ], 22 | module: { 23 | loaders: [{ 24 | test: /\.css$/, 25 | loaders: ['style', 'css'] 26 | }] 27 | }, 28 | devServer: { 29 | contentBase: './dist', 30 | hot: true 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ko-arahansa/part1/css-extract/webpack.config.prod.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | var ExtractTextPlugin = require("extract-text-webpack-plugin"); 5 | 6 | module.exports = { 7 | devtool: 'source-map', 8 | entry: ['./src/index'], 9 | output: { 10 | path: path.join(__dirname, 'dist'), 11 | filename: 'bundle.js' 12 | }, 13 | plugins: [ 14 | new webpack.optimize.UglifyJsPlugin({ 15 | compressor: { 16 | warnings: false, 17 | }, 18 | }), 19 | new webpack.optimize.OccurrenceOrderPlugin(), 20 | new HtmlWebpackPlugin({ 21 | template: './src/index.html' 22 | }), 23 | new ExtractTextPlugin("styles.css") 24 | ], 25 | module: { 26 | loaders: [{ 27 | test: /\.css$/, 28 | loader: ExtractTextPlugin.extract("style-loader", "css-loader") 29 | }] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/README.md: -------------------------------------------------------------------------------- 1 | # Example 1 - Bundling and Loaders 2 | 3 | ![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) 4 | 5 | 번들링 [예제 1](https://github.com/AriaFallah/WebpackTutorial/tree/master/ko-arahansa/part1/example1) 6 | 7 | ![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) 8 | 9 | 웹팩은 공식적으로 모듈 번들러로 얘기됩니다.만약 당신이 좀 더 모듈과 모듈번들링에 대해서 깊이 있는 정보를 원한다면 여기 두개의 무척 좋은 글을 읽어보세요 10 | [here](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) 11 | and [here](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2). 12 | 13 | 우리는 단순하게 갈 것입니다. 이것이 동작하는 방식은 당신이 하나의 파일을 엔트리 포인트로 설정하는 것입니다. 14 | 이 파일은 당신의 트리의 root가 될 것인데 당신이 다른 파일로부터 파일을 `require` 할 때마다 이 트리에 추가가 되게 됩니다. 당신이 `webpack`을 치면 모든 이러한 파일/모듈들은 하나의 단일 파일로 번들링되게 됩니다. 15 | 16 | 여기 예제가 있습니다. : 17 | 18 | ![Dependency Tree](http://i.imgur.com/dSghwwL.png) 19 | 20 | 이 사진은 디렉터리를 보여줍니다 : 21 | 22 | ``` 23 | MyDirectory 24 | |- index.js 25 | |- UIStuff.js 26 | |- APIStuff.js 27 | |- styles.css 28 | |- extraFile.js 29 | ``` 30 | 31 | 그리고 이것은 당신의 파일의 내용을 보여줍니다. 32 | 33 | ```javascript 34 | // index.js 35 | require('./styles.css') 36 | require('./UIStuff.js') 37 | require('./APIStuff.js') 38 | 39 | // UIStuff.js 40 | var React = require('React') 41 | React.createClass({ 42 | // stuff 43 | }) 44 | 45 | // APIStuff.js 46 | var fetch = require('fetch') // fetch polyfill 47 | fetch('https://google.com') 48 | ``` 49 | 50 | ```css 51 | /* styles.css */ 52 | body { 53 | background-color: rgb(200, 56, 97); 54 | } 55 | ``` 56 | 57 | 만약 당신이 `webpack` 을 치면 당신은 이 계층구조가 가진 내용들의 번들을 받게 됩니다. 하지만 같은 폴더 내에 있어도 `required`되지 않은 `extraFile.js`은 번들링에 포함되지 않게 됩니다. 58 | 59 | `bundle.js` 는 다음과 같을 것입니다 : 60 | 61 | ```javascript 62 | // contents of styles.css 63 | // contents of UIStuff.js + React 64 | // contents of APIStuff.js + fetch 65 | ``` 66 | 67 | 번들되는 것은 당신이 명시적으로 required 한 것들만 번들링됩니다. 68 | 69 | ### Loaders 70 | 71 | (로더) 당신은 아마 눈치 챘겠지만, 위의 예제에서 제가 뭔가 이상한 것을 했었습니다. 저는 자바스크립트 파일에 css파일을 `required` 했습니다. 72 | 73 | 자바스크립트 이외의 것들을 `require` 할 수 있다는 것은 웹팩에서 꽤 좋으면서 흥미롭습니다. 74 | 웹팩에서는 로더라는 것이 존재하는 데, 이러한 로더들을 사용하여서 당신은 `.css` 와 `.png`, `.html` 파일들 같은 것 무엇이든지 `require`할 수 있습니다. 75 | 76 | 예로, 위의 다이어그램에서 77 | 78 | ```javascript 79 | // index.js 80 | require('./styles.css') 81 | ``` 82 | 83 | 만약 제가 웹팩설정에 [the style-loader](https://github.com/webpack/style-loader) 와 [the css-loader](https://github.com/webpack/css-loader)를 추가하는 것은 유효할 뿐만 아니라, 제 페이지에 실제로 적용될 것입니다. 84 | 85 | 이것은 당신이 웹팩을 가지고서 할 수 있는 많은 로더들의 하나의 예일뿐입니다. -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example1", 3 | "version": "1.0.0", 4 | "description": "![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png)", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "fetch": "^1.0.0", 14 | "react": "^0.14.7" 15 | }, 16 | "devDependencies": { 17 | "css-loader": "^0.23.1", 18 | "style-loader": "^0.13.0", 19 | "webpack": "^1.12.14" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/src/APIStuff.js: -------------------------------------------------------------------------------- 1 | var fetch = require('fetch') // fetch polyfill 2 | fetch('https://google.com') 3 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/src/UIStuff.js: -------------------------------------------------------------------------------- 1 | var React = require('React') 2 | React.createClass({ 3 | // stuff 4 | }) 5 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/src/extraFile.js: -------------------------------------------------------------------------------- 1 | console.log('No one likes me') 2 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./UIStuff.js') 3 | require('./APIStuff.js') 4 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/src/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: rgb(200, 56, 97); 3 | } 4 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example1/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | 4 | module.exports = { 5 | entry: ['./src/index'], // .js after index is optional 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | plugins: [ 11 | new webpack.optimize.UglifyJsPlugin({ 12 | compressor: { 13 | warnings: false, 14 | }, 15 | }), 16 | new webpack.optimize.OccurrenceOrderPlugin() 17 | ], 18 | module: { 19 | loaders: [{ 20 | test: /\.css$/, 21 | loaders: ['style', 'css'] 22 | }] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example2/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 - A minimal example 2 | 3 | (최소한의 예제) 4 | [예제 2](https://github.com/AriaFallah/WebpackTutorial/tree/master/ko-arahansa/part1/example2) 5 | 6 | 당신의 디렉터리 구조가 다음과 같다고 합시다 : 7 | 8 | ``` 9 | MyDirectory 10 | |- dist 11 | |- src 12 | |- index.js 13 | |- webpack.config.js 14 | 15 | ``` 16 | 17 | 그러면 당신의 최소 웹팩 설정은 다음과 같이 만들 수 있을 것입니다. 18 | 19 | ```javascript 20 | // webpack.config.js 21 | var path = require('path') 22 | 23 | module.exports = { 24 | entry: ['./src/index'], // file extension after index is optional for .js files 25 | output: { 26 | path: path.join(__dirname, 'dist'), 27 | filename: 'bundle.js' 28 | } 29 | } 30 | ``` 31 | 32 | 새로운 속성들을 하나하나씩 알아보겠습니다 : 33 | 34 | * [entry](https://webpack.github.io/docs/configuration.html#entry) - 당신의 번들의 엔트리포인트입니다. [번들링](#bundling) 섹션에서 좀 더 알아볼 것입니다.. 35 | 36 | 웹팩은 여러개의 번들을 생성하기를 원하면, 여러개의 엔트리 포인트들을 허용하기 때문에 엔트리포인트는 배열입니다. 37 | 38 | * [output](https://webpack.github.io/docs/configuration.html#output) - 웹팩에 의해 출력되는 형태를 나타냅니다. 39 | * [path](https://webpack.github.io/docs/configuration.html#output-path) - 번들을 놓을 곳입니다. 40 | * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - 번들을 어떻게 부를 지 나타냅니다. 41 | 42 | `webpack`을 실행하면 이것은 `bundle.js` 라는 파일을 dist폴더 안에 생성할 것입니다. 43 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example2", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT" 12 | } 13 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example2/src/index.js: -------------------------------------------------------------------------------- 1 | console.log('It works!') 2 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example2/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | 3 | module.exports = { 4 | entry: ['./src/index'], // .js after index is optional 5 | output: { 6 | path: path.join(__dirname, 'dist'), 7 | filename: 'bundle.js' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example3/README.md: -------------------------------------------------------------------------------- 1 | # Example 3 - Introducing Plugins 2 | 3 | 당신이 모든 파일들을 한꺼번에 번들링했다가 당신이 그것이 900KB라는 것을 깨달았다고 해봅시다. 이것은 번들을 최소화함으로써 개선될 수 있습니다. 제가 이전에 언급한 [UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin)를 사용하여서 이러한 문제를 해결할 수 있습니다. 4 | 5 | 당신은 플러그인을 사용하려고 웹팩을 로컬로 설치하고 싶을 겁니다. 6 | 7 | npm install --save-dev webpack 8 | 9 | 당신은 이제 웹팩을 사용하여서 코드를 최소화 하세요 10 | 11 | ```javascript 12 | // webpack.config.js 13 | var path = require('path') 14 | var webpack = require('webpack') 15 | 16 | module.exports = { 17 | entry: ['./src/index'], 18 | output: { 19 | path: path.join(__dirname, 'dist'), 20 | filename: 'bundle.js' 21 | }, 22 | 23 | plugins: [ 24 | new webpack.optimize.UglifyJsPlugin({ 25 | compressor: { 26 | warnings: false, 27 | }, 28 | }) 29 | ] 30 | } 31 | ``` 32 | 33 | 새로운 속성들을 하나하나 알아보도록 하겠습니다 : 34 | 35 | * plugins - 플러그인들을 적을 배열. 36 | * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - 코드를 최소화하고 경고 메시지를 숨깁니다. 37 | 38 | 여기서 당신이 `webpack`을 칠 때, 당신은 `UglifyJsPlugin`을 가지고서 공백을 제거함으로써 900KB를 200KB 까지 줄일 수 있습니다. 39 | 40 | 당신은 또한 [OccurrenceOrderPlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin) 을 추가할 수가 있습니다. 41 | 42 | > 모듈을 할당하고 발생 카운트 아이디들을 발생(?chunk)시킵니다. ID들은 종종 적은(짧은) id들을 얻는데 사용됩니다. 이것은 id가 예상가능하며 파일 전체 크기를 경감시켜 추천됩니다. 43 | 44 | 솔직히 어떤 메커니즘이 하부에 있는 지 모르겠지만 현재(1버젼?)에서는 기본으로 들어가지 않기 때문에 [webpack2 beta it's included by default](https://gist.github.com/sokra/27b24881210b56bbaff7), 이것또한 추가해줬습니다 . 45 | 46 | 47 | ```JavaScript 48 | // webpack.config.js 49 | var path = require('path') 50 | var webpack = require('webpack') 51 | 52 | module.exports = { 53 | entry: ['./src/index'], 54 | output: { 55 | path: path.join(__dirname, 'dist'), 56 | filename: 'bundle.js' 57 | }, 58 | plugins: [ 59 | new webpack.optimize.UglifyJsPlugin({ 60 | compressor: { 61 | warnings: false, 62 | }, 63 | }), 64 | new webpack.optimize.OccurrenceOrderPlugin() 65 | ] 66 | } 67 | ``` 68 | 69 | 우리는 설정파일을 써서 우리의 번들 자바스크립트 파일을 최소화하였습니다. 이 번들은 다른 프로젝트의 디렉터리로 복사될 수 있습니다. 그리고 그곳의 script태그로 던져질 수 있습니다. 만약 당신이 웹팩의 기초만을 *자바스크립트만* 사용하기를 원한다면 [결론](#conclusion)을 생략할 수 있습니다. -------------------------------------------------------------------------------- /ko-arahansa/part1/example3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example3", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "webpack": "^1.12.13" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example3/src/index.js: -------------------------------------------------------------------------------- 1 | console.log('It works!') 2 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example3/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | 4 | module.exports = { 5 | entry: ['./src/index'], // .js after index is optional 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | plugins: [ 11 | new webpack.optimize.UglifyJsPlugin({ 12 | compressor: { 13 | warnings: false, 14 | }, 15 | }), 16 | new webpack.optimize.OccurrenceOrderPlugin() 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/README.md: -------------------------------------------------------------------------------- 1 | 이 튜토리얼의 앞에서 [로더들](#loaders)을 얘기했듯이, 이것들은 우리에게 js파일이 아닌것을 우리의 코드에 require하게 해줍니다. 이러한 경우 우리는 style loader 와 css loader를 필요로 합니다. 먼저 이 로더들을 설치하도록 하겠습니다: 2 | 3 | npm install --save-dev style-loader css-loader 4 | 5 | 이제 이것들은 설치되었고 설정을 좀 더 바꿔서 css loader 를 추가해줘보도록 하겠습니다. : 6 | 7 | 8 | ```javascript 9 | // webpack.config.js 10 | var path = require('path') 11 | var webpack = require('webpack') 12 | 13 | module.exports = { 14 | entry: ['./src/index'], 15 | output: { 16 | path: path.join(__dirname, 'dist'), 17 | filename: 'bundle.js' 18 | }, 19 | plugins: [ 20 | new webpack.optimize.UglifyJsPlugin({ 21 | compressor: { 22 | warnings: false, 23 | }, 24 | }), 25 | new webpack.optimize.OccurrenceOrderPlugin() 26 | ], 27 | module: { 28 | loaders: [{ 29 | test: /\.css$/, 30 | loaders: ['style', 'css'] 31 | }] 32 | } 33 | } 34 | ``` 35 | 36 | 새로운 속성들을 하나하나 알아보도록 하겠습니다 : 37 | 38 | * [모듈](http://webpack.github.io/docs/configuration.html#module) - 당신의 파일들에 영향을 주는 옵션들 입니다. 39 | * [loaders](http://webpack.github.io/docs/configuration.html#module-loaders) - 어플리케이션을 위해 적어줄 로더들의 배열입니다. 40 | * test - 로더가 적용될 파일을 매칭하기 위한 정규식입니다. 41 | * loaders - test에서 매칭된 파일에 어떤 로더를 사용할지 결정합니다. 42 | 43 | 여기서 우리가 `webpack`을 실행할 때, 만약 당신이 `.css`로 끝나는 파일들을 `require` 했다면 우리는 이것들에 `style`과 `css` 로더를 적용하고 번들에 해당 css를 추가합니다. 44 | 45 | 만약 우리가 로더들을 가지고 있지 않았다면, 우리는 다음과 같은 에러를 얻을 것입니다 : 46 | 47 | ``` 48 | ERROR in ./test.css 49 | Module parse failed: /Users/Developer/workspace/tutorials/webpack/part1/example1/test.css 50 | Line 1: Unexpected token { 51 | You may need an appropriate loader to handle this file type. 52 | ``` 53 | 54 | **Optional(옵션)** 55 | 56 | 만약 당신이 CSS대신에 SCSS 를 사용하기를 원핟다면 다음과 같이 실행하세요 : 57 | 58 | npm install --save-dev sass-loader node-sass webpack 59 | 60 | 그리고 로더 부분을 다음과 같이 적으세요.. 61 | 62 | ```javascript 63 | { 64 | test: /\.scss$/, 65 | loaders: ["style", "css", "sass"] 66 | } 67 | ``` 68 | 69 | LESS도 비슷합니다. 70 | 71 | 로더를 인식하는 점에서 중요한 점은 이러한 로더들이 적혀지는 *순서*입니다. 위의 예제에서 `sass`로더가 제일 처음 당신의 `.scss` 파일들에 적용이 되고 그리고 `css`로더가 적용이 되며 마지막으로 `style`로더가 적용이 되게 됩니다. 당신이 볼 수 있듯이, 패턴이 적용되는 순서는 오른쪽에서 왼쪽입니다. 72 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example4", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "webpack": "^1.12.13" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/src/index.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AriaFallah/WebpackTutorial/ada78b7f505da40e3a6985f272047cebca9cf40d/ko-arahansa/part1/example4/src/index.html -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/src/index.js: -------------------------------------------------------------------------------- 1 | console.log('It works!') 2 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/src/styles.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AriaFallah/WebpackTutorial/ada78b7f505da40e3a6985f272047cebca9cf40d/ko-arahansa/part1/example4/src/styles.css -------------------------------------------------------------------------------- /ko-arahansa/part1/example4/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | 4 | module.exports = { 5 | entry: ['./src/index'], // .js after index is optional 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | plugins: [ 11 | new webpack.optimize.UglifyJsPlugin({ 12 | compressor: { 13 | warnings: false, 14 | }, 15 | }), 16 | new webpack.optimize.OccurrenceOrderPlugin() 17 | ], 18 | module: { 19 | loaders: [{ 20 | test: /\.css$/, 21 | loaders: ['style', 'css'] 22 | }] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ko-arahansa/part1/example5/README.md: -------------------------------------------------------------------------------- 1 | # Example 5 - Adding More Plugins 2 | 3 | 이제 우리는 우리의 우리가 스타일할 실제 웹페이지에 스타일을 적용할 수 있을 기반을 가졌습니다. 4 | 우리는 이것을 [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin)을 통해서 할 것인데 이것은 HTML페이지를 생성하거나 이미 존재하는 페이지를 사용합니다. 우리는 이미 존재하는 `index.html`을 사용하고자 합니다. 5 | 6 | 먼저 플러그인을 설치해보도록 하겠습니다 : 7 | 8 | npm install --save-dev html-webpack-plugin@2 9 | 10 | 11 | 그리고 우리는 우리의 설정파일에 추가해보도록 하겠습니다. 12 | 13 | ```javascript 14 | // webpack.config.js 15 | var path = require('path') 16 | var webpack = require('webpack') 17 | var HtmlWebpackPlugin = require('html-webpack-plugin') 18 | 19 | module.exports = { 20 | entry: ['./src/index'], 21 | output: { 22 | path: path.join(__dirname, 'dist'), 23 | filename: 'bundle.js' 24 | }, 25 | plugins: [ 26 | new webpack.optimize.UglifyJsPlugin({ 27 | compressor: { 28 | warnings: false, 29 | }, 30 | }), 31 | new webpack.optimize.OccurrenceOrderPlugin(), 32 | new HtmlWebpackPlugin({ 33 | template: './src/index.html' 34 | }) 35 | ], 36 | module: { 37 | loaders: [{ 38 | test: /\.css$/, 39 | loaders: ['style', 'css'] 40 | }] 41 | } 42 | } 43 | ``` 44 | 45 | 여기서 우리는 `webpack`을 실행하면, 우리가 `HtmlWebpackPlugin`을 `./src/index.html`에 적용해뒀기 때문에, 이것은 `dist`폴더에 `./src/index.html`를 기반으로 하여서 `index.html` 을 생성할 것입니다. 46 | 47 | (There's no point in using `index.html` as a template if it's empty.) 48 | 이제 실제로 실제로 index.html을 만들어봅시다. 49 | 50 | ```html 51 | 52 | 53 | Webpack Tutorial 54 | 55 | 56 |

Very Website

57 |
58 | 59 | 60 | 61 | ``` 62 | 63 | 여기서 우리는 `bundle.js` 를 위한 ` 65 | 66 | ``` 67 | -------------------------------------------------------------------------------- /part1/css-extract/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "css-extract", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "build": "webpack --config webpack.config.prod.js", 8 | "dev": "webpack-dev-server --config webpack.config.dev.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "MIT", 13 | "devDependencies": { 14 | "css-loader": "^0.23.1", 15 | "extract-text-webpack-plugin": "^1.0.1", 16 | "html-webpack-plugin": "^2.8.1", 17 | "style-loader": "^0.13.0", 18 | "webpack": "^1.12.13", 19 | "webpack-dev-server": "^1.14.1" 20 | }, 21 | "dependencies": { 22 | "pleasejs": "^0.4.2" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /part1/css-extract/src/changeColor.js: -------------------------------------------------------------------------------- 1 | var Please = require('pleasejs') 2 | var div = document.getElementById('color') 3 | var button = document.getElementById('button') 4 | 5 | function changeColor() { 6 | div.style.backgroundColor = Please.make_color() 7 | console.log('pls do work') 8 | } 9 | 10 | button.addEventListener('click', changeColor) 11 | -------------------------------------------------------------------------------- /part1/css-extract/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Webpack Tutorial 4 | 5 | 6 |

Very Website

7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /part1/css-extract/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./changeColor.js') 3 | -------------------------------------------------------------------------------- /part1/css-extract/src/styles.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: rgb(114, 191, 190); 3 | text-align: center; 4 | } 5 | 6 | #color { 7 | width: 300px; 8 | height: 300px; 9 | margin: 0 auto; 10 | } 11 | 12 | button { 13 | cursor: pointer; 14 | display: block; 15 | width: 100px; 16 | outline: 0; 17 | border: 0; 18 | margin: 20px auto; 19 | } 20 | -------------------------------------------------------------------------------- /part1/css-extract/webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | devtool: 'cheap-eval-source-map', 7 | entry: [ 8 | 'webpack-dev-server/client?http://localhost:8080', 9 | 'webpack/hot/dev-server', 10 | './src/index' 11 | ], 12 | output: { 13 | path: path.join(__dirname, 'dist'), 14 | filename: 'bundle.js' 15 | }, 16 | plugins: [ 17 | new webpack.HotModuleReplacementPlugin(), 18 | new HtmlWebpackPlugin({ 19 | template: './src/index.html' 20 | }) 21 | ], 22 | module: { 23 | loaders: [{ 24 | test: /\.css$/, 25 | loaders: ['style', 'css'] 26 | }] 27 | }, 28 | devServer: { 29 | contentBase: './dist', 30 | hot: true 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /part1/css-extract/webpack.config.prod.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | var ExtractTextPlugin = require("extract-text-webpack-plugin"); 5 | 6 | module.exports = { 7 | devtool: 'source-map', 8 | entry: ['./src/index'], 9 | output: { 10 | path: path.join(__dirname, 'dist'), 11 | filename: 'bundle.js' 12 | }, 13 | plugins: [ 14 | new webpack.optimize.UglifyJsPlugin({ 15 | compressor: { 16 | warnings: false, 17 | }, 18 | }), 19 | new webpack.optimize.OccurrenceOrderPlugin(), 20 | new HtmlWebpackPlugin({ 21 | template: './src/index.html' 22 | }), 23 | new ExtractTextPlugin("styles.css") 24 | ], 25 | module: { 26 | loaders: [{ 27 | test: /\.css$/, 28 | loader: ExtractTextPlugin.extract("style-loader", "css-loader") 29 | }] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /part1/example1/README.md: -------------------------------------------------------------------------------- 1 | # Example 1 - Bundling and Loaders 2 | 3 | ![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) 4 | 5 | Webpack is formally referred to as a module bundler. If you want an in-depth and accessible explanation 6 | on modules and module bundling definitely check out these two great articles: 7 | [here](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) 8 | and [here](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2). 9 | We're gonna keep it simple. The way that it works is that you specify a single file as your entry point. 10 | This file will be the root of your tree. Then every time you `require` a file from another file it's 11 | added to the tree. When you run `webpack`, all these files/modules are bundled into a single file. 12 | 13 | Here's a simple example: 14 | 15 | ![Dependency Tree](http://i.imgur.com/dSghwwL.png) 16 | 17 | Given this picture you could have the directory: 18 | 19 | ``` 20 | MyDirectory 21 | |- index.js 22 | |- UIStuff.js 23 | |- APIStuff.js 24 | |- styles.css 25 | |- extraFile.js 26 | ``` 27 | 28 | and this could be the content of your files 29 | 30 | ```javascript 31 | // index.js 32 | require('./styles.css') 33 | require('./UIStuff.js') 34 | require('./APIStuff.js') 35 | 36 | // UIStuff.js 37 | var React = require('React') 38 | React.createClass({ 39 | // stuff 40 | }) 41 | 42 | // APIStuff.js 43 | var fetch = require('fetch') // fetch polyfill 44 | fetch('https://google.com') 45 | ``` 46 | 47 | ```css 48 | /* styles.css */ 49 | body { 50 | background-color: rgb(200, 56, 97); 51 | } 52 | ``` 53 | 54 | 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 55 | the bundle because it is not a part of the dependency tree: 56 | 57 | `bundle.js` will look like: 58 | 59 | ```javascript 60 | // contents of styles.css 61 | // contents of UIStuff.js + React 62 | // contents of APIStuff.js + fetch 63 | ``` 64 | 65 | The things that are bundled are only the things that you explicitly required across your files. 66 | 67 | ### Loaders 68 | 69 | As you probably noticed, I did something strange in the above example. I `required` a css file in a javascript file. 70 | 71 | The really cool and interesting thing about webpack is that you can `require` more than just 72 | javascript files. 73 | 74 | There is this thing in webpack called a loader. Using these loaders, you can 75 | `require` anything from `.css` and `.png` to `.html` files. 76 | 77 | For example in the diagram above I had 78 | 79 | ```javascript 80 | // index.js 81 | require('./styles.css') 82 | ``` 83 | 84 | 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 85 | valid, but also will actually apply the CSS to my page. 86 | 87 | This is just a single example of the many loaders you can use with webpack. 88 | -------------------------------------------------------------------------------- /part1/example1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example1", 3 | "version": "1.0.0", 4 | "description": "![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png)", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "fetch": "^1.0.0", 14 | "react": "^0.14.7" 15 | }, 16 | "devDependencies": { 17 | "css-loader": "^0.23.1", 18 | "style-loader": "^0.13.0", 19 | "webpack": "^1.12.14" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /part1/example1/src/APIStuff.js: -------------------------------------------------------------------------------- 1 | var fetch = require('fetch') // fetch polyfill 2 | fetch('https://google.com') 3 | -------------------------------------------------------------------------------- /part1/example1/src/UIStuff.js: -------------------------------------------------------------------------------- 1 | var React = require('React') 2 | React.createClass({ 3 | // stuff 4 | }) 5 | -------------------------------------------------------------------------------- /part1/example1/src/extraFile.js: -------------------------------------------------------------------------------- 1 | console.log('No one likes me') 2 | -------------------------------------------------------------------------------- /part1/example1/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./UIStuff.js') 3 | require('./APIStuff.js') 4 | -------------------------------------------------------------------------------- /part1/example1/src/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: rgb(200, 56, 97); 3 | } 4 | -------------------------------------------------------------------------------- /part1/example1/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | 4 | module.exports = { 5 | entry: ['./src/index'], // .js after index is optional 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | plugins: [ 11 | new webpack.optimize.UglifyJsPlugin({ 12 | compressor: { 13 | warnings: false, 14 | }, 15 | }), 16 | new webpack.optimize.OccurrenceOrderPlugin() 17 | ], 18 | module: { 19 | loaders: [{ 20 | test: /\.css$/, 21 | loaders: ['style', 'css'] 22 | }] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /part1/example2/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 - A minimal example 2 | 3 | Say your directory structure looks like this: 4 | 5 | ``` 6 | MyDirectory 7 | |- dist 8 | |- src 9 | |- index.js 10 | |- webpack.config.js 11 | 12 | ``` 13 | 14 | Then a very minimal webpack config you can make is this 15 | 16 | ```javascript 17 | // webpack.config.js 18 | var path = require('path') 19 | 20 | module.exports = { 21 | entry: ['./src/index'], // .js after index is optional 22 | output: { 23 | path: path.join(__dirname, 'dist'), 24 | filename: 'bundle.js' 25 | } 26 | } 27 | ``` 28 | 29 | Going over the new properties one by one: 30 | 31 | * [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. 32 | 33 | * [output](https://webpack.github.io/docs/configuration.html#output) - Dictates the form of the output by webpack 34 | * [path](https://webpack.github.io/docs/configuration.html#output-path) - where to put the bundle 35 | * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - what to call the bundle 36 | 37 | When you run `webpack`, this will create a file called `bundle.js` in the dist folder. 38 | -------------------------------------------------------------------------------- /part1/example2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example2", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT" 12 | } 13 | -------------------------------------------------------------------------------- /part1/example2/src/index.js: -------------------------------------------------------------------------------- 1 | console.log('It works!') 2 | -------------------------------------------------------------------------------- /part1/example2/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | 3 | module.exports = { 4 | entry: ['./src/index'], // .js after index is optional 5 | output: { 6 | path: path.join(__dirname, 'dist'), 7 | filename: 'bundle.js' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /part1/example3/README.md: -------------------------------------------------------------------------------- 1 | # Example 3 - Introducing Plugins 2 | 3 | Imagine that you've used webpack to bundle all your files together, and now you've realized that all 4 | together it's 900KB. This is a problem that can be ameliorated by minifying your bundle. To do this 5 | you need to use a plugin I mentioned earlier called the 6 | [UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin). 7 | 8 | Moreover you will need to have webpack installed locally to actually be able to use the plugin. 9 | 10 | npm install --save-dev webpack 11 | 12 | Now you can require webpack and minify your code. 13 | 14 | ```javascript 15 | // webpack.config.js 16 | var path = require('path') 17 | var webpack = require('webpack') 18 | 19 | module.exports = { 20 | entry: ['./src/index'], 21 | output: { 22 | path: path.join(__dirname, 'dist'), 23 | filename: 'bundle.js' 24 | }, 25 | 26 | plugins: [ 27 | new webpack.optimize.UglifyJsPlugin({ 28 | compressor: { 29 | warnings: false, 30 | }, 31 | }) 32 | ] 33 | } 34 | ``` 35 | Going over the new properties one by one: 36 | 37 | * plugins - An array that holds your plugins. 38 | * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - Minify your code, and suppress warning messages. 39 | 40 | This time, when you run `webpack`, now that you have the `UglifyJsPlugin` this could reduce your 41 | imaginary 900KB file to 200KB by through processes such as removing all the whitespace. 42 | 43 | You can also add the [OrderOccurencePlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin) 44 | 45 | > 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. 46 | 47 | 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. 48 | 49 | ```javascript 50 | // webpack.config.js 51 | var path = require('path') 52 | var webpack = require('webpack') 53 | 54 | module.exports = { 55 | entry: ['./src/index'], 56 | output: { 57 | path: path.join(__dirname, 'dist'), 58 | filename: 'bundle.js' 59 | }, 60 | plugins: [ 61 | new webpack.optimize.UglifyJsPlugin({ 62 | compressor: { 63 | warnings: false, 64 | }, 65 | }), 66 | new webpack.optimize.OccurenceOrderPlugin() 67 | ] 68 | } 69 | ``` 70 | 71 | So now we have written a config that allows us to minify and bundle our javascript. This bundle 72 | could be copied and pasted into another project's directory, and thrown into a ` 60 | 61 | ``` 62 | -------------------------------------------------------------------------------- /zh-TW/part1/css-extract/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "css-extract", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "build": "webpack --config webpack.config.prod.js", 8 | "dev": "webpack-dev-server --config webpack.config.dev.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "MIT", 13 | "devDependencies": { 14 | "css-loader": "^0.23.1", 15 | "extract-text-webpack-plugin": "^1.0.1", 16 | "html-webpack-plugin": "^2.8.1", 17 | "style-loader": "^0.13.0", 18 | "webpack": "^1.12.13", 19 | "webpack-dev-server": "^1.14.1" 20 | }, 21 | "dependencies": { 22 | "pleasejs": "^0.4.2" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /zh-TW/part1/css-extract/src/changeColor.js: -------------------------------------------------------------------------------- 1 | var Please = require('pleasejs') 2 | var div = document.getElementById('color') 3 | var button = document.getElementById('button') 4 | 5 | function changeColor() { 6 | div.style.backgroundColor = Please.make_color() 7 | console.log('pls do work') 8 | } 9 | 10 | button.addEventListener('click', changeColor) 11 | -------------------------------------------------------------------------------- /zh-TW/part1/css-extract/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Webpack Tutorial 4 | 5 | 6 |

Very Website

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

Very Website

7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /zh-cn/part1/css-extract/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./changeColor.js') 3 | -------------------------------------------------------------------------------- /zh-cn/part1/css-extract/src/styles.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: rgb(114, 191, 190); 3 | text-align: center; 4 | } 5 | 6 | #color { 7 | width: 300px; 8 | height: 300px; 9 | margin: 0 auto; 10 | } 11 | 12 | button { 13 | cursor: pointer; 14 | display: block; 15 | width: 100px; 16 | outline: 0; 17 | border: 0; 18 | margin: 20px auto; 19 | } 20 | -------------------------------------------------------------------------------- /zh-cn/part1/css-extract/webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | devtool: 'cheap-eval-source-map', 7 | entry: [ 8 | 'webpack-dev-server/client?http://localhost:8080', 9 | 'webpack/hot/dev-server', 10 | './src/index' 11 | ], 12 | output: { 13 | path: path.join(__dirname, 'dist'), 14 | filename: 'bundle.js' 15 | }, 16 | plugins: [ 17 | new webpack.HotModuleReplacementPlugin(), 18 | new HtmlWebpackPlugin({ 19 | template: './src/index.html' 20 | }) 21 | ], 22 | module: { 23 | loaders: [{ 24 | test: /\.css$/, 25 | loaders: ['style', 'css'] 26 | }] 27 | }, 28 | devServer: { 29 | contentBase: './dist', 30 | hot: true 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /zh-cn/part1/css-extract/webpack.config.prod.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | var HtmlWebpackPlugin = require('html-webpack-plugin') 4 | var ExtractTextPlugin = require("extract-text-webpack-plugin"); 5 | 6 | module.exports = { 7 | devtool: 'source-map', 8 | entry: ['./src/index'], 9 | output: { 10 | path: path.join(__dirname, 'dist'), 11 | filename: 'bundle.js' 12 | }, 13 | plugins: [ 14 | new webpack.optimize.UglifyJsPlugin({ 15 | compressor: { 16 | warnings: false, 17 | }, 18 | }), 19 | new webpack.optimize.OccurrenceOrderPlugin(), 20 | new HtmlWebpackPlugin({ 21 | template: './src/index.html' 22 | }), 23 | new ExtractTextPlugin("styles.css") 24 | ], 25 | module: { 26 | loaders: [{ 27 | test: /\.css$/, 28 | loader: ExtractTextPlugin.extract("style-loader", "css-loader") 29 | }] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/README.md: -------------------------------------------------------------------------------- 1 | # 范例一 - Bundle 和 Loader 2 | 3 | ![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png) 4 | 5 | Webpack 简称为模块的整合工具。如果你想要深入的话,可以拜访「modules」和「module bundling definitely」这两篇优秀的解释文章: 6 | [JavaScript Modules: A Beginner’s Guide](https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.jw1txw6uh) 7 | 和 [JavaScript Modules Part 2: Module Bundling](https://medium.com/@preethikasireddy/javascript-modules-part-2-module-bundling-5020383cf306#.lfnspler2)。 8 | 我们要保持它的简单,webpack 运作的方式是通过指定一个单一文件作为你的进入点。 9 | 这个文件会是 tree 的 root。然后你每次 `require` 一个文件从其他文件并把它加入到 tree。当你执行 `webpack`,所有的文件和 module 都会被 bundle 成一个文件。 10 | 11 | 这里是一个简单的范例: 12 | 13 | ![Dependency Tree](http://i.imgur.com/dSghwwL.png) 14 | 15 | 根据这样的情况,你可以有这样的目录: 16 | 17 | ``` 18 | MyDirectory 19 | |- index.js 20 | |- UIStuff.js 21 | |- APIStuff.js 22 | |- styles.css 23 | |- extraFile.js 24 | ``` 25 | 26 | 这些可能是你文件的内容: 27 | 28 | ```javascript 29 | // index.js 30 | require('./styles.css') 31 | require('./UIStuff.js') 32 | require('./APIStuff.js') 33 | 34 | // UIStuff.js 35 | var React = require('React') 36 | React.createClass({ 37 | // stuff 38 | }) 39 | 40 | // APIStuff.js 41 | var fetch = require('fetch') // fetch polyfill 42 | fetch('https://google.com') 43 | ``` 44 | 45 | ```css 46 | /* styles.css */ 47 | body { 48 | background-color: rgb(200, 56, 97); 49 | } 50 | ``` 51 | 52 | 当你执行 `webpack`,你会得到一个这个 tree 的 bundle 内容,虽然 `extraFile.js` 也是在相同的目录中,但它不是被 bundle 的一部份,因为它在 `index.js` 没有被 `require`。 53 | 54 | `bundle.js` 看起来会像: 55 | 56 | ```javascript 57 | // contents of styles.css 58 | // contents of UIStuff.js + React 59 | // contents of APIStuff.js + fetch 60 | ``` 61 | 62 | 被 bundle 的这些文件是你明确所 require 进来的文件。 63 | 64 | ### Loaders 65 | 66 | 你可能会注意到,我在上方的范例做了一些奇怪的事情。我在 JavaScript 文件中 `require` 一个 css 文件。 67 | 68 | 关于 webpack 真的很酷,有趣的事情是,你可以 `require` 其他不只是 JavaScript 的文件。 69 | 70 | 在 webpack 这些东西我们称为 loader。使用这些 loader,你可以 `require` 任何 `.css` 和 `.png` 到 `.html` 档。 71 | 72 | 例如在上图我有: 73 | 74 | ```javascript 75 | // index.js 76 | require('./styles.css') 77 | ``` 78 | 79 | 如果在我的 webpack 配置中,inclue [style-loader](https://github.com/webpack/style-loader) 和 [css-loader](https://github.com/webpack/css-loader),这是可行的,还可以实际应用 CSS 到我的网页。 80 | 81 | 你可以在 webpack 使用多个 loader,这里只是一个单一的例子。 82 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example1", 3 | "version": "1.0.0", 4 | "description": "![Official Dependency Tree](http://i.imgur.com/YU4xBPQ.png)", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "fetch": "^1.0.0", 14 | "react": "^0.14.7" 15 | }, 16 | "devDependencies": { 17 | "css-loader": "^0.23.1", 18 | "style-loader": "^0.13.0", 19 | "webpack": "^1.12.14" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/src/APIStuff.js: -------------------------------------------------------------------------------- 1 | var fetch = require('fetch') // fetch polyfill 2 | fetch('https://google.com') 3 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/src/UIStuff.js: -------------------------------------------------------------------------------- 1 | var React = require('React') 2 | React.createClass({ 3 | // stuff 4 | }) 5 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/src/extraFile.js: -------------------------------------------------------------------------------- 1 | console.log('No one likes me') 2 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/src/index.js: -------------------------------------------------------------------------------- 1 | require('./styles.css') 2 | require('./UIStuff.js') 3 | require('./APIStuff.js') 4 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/src/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: rgb(200, 56, 97); 3 | } 4 | -------------------------------------------------------------------------------- /zh-cn/part1/example1/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var webpack = require('webpack') 3 | 4 | module.exports = { 5 | entry: ['./src/index'], // .js after index is optional 6 | output: { 7 | path: path.join(__dirname, 'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | plugins: [ 11 | new webpack.optimize.UglifyJsPlugin({ 12 | compressor: { 13 | warnings: false, 14 | }, 15 | }), 16 | new webpack.optimize.OccurrenceOrderPlugin() 17 | ], 18 | module: { 19 | loaders: [{ 20 | test: /\.css$/, 21 | loaders: ['style', 'css'] 22 | }] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /zh-cn/part1/example2/README.md: -------------------------------------------------------------------------------- 1 | # 范例二 - 一个简单的范例 2 | 3 | 你的目录结构像是这样: 4 | 5 | ``` 6 | MyDirectory 7 | |- dist 8 | |- src 9 | |- index.js 10 | |- webpack.config.js 11 | 12 | ``` 13 | 14 | 然后这是一个非常简易的 webpack 配置: 15 | 16 | ```javascript 17 | // webpack.config.js 18 | var path = require('path') 19 | 20 | module.exports = { 21 | entry: ['./src/index'], // .js after index is optional 22 | output: { 23 | path: path.join(__dirname, 'dist'), 24 | filename: 'bundle.js' 25 | } 26 | } 27 | ``` 28 | 29 | 我们一个一个复习这些属性: 30 | 31 | * [entry](https://webpack.github.io/docs/configuration.html#entry) - 这是你的 bundle 的进入点,这是我们在讨论 [bundling](#bundling) 的部分。`entry` 是一个阵列,根据你的需求,webpack 允许可以有多个进入点,来产生多个 bundle 文件。 32 | 33 | * [output](https://webpack.github.io/docs/configuration.html#output) - 由 webpack 规定的形式输出。 34 | * [path](https://webpack.github.io/docs/configuration.html#output-path) - bundle 文件位置。 35 | * [filename](https://webpack.github.io/docs/configuration.html#output-filename) - bundle 文件名称。 36 | 37 | 当你执行 `webpack`,会在你的 dist 资料夹建立一个叫做 `bundle.js` 的文件。 38 | -------------------------------------------------------------------------------- /zh-cn/part1/example2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example2", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT" 12 | } 13 | -------------------------------------------------------------------------------- /zh-cn/part1/example2/src/index.js: -------------------------------------------------------------------------------- 1 | console.log('It works!') 2 | -------------------------------------------------------------------------------- /zh-cn/part1/example2/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | 3 | module.exports = { 4 | entry: ['./src/index'], // .js after index is optional 5 | output: { 6 | path: path.join(__dirname, 'dist'), 7 | filename: 'bundle.js' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /zh-cn/part1/example3/README.md: -------------------------------------------------------------------------------- 1 | # 范例三 - 介绍 Plugins 2 | 3 | 想像一下,你使用 webpack 将你的文件 bundle 在一起,然后你发现到 bundle 后的结果是 900KB。这里有个问题,但是你可以通过 minify 你的 bundle 文件来做改善。如果需要做到这一点,你需要使用一个我在前面稍早提到的 [UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) plugin。 4 | 5 | 此外,你需要在本机安装 webpack 才能实际的去使用这个 plugin。 6 | 7 | npm install --save-dev webpack 8 | 9 | 现在你可以 require webpack 并 minify 你的代码。 10 | 11 | ```javascript 12 | // webpack.config.js 13 | var path = require('path') 14 | var webpack = require('webpack') 15 | 16 | module.exports = { 17 | entry: ['./src/index'], 18 | output: { 19 | path: path.join(__dirname, 'dist'), 20 | filename: 'bundle.js' 21 | }, 22 | 23 | plugins: [ 24 | new webpack.optimize.UglifyJsPlugin({ 25 | compressor: { 26 | warnings: false, 27 | }, 28 | }) 29 | ] 30 | } 31 | ``` 32 | 我们一个一个复习这些属性: 33 | 34 | * plugins - 一个可以储存你的 plugin 的阵列。 35 | * [webpack.optimize.UglifyJsPlugin](https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) - Minify 你的代码,并显示警告讯息。 36 | 37 | 这个时候,当我们执行 `webpack`,`UglifyJsPlugin` 通过像是移除所有空白的处理,可以将你的文件减少至 200KB。 38 | 39 | 你也可以加入 [OccurenceOrderPlugin](https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin)。 40 | 41 | > 通过发生次数分配 module 和 chunk 的 id。一些常用的 Id 取得较低(短)的 id。这使得 id 可以预测,减少文件的大小和建议。 42 | 43 | 老实说,我不太确定底层的机制是如何工作的,但在目前包含 [webpack2 beta 的预设情况下](https://gist.github.com/sokra/27b24881210b56bbaff7),所以我将它包含在内。 44 | 45 | ```javascript 46 | // webpack.config.js 47 | var path = require('path') 48 | var webpack = require('webpack') 49 | 50 | module.exports = { 51 | entry: ['./src/index'], 52 | output: { 53 | path: path.join(__dirname, 'dist'), 54 | filename: 'bundle.js' 55 | }, 56 | plugins: [ 57 | new webpack.optimize.UglifyJsPlugin({ 58 | compressor: { 59 | warnings: false, 60 | }, 61 | }), 62 | new webpack.optimize.OccurenceOrderPlugin() 63 | ] 64 | } 65 | ``` 66 | 67 | 所以现在我们写了一个配置让我们可以 minify 和 bundle 我们的 JavaScript。这个 bundle 文件可以被复制并贴到其他的专桉目录中,放入 `