|
| 1 | +const root = { |
| 2 | + src: `${__dirname}/source`, |
| 3 | + dist: `${__dirname}/public`, |
| 4 | + tmp: `${__dirname}/tmp` |
| 5 | +} |
| 6 | + |
| 7 | +const paths = { |
| 8 | + src: { |
| 9 | + root: `${root.src}`, |
| 10 | + html: `${root.src}/html`, |
| 11 | + js: `${root.src}/js`, |
| 12 | + css: `${root.src}/css`, |
| 13 | + static: `${root.src}/static` |
| 14 | + }, |
| 15 | + dist: { |
| 16 | + root: `${root.dist}`, |
| 17 | + js: `${root.dist}/js`, |
| 18 | + css: `${root.dist}/css`, |
| 19 | + font: `${root.dist}/fonts` |
| 20 | + }, |
| 21 | + node: { |
| 22 | + modules: `${__dirname}/node_modules` |
| 23 | + } |
| 24 | +} |
| 25 | +const resource = { |
| 26 | + src: { |
| 27 | + jade: `${paths.src.html}/**/*.jade`, |
| 28 | + webpack: { |
| 29 | + babel: `${paths.src.js}/**/*.js`, |
| 30 | + vue: `${paths.src.js}/**/*.vue` |
| 31 | + }, |
| 32 | + sass: `${paths.src.css}/**/*.s+(a|c)ss`, |
| 33 | + static: `${paths.src.static}/**/*`, |
| 34 | + }, |
| 35 | + vendor: { |
| 36 | + js: { |
| 37 | + jquery: `${paths.node.modules}/jquery/dist/jquery.js`, |
| 38 | + bootstrap: `${paths.node.modules}/bootstrap-sass/assets/javascripts/bootstrap.js`, |
| 39 | + eventemitter: `${paths.node.modules}/wolfy87-eventemitter/EventEmitter.js` |
| 40 | + }, |
| 41 | + fontawesome: `${paths.node.modules}/font-awesome/fonts/**/*`, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +import gulp from 'gulp' |
| 46 | +import gulpLoaderPlugins from 'gulp-load-plugins' |
| 47 | +import del from 'del' |
| 48 | +import path from 'path' |
| 49 | +import webpack from 'webpack' |
| 50 | +import webpackStream from 'webpack-stream' |
| 51 | +import runSequence from 'run-sequence' |
| 52 | +import browserSyncTool from 'browser-sync' |
| 53 | +import RevAll from 'gulp-rev-all' |
| 54 | + |
| 55 | +const $ = gulpLoaderPlugins() |
| 56 | +const browserSync = browserSyncTool.create() |
| 57 | + |
| 58 | +let production = false |
| 59 | + |
| 60 | +// build and watch for developer |
| 61 | +gulp.task('default', ['build', 'server']) |
| 62 | + |
| 63 | +//## build for developer |
| 64 | +gulp.task('build', (callback) => |
| 65 | + runSequence('clean', ['build:jade', 'build:sass', 'build:webpack', 'build:static'], callback) |
| 66 | +) |
| 67 | + |
| 68 | +//## build production |
| 69 | +gulp.task('build-prod', (callback) => |
| 70 | + runSequence('production', 'build', 'revision', callback) |
| 71 | +) |
| 72 | + |
| 73 | +// clean dist |
| 74 | +gulp.task('clean', () => |
| 75 | + del.sync([`${paths.dist.root}/*`, `!${paths.dist.root}/.git*`], { force: true }) |
| 76 | +) |
| 77 | + |
| 78 | +// production option |
| 79 | +gulp.task('production', () => production = true ) |
| 80 | + |
| 81 | +// support Resource Revision |
| 82 | +gulp.task('revision', (callback) => |
| 83 | + runSequence('revision:clean', 'revision:append', 'clean', 'revision:copy', 'revision:clean', callback) |
| 84 | +) |
| 85 | + |
| 86 | +// compile Webpack [ ES6(Babel) / Vue -> SPA(main.js) ] |
| 87 | +gulp.task('build:webpack', () => { |
| 88 | + process.env.NODE_ENV = (production == true) ? 'production' : 'development' |
| 89 | + let plugins = [ new webpack.optimize.DedupePlugin() ] |
| 90 | + if (production) plugins.push(new webpack.optimize.UglifyJsPlugin({compress: { warnings: false }})) |
| 91 | + gulp.src([resource.src.webpack.babel, resource.src.webpack.vue]) |
| 92 | + .pipe($.plumber()) |
| 93 | + .pipe(webpackStream({ |
| 94 | + entry: `${paths.src.js}/main.js`, |
| 95 | + output: {filename: 'bundler.js'}, |
| 96 | + watch: !production, |
| 97 | + module: { |
| 98 | + loaders: [ |
| 99 | + {test: /\.(js|jsx)$/, loader: 'babel', query: {presets: ['es2015', 'react']}} |
| 100 | + ] |
| 101 | + }, |
| 102 | + resolve: { |
| 103 | + modulesDirectories: ['node_modules', paths.src.js], |
| 104 | + extensions: ['', '.js', ".jsx", ".jade"] |
| 105 | + }, |
| 106 | + plugins: plugins |
| 107 | + }, webpack)) |
| 108 | + .pipe(gulp.dest(paths.dist.js)) |
| 109 | + .pipe(browserSync.stream()) |
| 110 | +}) |
| 111 | + |
| 112 | +// compile Jade -> HTML |
| 113 | +gulp.task('build:jade', () => { |
| 114 | + gulp.src(resource.src.jade) |
| 115 | + .pipe($.plumber()) |
| 116 | + .pipe($.jade()) |
| 117 | + .pipe($.htmlhint()) |
| 118 | + .pipe($.htmlhint.reporter()) |
| 119 | + .pipe(gulp.dest(paths.dist.root)) |
| 120 | + .pipe(browserSync.stream()) |
| 121 | +}) |
| 122 | + |
| 123 | +// compile Sass -> CSS |
| 124 | +gulp.task('build:sass', () => { |
| 125 | + gulp.src(resource.src.sass) |
| 126 | + .pipe($.plumber()) |
| 127 | + .pipe($.sass()) |
| 128 | + .pipe($.concat('style.css')) |
| 129 | + .pipe($.pleeease()) |
| 130 | + .pipe(gulp.dest(paths.dist.css)) |
| 131 | + .pipe(browserSync.stream()) |
| 132 | +}) |
| 133 | + |
| 134 | +// copy Static Resource |
| 135 | +gulp.task('build:static', () => { |
| 136 | + const libs = resource.vendor.js |
| 137 | + gulp.src(Object.keys(libs).map((key) => libs[key])) |
| 138 | + .pipe($.concat("vendor.js")) |
| 139 | + .pipe($.if(production, $.uglify())) |
| 140 | + .pipe(gulp.dest(paths.dist.js)) |
| 141 | + gulp.src(resource.vendor.fontawesome) |
| 142 | + .pipe(gulp.dest(paths.dist.font)) |
| 143 | + gulp.src(resource.src.static) |
| 144 | + .pipe(gulp.dest(paths.dist.root)) |
| 145 | +}) |
| 146 | + |
| 147 | +// run Development Web Server (BrowserSync) [localhost:3000] |
| 148 | +gulp.task('server', () => { |
| 149 | + browserSync.init({ |
| 150 | + server: {baseDir: paths.dist.root}, |
| 151 | + notify: false |
| 152 | + }) |
| 153 | + // watch for source |
| 154 | + gulp.watch(resource.src.jade, ['build:jade']) |
| 155 | + gulp.watch(resource.src.sass, ['build:sass']) |
| 156 | + gulp.watch(resource.src.static, ['build:static']) |
| 157 | +}) |
| 158 | + |
| 159 | +// append Resource Revision |
| 160 | +gulp.task('revision:clean', () => { |
| 161 | + del.sync([root.tmp], { force: true }) |
| 162 | +}) |
| 163 | + |
| 164 | +gulp.task('revision:append', () => { |
| 165 | + let revAll = new RevAll({dontRenameFile: [/^\/favicon.ico$/g, '.html']}) |
| 166 | + gulp.src(`${paths.dist.root}/**/*`) |
| 167 | + .pipe(revAll.revision()) |
| 168 | + .pipe(gulp.dest(root.tmp)) |
| 169 | +}) |
| 170 | + |
| 171 | +gulp.task('revision:copy', () => { |
| 172 | + gulp.src(`${root.tmp}/**/*`) |
| 173 | + .pipe(gulp.dest(paths.dist.root)) |
| 174 | +}) |
0 commit comments