- mkdir 01-es6
- cd 01-es6/
- mkdir app
- mkdir server
- mkdir tasks
- cd app/
- mkdir css
- mkdir js
- mkdir views
- mkdir js/class
- touch js/class/test.js
- touch js/index.js
- touch views/error.ejs
- touch views/index.ejs
- cd ../server/
- express -e .
- npm install
- cd ../tasks/
- mkdir util
- touch util/args.js
- cd ..
- npm init
- touch .babelrc
- touch gulpfile.babel.js
- tasks/util/args.js
import yargs from 'yargs'
const args = yargs
.option('production', {
boolean: true,
default: false,
describe: 'min all scripts'
})
.option('watch', {
boolean: true,
default: false,
describe: 'watch all files'
})
.option('verbose', {
boolean: true,
default: false,
describe: 'log'
})
.option('sourcemaps', {
describe: 'force the creation of sourcemaps'
})
.option('port', {
string: true,
default: 8080,
describe: 'server port'
})
.argv
- touch tasks/scripts.js
- tasks/scripts.js
import gulp from 'gulp'
import gulpif from 'gulp-if'
import concat from 'gulp-concat'
import webpack from 'webpack'
import gulpWebpack from 'webpack-stream'
import named from 'vinyl-named'
import livereload from 'gulp-livereload'
import plumber from 'gulp-plumber'
import rename from 'gulp-rename'
import uglify from 'gulp-uglify'
import {log, colors} from 'gulp-util'
import args from './util/args'
- cnpm install gulp gulp-if gulp-concat webpack webpack-stream vinyl-named gulp-livereload gulp-plumber gulp-rename gulp-uglify gulp-util yargs --save-dev
- tasks/scripts.js
gulp.task('scripts', () => {
return gulp.src(['app/js/index.js'])
.pipe(plumber({
errorHandle: function () {
}
}))
.pipe(named())
.pipe(gulpWebpack({
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel'
}
]
}
}), null, (err, stats) => {
log(`Finished '${colors.cyan('scripts')}'`, stats.toString({
chunks: false
}))
})
.pipe(gulp.dest('server/public/js'))
.pipe(rename({
basename: 'cp',
extname: '.min.js'
}))
.pipe(uglify({
compress: {
properties: false
},
output: {
'quote_keys': true
}
}))
.pipe(gulp.dest('server/public/js'))
.pipe(gulpif(args.watch, livereload()))
})
- touch tasks/pages.js
- tasks/page.js
import gulp from 'gulp'
import gulpif from 'gulp-if'
import livereload from 'gulp-livereload'
import args from './util/args'
gulp.task('pages', () => {
return gulp.src('app/**/*.ejs')
.pipe(gulp.dest('server'))
.pipe(gulpif(args.watch, livereload()))
})
- touch tasks/css.js
- tasks/css.js
import gulp from 'gulp'
import gulpif from 'gulp-if'
import livereload from 'gulp-livereload'
import args from './util/args'
gulp.task('css', () => {
return gulp.src('app/**/*.css')
.pipe(gulp.dest('server/public'))
})
- touch tasks/server.js
- tasks/server.js
import gulp from 'gulp'
import gulpif from 'gulp-if'
import liveserver from 'gulp-live-server'
import args from './util/args'
gulp.task('serve', (cb) => {
if (!args.watch) return cb()
var server = liveserver.new(['--harmony', 'server/bin/www'])
server.start()
gulp.watch(['server/public/**/*.js', 'server/views/**/*.ejs'], function (file) {
server.notify.apply(server, [file])
})
gulp.watch(['server/routes/**/*.js', 'server/app.js'], function () {
server.start.bind(server)()
})
})
- touch tasks/browser.js
- tasks/browser.js
import gulp from 'gulp'
import gulpif from 'gulp-if'
import gutil from 'gulp-util'
import args from './util/args'
gulp.task('browser', (cb) => {
if (!args.watch) return cb()
gulp.watch('app/**/*.js', ['scripts'])
gulp.watch('app/**/*.ejs', ['pages'])
gulp.watch('app/**/*.css', ['css'])
})