step 14 React 추가
@583fc5104def4730e55afc90ce045f3dabaaa9c2
+++ step14_react/gulpfile.js
... | ... | @@ -0,0 +1,53 @@ |
| 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 source = require('vinyl-source-stream');
| |
| 7 | +var buffer = require('vinyl-buffer');
| |
| 8 | +var sourcemaps = require('gulp-sourcemaps');
| |
| 9 | + | |
| 10 | +//dist 폴더를 기준으로 웹서버 실행 | |
| 11 | +gulp.task('server', ['build:javascript', 'build:html'], function () {
| |
| 12 | + return browserSync.init({
| |
| 13 | + server: {
| |
| 14 | + baseDir: './dist' | |
| 15 | + } | |
| 16 | + }); | |
| 17 | +}); | |
| 18 | + | |
| 19 | +//HTML 파일을 dist 로 복사 | |
| 20 | +gulp.task('build:html', function () {
| |
| 21 | + return gulp.src('./src/**/*.html')
| |
| 22 | + .pipe(gulp.dest('dist'))
| |
| 23 | + .pipe(browserSync.reload({stream: true}));
| |
| 24 | +}); | |
| 25 | + | |
| 26 | +//자바스크립트 파일을 browserify로 번들링 | |
| 27 | +gulp.task('build:javascript', function () {
| |
| 28 | + | |
| 29 | + return browserify({entries: ['src/js/main.js'], debug: true})
| |
| 30 | + .transform('babelify', {presets: ['es2015', 'react']})
| |
| 31 | + .bundle() | |
| 32 | + .on('error', function (err) {
| |
| 33 | + //browserify bundling 과정에서 오류가 날 경우 gulp가 죽지않도록 예외처리 | |
| 34 | + console.error(err); | |
| 35 | + this.emit('end');
| |
| 36 | + }) | |
| 37 | + .pipe(source('main.js')) //vinyl object 로 변환
| |
| 38 | + .pipe(buffer()) //buffered vinyl object 로 변환 | |
| 39 | + .pipe(sourcemaps.init({loadMaps: true, debug: true})) //소스맵 생성 준비
| |
| 40 | + .pipe(uglify()) //minify 해서 | |
| 41 | + .pipe(sourcemaps.write('./')) //생성된 소스맵을 스트림에 추가
| |
| 42 | + .pipe(gulp.dest('dist/js')) //dist 폴더에 저장
| |
| 43 | + .pipe(browserSync.reload({stream: true})); //browserSync 로 브라우저에 반영
| |
| 44 | +}); | |
| 45 | + | |
| 46 | +//파일 변경 감지 | |
| 47 | +gulp.task('watch', function () {
| |
| 48 | + gulp.watch('src/**/*.js', ['build:javascript']);
| |
| 49 | + gulp.watch('src/**/*.html', ['build:html']);
| |
| 50 | +}); | |
| 51 | + | |
| 52 | +//gulp를 실행하면 default 로 minifycss task를 실행 | |
| 53 | +gulp.task('default', ['server', 'watch']);
|
+++ step14_react/package.json
... | ... | @@ -0,0 +1,32 @@ |
| 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 | + "react": "^0.14.6", | |
| 20 | + "react-dom": "^0.14.6", | |
| 21 | + "babel-preset-es2015": "^6.3.13", | |
| 22 | + "babel-preset-react": "^6.3.13", | |
| 23 | + "babelify": "^7.2.0", | |
| 24 | + "browser-sync": "^2.11.1", | |
| 25 | + "browserify": "^13.0.0", | |
| 26 | + "gulp": "^3.9.0", | |
| 27 | + "gulp-sourcemaps": "^1.6.0", | |
| 28 | + "gulp-uglify": "^1.4.0", | |
| 29 | + "vinyl-buffer": "^1.0.0", | |
| 30 | + "vinyl-source-stream": "^1.1.0" | |
| 31 | + } | |
| 32 | +} |
+++ step14_react/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> |
+++ step14_react/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?