step 17 webpack 추가
@ae3d12677550bd7cff1622cb2f9fc566d87b14ce
--- README.md
+++ README.md
... | ... | @@ -76,9 +76,10 @@ |
| 76 | 76 |
- [Step 11. 증분 빌드 적용하기](./step11_incremental_build) |
| 77 | 77 |
- [Step 12. plumber로 오류처리하기](./step12_plumber) |
| 78 | 78 |
- [Step 13. runSequence를 사용해서 직렬/병렬로 실행하기](./step13_run_sequence) |
| 79 |
-- [Step 14. React + Browserify + Babel 빌드하기](./step14_react) |
|
| 79 |
+- [Step 14. Browserify 로 React 번들링하기](./step14_react) |
|
| 80 | 80 |
- [Step 15. 다중 엔트리 React + Browserify + Watchify + Babel 빌드하기](./step15_watchify) |
| 81 | 81 |
- [Step 16. 수행할 작업을 메뉴에서 선택해서 실행하기](./step16_select_task) |
| 82 |
+- [Step 17. Webpack 으로 React 번들링하기 ](./step17_webpack) |
|
| 82 | 83 |
|
| 83 | 84 |
#### Gulp 를 활용하는데 도움이 되는 링크들 |
| 84 | 85 |
- [Gulp Recipes](https://github.com/gulpjs/gulp/tree/master/docs/recipes) |
+++ step17_webpack/gulpfile.js
... | ... | @@ -0,0 +1,183 @@ |
| 1 | +var gulp = require('gulp');
| |
| 2 | +var browserSync = require('browser-sync').create();
| |
| 3 | +var webpack = require('webpack');
| |
| 4 | + | |
| 5 | +//dist 폴더를 기준으로 웹서버 실행 | |
| 6 | +gulp.task('server', ['build:javascript', 'build:html'], function () {
| |
| 7 | + return browserSync.init({
| |
| 8 | + server: {
| |
| 9 | + baseDir: './dist' | |
| 10 | + } | |
| 11 | + }); | |
| 12 | +}); | |
| 13 | + | |
| 14 | +//HTML 파일을 dist 로 복사 | |
| 15 | +gulp.task('build:html', function () {
| |
| 16 | + return gulp.src('./src/**/*.html')
| |
| 17 | + .pipe(gulp.dest('dist'))
| |
| 18 | + .pipe(browserSync.reload({stream: true}));
| |
| 19 | +}); | |
| 20 | + | |
| 21 | +//자바스크립트 파일을 webpack으로 번들링 | |
| 22 | +gulp.task('build:javascript', function (done) {
| |
| 23 | + //웹팩 실행 | |
| 24 | + webpack({ | |
| 25 | + //기본 디렉토리 | |
| 26 | + //context: process.cwd(), | |
| 27 | + | |
| 28 | + //소스맵 생성 수준 설정 | |
| 29 | + //https://webpack.github.io/docs/configuration.html#debug | |
| 30 | + //devtool: '#source-map', | |
| 31 | + | |
| 32 | + //엔트리 파일 리스트 | |
| 33 | + //배열이나 키밸류 형태로 지정. 키밸류의 경우 키는 해당 엔트리의 이름, 밸류는 경로를 지정. | |
| 34 | + entry: { | |
| 35 | + main: './src/js/main.js' | |
| 36 | + }, | |
| 37 | + | |
| 38 | + //빌드 결과물에 대한 설정 | |
| 39 | + output: {
| |
| 40 | + //빌드 결과물 경로 | |
| 41 | + path: 'dist/js', | |
| 42 | + //모듈에 대한 정보를 담은 주석을 포함시킬 지 여부. production 에서는 반드시 false 로 설정. | |
| 43 | + pathinfo: false, | |
| 44 | + //빌드 결과물 파일명 | |
| 45 | + //[id] 는 해당 청크의 id 로 치환 | |
| 46 | + //[hash] 는 결과물의 해시로 치환 | |
| 47 | + //[name] 은 entry 의 key 로 치환 | |
| 48 | + filename: '[name].js', | |
| 49 | + //웹에서 해당 asset에 접근하기 위해 필요한 경로로 로컬과 다른 경우 사용 | |
| 50 | + publicPath: '/' | |
| 51 | + }, | |
| 52 | + | |
| 53 | + //require 등으로 모듈을 로딩할 때 받은 경로를 어떻게 해석할지에 대해 설정 | |
| 54 | + resolve: {
| |
| 55 | + //모듈을 찾기 시작하는 루트 경로. 기본값은 node_modules | |
| 56 | + //root: nodeModulesPath, | |
| 57 | + //배열로 ['', '.js', '.jsx'] 와 같이 extensions 를 설정하면 js 확장자로 찾아보고 없으면 jsx 확장자로 로딩을 시도 | |
| 58 | + extensions: ['', '.js', '.jsx'], | |
| 59 | + }, | |
| 60 | + | |
| 61 | + //웹팩에서 사용하는 로더들을 어떻게 가져올지 설정 | |
| 62 | + resolveLoader: {
| |
| 63 | + //로더를 가져올 때 이름만 명시하면 뒤에 loader 를 붙여서 require 를 시도 | |
| 64 | + //기본값은 ["*-webpack-loader", "*-web-loader", "*-loader", "*"] | |
| 65 | + moduleTemplates: ["*-webpack-loader", "*-web-loader", "*-loader", "*"] | |
| 66 | + }, | |
| 67 | + | |
| 68 | + //번들링에서 제외하는 모듈을 명시 | |
| 69 | + // externals: {
| |
| 70 | + // 'jquery': '$', | |
| 71 | + // 'underscore': '_', | |
| 72 | + // 'react': 'React' | |
| 73 | + // }, | |
| 74 | + | |
| 75 | + //ESLint 로더 설정 | |
| 76 | + // eslint: {
| |
| 77 | + // configFile: path.join(__dirname, 'eslint.js'), | |
| 78 | + // useEslintrc: false | |
| 79 | + // }, | |
| 80 | + | |
| 81 | + //각 모듈들을 로딩할 때 어떻게 다룰지를 설정 | |
| 82 | + module: {
| |
| 83 | + //다른 로더들에 앞서 수행하는 로더 | |
| 84 | + // preLoaders: [ | |
| 85 | + // {
| |
| 86 | + // //정규식 패턴에 매칭되는 모듈인 경우, 여기에서는 .js 파일인 경우. | |
| 87 | + // test: /\.js$/, | |
| 88 | + // //eslint 로더를 수행 | |
| 89 | + // loader: 'eslint', | |
| 90 | + // //만족해야하는 조건. 여기에서는 경로를 지정 | |
| 91 | + // include: srcPath, | |
| 92 | + // } | |
| 93 | + // ], | |
| 94 | + | |
| 95 | + //적용할 로더. 배열의 마지막 로더부터 적용. | |
| 96 | + loaders: [ | |
| 97 | + {
| |
| 98 | + test: /\.(js|jsx)$/, | |
| 99 | + exclude: /node_modules/, | |
| 100 | + loader: 'babel', | |
| 101 | + query: {
| |
| 102 | + //production 인 경우 캐시 설정은 false 로 함 | |
| 103 | + cacheDirectory: true, | |
| 104 | + presets: [ | |
| 105 | + 'babel-preset-react', | |
| 106 | + 'babel-preset-es2015' | |
| 107 | + ].map(require.resolve), | |
| 108 | + plugins: [ | |
| 109 | + 'babel-plugin-syntax-trailing-function-commas', | |
| 110 | + 'babel-plugin-transform-class-properties', | |
| 111 | + 'babel-plugin-transform-object-rest-spread', | |
| 112 | + 'babel-plugin-transform-es3-member-expression-literals', | |
| 113 | + 'babel-plugin-transform-es3-property-literals' | |
| 114 | + ].map(require.resolve) | |
| 115 | + } | |
| 116 | + } | |
| 117 | + // {
| |
| 118 | + // test: /\.css$/, | |
| 119 | + // include: srcPath, | |
| 120 | + // loader: 'style!css!postcss' | |
| 121 | + // }, | |
| 122 | + // {
| |
| 123 | + // test: /\.json$/, | |
| 124 | + // loader: 'json' | |
| 125 | + // }, | |
| 126 | + // {
| |
| 127 | + // test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/, | |
| 128 | + // loader: 'file', | |
| 129 | + // }, | |
| 130 | + // {
| |
| 131 | + // test: /\.(mp4|webm)$/, | |
| 132 | + // loader: 'url?limit=10000' | |
| 133 | + // } | |
| 134 | + ] | |
| 135 | + }, | |
| 136 | + | |
| 137 | + //컴파일러에 대한 추가적인 플러그인 설정 | |
| 138 | + plugins: [ | |
| 139 | + new webpack.DefinePlugin({
| |
| 140 | + 'process.env': {
| |
| 141 | + // production으로 설정하면 리액트 라이브러리 사이즈가 작아짐 | |
| 142 | + 'NODE_ENV': JSON.stringify('development')
| |
| 143 | + } | |
| 144 | + }), | |
| 145 | + new webpack.optimize.OccurrenceOrderPlugin(), | |
| 146 | + new webpack.optimize.DedupePlugin(), | |
| 147 | + new webpack.optimize.UglifyJsPlugin({
| |
| 148 | + compressor: {
| |
| 149 | + screw_ie8: true, | |
| 150 | + warnings: false | |
| 151 | + }, | |
| 152 | + mangle: {
| |
| 153 | + screw_ie8: true | |
| 154 | + }, | |
| 155 | + output: {
| |
| 156 | + comments: false, | |
| 157 | + screw_ie8: true | |
| 158 | + } | |
| 159 | + }) | |
| 160 | + ], | |
| 161 | + | |
| 162 | + //파일 변경을 감지할지 여부 | |
| 163 | + watch: true | |
| 164 | + }, function (err, stats) {
| |
| 165 | + if (err) {
| |
| 166 | + console.error('[웹팩 오류]', err);
| |
| 167 | + }; | |
| 168 | + | |
| 169 | + console.log('[웹팩 빌드 완료]', stats.toString()); | |
| 170 | + browserSync.reload(); | |
| 171 | + | |
| 172 | + typeof done === 'function' && done(); | |
| 173 | + done = null; | |
| 174 | + }); | |
| 175 | +}); | |
| 176 | + | |
| 177 | +//파일 변경 감지 | |
| 178 | +gulp.task('watch', function () {
| |
| 179 | + gulp.watch('src/**/*.html', ['build:html']);
| |
| 180 | +}); | |
| 181 | + | |
| 182 | +//gulp를 실행하면 default 로 minifycss task를 실행 | |
| 183 | +gulp.task('default', ['server', 'watch']);
|
+++ step17_webpack/package.json
... | ... | @@ -0,0 +1,37 @@ |
| 1 | +{ | |
| 2 | + "name": "gulp-step-by-step", | |
| 3 | + "version": "1.0.0", | |
| 4 | + "description": "gulp.js 를 시작하기 위한 간단한 예제들과 설명", | |
| 5 | + "main": "index.js", | |
| 6 | + "scripts": { | |
| 7 | + "test": "echo \"Error: no test specified\" && exit 1" | |
| 8 | + }, | |
| 9 | + "repository": { | |
| 10 | + "type": "git", | |
| 11 | + "url": "git+https://github.com/eu81273/gulp-step-by-step.git" | |
| 12 | + }, | |
| 13 | + "author": "AHN JAE-HA", | |
| 14 | + "license": "MIT", | |
| 15 | + "bugs": { | |
| 16 | + "url": "https://github.com/eu81273/gulp-step-by-step/issues" | |
| 17 | + }, | |
| 18 | + "devDependencies": { | |
| 19 | + "babel-core": "6.10.4", | |
| 20 | + "babel-loader": "6.2.4", | |
| 21 | + "babel-plugin-syntax-trailing-function-commas": "^6.8.0", | |
| 22 | + "babel-plugin-transform-class-properties": "^6.11.5", | |
| 23 | + "babel-plugin-transform-es3-member-expression-literals": "^6.8.0", | |
| 24 | + "babel-plugin-transform-es3-property-literals": "^6.8.0", | |
| 25 | + "babel-plugin-transform-object-rest-spread": "^6.8.0", | |
| 26 | + "babel-preset-es2015": "^6.9.0", | |
| 27 | + "babel-preset-es2016": "^6.11.3", | |
| 28 | + "babel-preset-react": "^6.3.13", | |
| 29 | + "browser-sync": "^2.11.1", | |
| 30 | + "gulp": "^3.9.0", | |
| 31 | + "webpack": "^1.13.1" | |
| 32 | + }, | |
| 33 | + "dependencies": { | |
| 34 | + "react": "^0.14.6", | |
| 35 | + "react-dom": "^0.14.6" | |
| 36 | + } | |
| 37 | +} |
+++ step17_webpack/src/index.html
... | ... | @@ -0,0 +1,12 @@ |
| 1 | +<!DOCTYPE html> | |
| 2 | +<html lang="ko"> | |
| 3 | +<head> | |
| 4 | + <meta charset="UTF-8"> | |
| 5 | + <title>gulp example</title> | |
| 6 | +</head> | |
| 7 | +<body> | |
| 8 | + <div id="application"></div> | |
| 9 | + | |
| 10 | + <script src="js/main.js"></script> | |
| 11 | +</body> | |
| 12 | +</html> |
+++ step17_webpack/src/js/main.js
... | ... | @@ -0,0 +1,10 @@ |
| 1 | +import React from 'react'; | |
| 2 | +import ReactDOM from 'react-dom'; | |
| 3 | + | |
| 4 | +let rootElement = document.getElementById('application'); | |
| 5 | +let message = 'Hello World!'; | |
| 6 | + | |
| 7 | +ReactDOM.render( | |
| 8 | + <div>{message}</div>, | |
| 9 | + rootElement | |
| 10 | +); |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?