step 11 incremental build 추가
@b6ce0afb99d15b6c73ab43511df076c1a02efa62
--- README.md
+++ README.md
... | ... | @@ -67,6 +67,7 @@ |
| 67 | 67 |
- [Step 08. BrowerSync로 미니 서버를 띄워서 작업하기](https://github.com/eu81273/gulp-step-by-step/tree/master/step08_browser_sync) |
| 68 | 68 |
- [Step 09. Browserify로 모듈 번들링하기](https://github.com/eu81273/gulp-step-by-step/tree/master/step09_browserify) |
| 69 | 69 |
- [Step 10. 소스맵 사용하기](https://github.com/eu81273/gulp-step-by-step/tree/master/step10_sourcemaps) |
| 70 |
+- [Step 11. 증분 빌드 적용하기](https://github.com/eu81273/gulp-step-by-step/tree/master/step11_incremental_build) |
|
| 70 | 71 |
|
| 71 | 72 |
#### Gulp 를 활용하는데 도움이 되는 링크들 |
| 72 | 73 |
- [Gulp Recipes](https://github.com/gulpjs/gulp/tree/master/docs/recipes) |
--- package.json
+++ package.json
... | ... | @@ -21,9 +21,12 @@ |
| 21 | 21 |
"browserify": "^11.2.0", |
| 22 | 22 |
"gulp": "^3.9.0", |
| 23 | 23 |
"gulp-autoprefixer": "^3.1.0", |
| 24 |
+ "gulp-cached": "^1.1.0", |
|
| 24 | 25 |
"gulp-concat": "^2.6.0", |
| 25 | 26 |
"gulp-minify-css": "^1.2.1", |
| 26 | 27 |
"gulp-minify-html": "^1.0.4", |
| 28 |
+ "gulp-newer": "^1.1.0", |
|
| 29 |
+ "gulp-remember": "^0.3.0", |
|
| 27 | 30 |
"gulp-sourcemaps": "^1.6.0", |
| 28 | 31 |
"gulp-uglify": "^1.4.0", |
| 29 | 32 |
"vinyl-buffer": "^1.0.0", |
+++ step11_incremental_build/gulpfile.js
... | ... | @@ -0,0 +1,72 @@ |
| 1 | +var gulp = require('gulp');
| |
| 2 | +var uglify = require('gulp-uglify');
| |
| 3 | +var concat = require('gulp-concat');
| |
| 4 | +var minifycss = require('gulp-minify-css');
| |
| 5 | +var minifyhtml = require('gulp-minify-html');
| |
| 6 | +var browserSync = require('browser-sync').create();
| |
| 7 | +var browserify = require('browserify');
| |
| 8 | +var source = require('vinyl-source-stream');
| |
| 9 | +var buffer = require('vinyl-buffer');
| |
| 10 | +var sourcemaps = require('gulp-sourcemaps');
| |
| 11 | +var newer = require('gulp-newer');
| |
| 12 | +var cached = require('gulp-cached');
| |
| 13 | +var remember = require('gulp-remember');
| |
| 14 | + | |
| 15 | +//dist 폴더를 기준으로 웹서버 실행 | |
| 16 | +gulp.task('server', ['uglify', 'minifycss', 'minifyhtml'], function () {
| |
| 17 | + return browserSync.init({
| |
| 18 | + server: {
| |
| 19 | + baseDir: './dist' | |
| 20 | + } | |
| 21 | + }); | |
| 22 | +}); | |
| 23 | + | |
| 24 | +//HTML 파일을 minify | |
| 25 | +gulp.task('minifyhtml', function () {
| |
| 26 | + return gulp.src('src/**/*.html') //src 폴더 아래의 모든 html 파일을
| |
| 27 | + .pipe(newer('dist')) //dist에 있는 결과물보다 새로운 파일만 다음 단계로 진행
| |
| 28 | + .pipe(minifyhtml()) //minify 해서 | |
| 29 | + .pipe(gulp.dest('dist')) //dist 폴더에 저장
| |
| 30 | + .pipe(browserSync.reload({stream:true})); //browserSync 로 브라우저에 반영
| |
| 31 | +}); | |
| 32 | + | |
| 33 | +//자바스크립트 파일을 browserify로 번들링 | |
| 34 | +gulp.task('uglify', function () {
| |
| 35 | + return browserify({entries: ['src/js/main.js'], debug: true})
| |
| 36 | + .bundle() //browserify로 번들링 | |
| 37 | + .on('error', function (err) {
| |
| 38 | + //browserify bundling 과정에서 오류가 날 경우 gulp가 죽지않도록 예외처리 | |
| 39 | + console.error(err); | |
| 40 | + this.emit('end');
| |
| 41 | + }) | |
| 42 | + .pipe(source('main.js')) //vinyl object 로 변환
| |
| 43 | + .pipe(buffer()) //buffered vinyl object 로 변환 | |
| 44 | + .pipe(sourcemaps.init({loadMaps: true, debug: true})) //소스맵 생성 준비
| |
| 45 | + .pipe(uglify()) //minify 해서 | |
| 46 | + .pipe(sourcemaps.write('./')) //생성된 소스맵을 스트림에 추가
| |
| 47 | + .pipe(gulp.dest('dist/js')) //dist 폴더에 저장
| |
| 48 | + .pipe(browserSync.reload({stream:true})); //browserSync 로 브라우저에 반영
| |
| 49 | +}); | |
| 50 | + | |
| 51 | +//CSS 파일을 minify | |
| 52 | +gulp.task('minifycss', function () {
| |
| 53 | + return gulp.src('src/**/*.css') //src 폴더 아래의 모든 css 파일을
| |
| 54 | + .pipe(sourcemaps.init({loadMaps: true, debug: true})) //소스맵 생성 준비
| |
| 55 | + .pipe(cached('css')) //파일들을 캐시하고 캐시된 것보다 새로운 파일만 다음 단계로 진행
| |
| 56 | + .pipe(minifycss()) //새로운 파일만 minify (@import된 파일이 수정된 경우 최상위 파일은 캐시된 상태 그대로이므로 minifycss를 타지 않아 정상적으로 종속성 관리가 이루어지지 않는 점을 주의!) | |
| 57 | + .pipe(remember('css')) //minify된 새로운 파일과 캐시된 나머지 내용들을 다시 스트림으로
| |
| 58 | + .pipe(concat('main.css')) //병합하고
| |
| 59 | + .pipe(sourcemaps.write('./')) //생성된 소스맵을 스트림에 추가
| |
| 60 | + .pipe(gulp.dest('dist/css')) //dist 폴더에 저장
| |
| 61 | + .pipe(browserSync.reload({stream:true})); //browserSync 로 브라우저에 반영
| |
| 62 | +}); | |
| 63 | + | |
| 64 | +//파일 변경 감지 | |
| 65 | +gulp.task('watch', function () {
| |
| 66 | + gulp.watch('src/**/*.js', ['uglify']);
| |
| 67 | + gulp.watch('src/**/*.css', ['minifycss']);
| |
| 68 | + gulp.watch('src/**/*.html', ['minifyhtml']);
| |
| 69 | +}); | |
| 70 | + | |
| 71 | +//gulp를 실행하면 default 로 minifycss task를 실행 | |
| 72 | +gulp.task('default', ['server', 'watch']);
|
+++ step11_incremental_build/src/css/dummy1.css
... | ... | @@ -0,0 +1,3 @@ |
| 1 | +h2 {
| |
| 2 | + color: blue; | |
| 3 | +} |
+++ step11_incremental_build/src/css/dummy2.css
... | ... | @@ -0,0 +1,3 @@ |
| 1 | +b {
| |
| 2 | + font-size: 5em; | |
| 3 | +} |
+++ step11_incremental_build/src/dummy.html
... | ... | @@ -0,0 +1,1 @@ |
| 1 | +<!-- 내용없음! --> |
+++ step11_incremental_build/src/index.html
... | ... | @@ -0,0 +1,13 @@ |
| 1 | +<!DOCTYPE html> | |
| 2 | +<html lang="ko"> | |
| 3 | +<head> | |
| 4 | + <meta charset="UTF-8"> | |
| 5 | + <title>gulp example</title> | |
| 6 | + <link rel="stylesheet" type="text/css" href="css/main.css"> | |
| 7 | +</head> | |
| 8 | +<body> | |
| 9 | + <h2>Hello World!!</h2> | |
| 10 | + | |
| 11 | + <script src="js/main.js"></script> | |
| 12 | +</body> | |
| 13 | +</html> |
+++ step11_incremental_build/src/js/main.js
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +(function () { | |
| 2 | + var module1 = require('./modules/module1'); | |
| 3 | + var module2 = require('./modules/module2'); | |
| 4 | + var module3 = require('./modules/module3'); | |
| 5 | + | |
| 6 | + module1(); | |
| 7 | + module2(); | |
| 8 | + module3(); | |
| 9 | +})(); |
+++ step11_incremental_build/src/js/modules/module1.js
... | ... | @@ -0,0 +1,6 @@ |
| 1 | +//dummy hello world function | |
| 2 | +function hello1 () {
| |
| 3 | + console.log('hello world! - 1');
| |
| 4 | +} | |
| 5 | + | |
| 6 | +module.exports = hello1; |
+++ step11_incremental_build/src/js/modules/module2.js
... | ... | @@ -0,0 +1,6 @@ |
| 1 | +//dummy hello world function | |
| 2 | +function hello2 () {
| |
| 3 | + console.log('hello world! - 2');
| |
| 4 | +} | |
| 5 | + | |
| 6 | +module.exports = hello2; |
+++ step11_incremental_build/src/js/modules/module3.js
... | ... | @@ -0,0 +1,6 @@ |
| 1 | +//dummy hello world function | |
| 2 | +function hello3 () {
| |
| 3 | + console.log('hello world! - 3');
| |
| 4 | +} | |
| 5 | + | |
| 6 | +module.exports = hello3; |
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?