AHN JAEHA 2016-06-27
step 15 watchify + multiple browserify 추가
@d5b01c18cb220cee969a6f2d7f6a2d0a3c55f5dc
 
step15_watchify/gulpfile.js (added)
+++ step15_watchify/gulpfile.js
@@ -0,0 +1,81 @@
+var gulp = require('gulp');
+var uglify = require('gulp-uglify');
+var browserSync = require('browser-sync').create();
+var browserify = require('browserify');
+var babelify = require('babelify');
+var watchify = require('watchify');
+var source = require('vinyl-source-stream');
+var buffer = require('vinyl-buffer');
+var sourcemaps  = require('gulp-sourcemaps');
+var mergeStream = require('merge-stream');
+var gutil = require('gulp-util');
+
+
+//dist 폴더를 기준으로 웹서버 실행
+gulp.task('server', ['build:javascript', 'build:html'], function () {
+	return browserSync.init({
+		server: {
+			baseDir: './dist'
+		}
+	});
+});
+
+//HTML 파일을 dist 로 복사
+gulp.task('build:html', function () {
+	return gulp.src('./src/**/*.html')
+		.pipe(gulp.dest('dist'))
+		.pipe(browserSync.reload({stream: true}));
+});
+
+//자바스크립트 파일을 browserify로 번들링
+gulp.task('build:javascript', function () {
+
+	var entries = [{
+		src: 'src/js/first.js',
+		filename: 'first.js'
+	}, {
+		src: 'src/js/second.js',
+		filename: 'second.js'
+	}, {
+		src: 'src/js/third.js',
+		filename: 'third.js'
+	}];
+
+	return mergeStream.apply(null, entries.map(function (entry) {
+		var target = watchify(browserify({entries: entry.src, debug: true, cache: {}, packageCache: {}}))
+			.transform(babelify, {presets: ['es2015', 'react']});
+
+		var bundle = function () {
+			console.log(gutil.colors.green(entry.filename), 'bundling..');
+			console.time(gutil.colors.green(entry.filename));
+
+			return target.bundle()
+			.on('error', function (err) {
+				//browserify bundling 과정에서 오류가 날 경우 gulp가 죽지않도록 예외처리
+				console.error(err);
+				this.emit('end');
+			})
+			.pipe(source(entry.filename)) //vinyl object 로 변환
+			.pipe(buffer()) //buffered vinyl object 로 변환
+			.pipe(sourcemaps.init({loadMaps: true, debug: true})) //소스맵 생성 준비
+			.pipe(uglify()) //minify 해서
+			.pipe(sourcemaps.write('./')) //생성된 소스맵을 스트림에 추가
+			.pipe(gulp.dest('dist/js')) //dist 폴더에 저장
+			.on('end', function () {
+				console.timeEnd(gutil.colors.green(entry.filename));
+			})
+			.pipe(browserSync.reload({stream: true})); //browserSync 로 브라우저에 반영
+		};
+
+		target.on('update', bundle);
+		return bundle();
+	}));
+});
+
+//파일 변경 감지
+gulp.task('watch', function () {
+	gulp.watch('src/**/*.html', ['build:html']);
+});
+
+//gulp를 실행하면 default 로 minifycss task를 실행
+gulp.task('default', ['server', 'watch']);
 
step15_watchify/package.json (added)
+++ step15_watchify/package.json
@@ -0,0 +1,34 @@
+{
+  "name": "gulp-step-by-step",
+  "version": "1.0.0",
+  "description": "gulp.js 를 시작하기 위한 간단한 예제들과 설명",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/eu81273/gulp-step-by-step.git"
+  },
+  "author": "AHN JAE-HA",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/eu81273/gulp-step-by-step/issues"
+  },
+  "devDependencies": {
+    "babel-preset-es2015": "^6.3.13",
+    "babel-preset-react": "^6.3.13",
+    "babelify": "^7.2.0",
+    "browser-sync": "^2.11.1",
+    "browserify": "^13.0.0",
+    "gulp": "^3.9.0",
+    "gulp-sourcemaps": "^1.6.0",
+    "gulp-uglify": "^1.4.0",
+    "merge-stream": "^1.0.0",
+    "react": "^0.14.6",
+    "react-dom": "^0.14.6",
+    "vinyl-buffer": "^1.0.0",
+    "vinyl-source-stream": "^1.1.0",
+    "watchify": "^3.7.0"
+  }
+}
 
step15_watchify/src/index.html (added)
+++ step15_watchify/src/index.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="ko">
+<head>
+    <meta charset="UTF-8">
+    <title>gulp example</title>
+</head>
+<body>
+    <div id="first"></div>
+    <div id="second"></div>
+    <div id="third"></div>
+
+    <script src="js/first.js"></script>
+    <script src="js/second.js"></script>
+    <script src="js/third.js"></script>
+</body>
+</html>
 
step15_watchify/src/js/first.js (added)
+++ step15_watchify/src/js/first.js
@@ -0,0 +1,10 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+let rootElement = document.getElementById('first');
+let message = 'Hello, this is first!';
+
+ReactDOM.render(
+	<div>{message}</div>,
+	rootElement
+);
 
step15_watchify/src/js/second.js (added)
+++ step15_watchify/src/js/second.js
@@ -0,0 +1,10 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+let rootElement = document.getElementById('second');
+let message = 'Hello, this is second!';
+
+ReactDOM.render(
+	<div>{message}</div>,
+	rootElement
+);
 
step15_watchify/src/js/third.js (added)
+++ step15_watchify/src/js/third.js
@@ -0,0 +1,10 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+let rootElement = document.getElementById('third');
+let message = 'Hello, this is third!';
+
+ReactDOM.render(
+	<div>{message}</div>,
+	rootElement
+);
Add a comment
List