step 15 watchify + multiple browserify 추가
@d5b01c18cb220cee969a6f2d7f6a2d0a3c55f5dc
+++ step15_watchify/gulpfile.js
... | ... | @@ -0,0 +1,81 @@ |
| 1 | +var gulp = require('gulp');
| |
| 2 | +var uglify = require('gulp-uglify');
| |
| 3 | +var browserSync = require('browser-sync').create();
| |
| 4 | +var browserify = require('browserify');
| |
| 5 | +var babelify = require('babelify');
| |
| 6 | +var watchify = require('watchify');
| |
| 7 | +var source = require('vinyl-source-stream');
| |
| 8 | +var buffer = require('vinyl-buffer');
| |
| 9 | +var sourcemaps = require('gulp-sourcemaps');
| |
| 10 | +var mergeStream = require('merge-stream');
| |
| 11 | +var gutil = require('gulp-util');
| |
| 12 | + | |
| 13 | + | |
| 14 | +//dist 폴더를 기준으로 웹서버 실행 | |
| 15 | +gulp.task('server', ['build:javascript', 'build:html'], function () {
| |
| 16 | + return browserSync.init({
| |
| 17 | + server: {
| |
| 18 | + baseDir: './dist' | |
| 19 | + } | |
| 20 | + }); | |
| 21 | +}); | |
| 22 | + | |
| 23 | +//HTML 파일을 dist 로 복사 | |
| 24 | +gulp.task('build:html', function () {
| |
| 25 | + return gulp.src('./src/**/*.html')
| |
| 26 | + .pipe(gulp.dest('dist'))
| |
| 27 | + .pipe(browserSync.reload({stream: true}));
| |
| 28 | +}); | |
| 29 | + | |
| 30 | +//자바스크립트 파일을 browserify로 번들링 | |
| 31 | +gulp.task('build:javascript', function () {
| |
| 32 | + | |
| 33 | + var entries = [{
| |
| 34 | + src: 'src/js/first.js', | |
| 35 | + filename: 'first.js' | |
| 36 | + }, {
| |
| 37 | + src: 'src/js/second.js', | |
| 38 | + filename: 'second.js' | |
| 39 | + }, {
| |
| 40 | + src: 'src/js/third.js', | |
| 41 | + filename: 'third.js' | |
| 42 | + }]; | |
| 43 | + | |
| 44 | + return mergeStream.apply(null, entries.map(function (entry) {
| |
| 45 | + var target = watchify(browserify({entries: entry.src, debug: true, cache: {}, packageCache: {}}))
| |
| 46 | + .transform(babelify, {presets: ['es2015', 'react']});
| |
| 47 | + | |
| 48 | + var bundle = function () {
| |
| 49 | + console.log(gutil.colors.green(entry.filename), 'bundling..'); | |
| 50 | + console.time(gutil.colors.green(entry.filename)); | |
| 51 | + | |
| 52 | + return target.bundle() | |
| 53 | + .on('error', function (err) {
| |
| 54 | + //browserify bundling 과정에서 오류가 날 경우 gulp가 죽지않도록 예외처리 | |
| 55 | + console.error(err); | |
| 56 | + this.emit('end');
| |
| 57 | + }) | |
| 58 | + .pipe(source(entry.filename)) //vinyl object 로 변환 | |
| 59 | + .pipe(buffer()) //buffered vinyl object 로 변환 | |
| 60 | + .pipe(sourcemaps.init({loadMaps: true, debug: true})) //소스맵 생성 준비
| |
| 61 | + .pipe(uglify()) //minify 해서 | |
| 62 | + .pipe(sourcemaps.write('./')) //생성된 소스맵을 스트림에 추가
| |
| 63 | + .pipe(gulp.dest('dist/js')) //dist 폴더에 저장
| |
| 64 | + .on('end', function () {
| |
| 65 | + console.timeEnd(gutil.colors.green(entry.filename)); | |
| 66 | + }) | |
| 67 | + .pipe(browserSync.reload({stream: true})); //browserSync 로 브라우저에 반영
| |
| 68 | + }; | |
| 69 | + | |
| 70 | + target.on('update', bundle);
| |
| 71 | + return bundle(); | |
| 72 | + })); | |
| 73 | +}); | |
| 74 | + | |
| 75 | +//파일 변경 감지 | |
| 76 | +gulp.task('watch', function () {
| |
| 77 | + gulp.watch('src/**/*.html', ['build:html']);
| |
| 78 | +}); | |
| 79 | + | |
| 80 | +//gulp를 실행하면 default 로 minifycss task를 실행 | |
| 81 | +gulp.task('default', ['server', 'watch']);
|
+++ step15_watchify/package.json
... | ... | @@ -0,0 +1,34 @@ |
| 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-preset-es2015": "^6.3.13", | |
| 20 | + "babel-preset-react": "^6.3.13", | |
| 21 | + "babelify": "^7.2.0", | |
| 22 | + "browser-sync": "^2.11.1", | |
| 23 | + "browserify": "^13.0.0", | |
| 24 | + "gulp": "^3.9.0", | |
| 25 | + "gulp-sourcemaps": "^1.6.0", | |
| 26 | + "gulp-uglify": "^1.4.0", | |
| 27 | + "merge-stream": "^1.0.0", | |
| 28 | + "react": "^0.14.6", | |
| 29 | + "react-dom": "^0.14.6", | |
| 30 | + "vinyl-buffer": "^1.0.0", | |
| 31 | + "vinyl-source-stream": "^1.1.0", | |
| 32 | + "watchify": "^3.7.0" | |
| 33 | + } | |
| 34 | +} |
+++ step15_watchify/src/index.html
... | ... | @@ -0,0 +1,16 @@ |
| 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="first"></div> | |
| 9 | + <div id="second"></div> | |
| 10 | + <div id="third"></div> | |
| 11 | + | |
| 12 | + <script src="js/first.js"></script> | |
| 13 | + <script src="js/second.js"></script> | |
| 14 | + <script src="js/third.js"></script> | |
| 15 | +</body> | |
| 16 | +</html> |
+++ step15_watchify/src/js/first.js
... | ... | @@ -0,0 +1,10 @@ |
| 1 | +import React from 'react'; | |
| 2 | +import ReactDOM from 'react-dom'; | |
| 3 | + | |
| 4 | +let rootElement = document.getElementById('first'); | |
| 5 | +let message = 'Hello, this is first!'; | |
| 6 | + | |
| 7 | +ReactDOM.render( | |
| 8 | + <div>{message}</div>, | |
| 9 | + rootElement | |
| 10 | +); |
+++ step15_watchify/src/js/second.js
... | ... | @@ -0,0 +1,10 @@ |
| 1 | +import React from 'react'; | |
| 2 | +import ReactDOM from 'react-dom'; | |
| 3 | + | |
| 4 | +let rootElement = document.getElementById('second'); | |
| 5 | +let message = 'Hello, this is second!'; | |
| 6 | + | |
| 7 | +ReactDOM.render( | |
| 8 | + <div>{message}</div>, | |
| 9 | + rootElement | |
| 10 | +); |
+++ step15_watchify/src/js/third.js
... | ... | @@ -0,0 +1,10 @@ |
| 1 | +import React from 'react'; | |
| 2 | +import ReactDOM from 'react-dom'; | |
| 3 | + | |
| 4 | +let rootElement = document.getElementById('third'); | |
| 5 | +let message = 'Hello, this is third!'; | |
| 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?