gulpfile.js 5.1 KB
Newer Older
N
nkzawa 已提交
1 2 3 4
const gulp = require('gulp')
const babel = require('gulp-babel')
const cache = require('gulp-cached')
const notify_ = require('gulp-notify')
N
nkzawa 已提交
5
const ava = require('gulp-ava')
D
Dan Zajdband 已提交
6
const benchmark = require('gulp-benchmark')
N
nkzawa 已提交
7
const sequence = require('run-sequence')
N
nkzawa 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const webpack = require('webpack-stream')
const del = require('del')

const babelOptions = {
  presets: ['es2015', 'react'],
  plugins: [
    'transform-async-to-generator',
    'transform-object-rest-spread',
    'transform-class-properties',
    'transform-runtime'
  ]
}

gulp.task('compile', [
  'compile-bin',
  'compile-lib',
  'compile-server',
25
  'compile-client'
N
nkzawa 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
])

gulp.task('compile-bin', () => {
  return gulp.src('bin/*')
  .pipe(cache('bin'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/bin'))
  .pipe(notify('Compiled binaries'))
})

gulp.task('compile-lib', () => {
  return gulp.src('lib/**/*.js')
  .pipe(cache('lib'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/lib'))
  .pipe(notify('Compiled lib files'))
})

gulp.task('compile-server', () => {
  return gulp.src('server/**/*.js')
  .pipe(cache('server'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/server'))
  .pipe(notify('Compiled server files'))
})

gulp.task('compile-client', () => {
  return gulp.src('client/**/*.js')
  .pipe(cache('client'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/client'))
  .pipe(notify('Compiled client files'))
})

N
nkzawa 已提交
60 61 62 63 64 65 66 67
gulp.task('compile-test', () => {
  return gulp.src('test/*.js')
  .pipe(cache('test'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/test'))
  .pipe(notify('Compiled test files'))
})

68
gulp.task('copy', ['copy-pages'])
N
nkzawa 已提交
69 70 71 72 73 74

gulp.task('copy-pages', () => {
  return gulp.src('pages/**/*.js')
  .pipe(gulp.dest('dist/pages'))
})

N
nkzawa 已提交
75 76 77
gulp.task('copy-test-fixtures', () => {
  return gulp.src('test/fixtures/**/*')
  .pipe(gulp.dest('dist/test/fixtures'))
D
Dan Zajdband 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
})

gulp.task('compile-bench', () => {
  return gulp.src('bench/*.js')
  .pipe(cache('bench'))
  .pipe(babel(babelOptions))
  .pipe(gulp.dest('dist/bench'))
  .pipe(notify('Compiled bench files'))
})

gulp.task('copy-bench-fixtures', () => {
  return gulp.src('bench/fixtures/**/*')
  .pipe(gulp.dest('dist/bench/fixtures'))
})
N
nkzawa 已提交
92

N
nkzawa 已提交
93
gulp.task('build', [
N
nkzawa 已提交
94 95
  'build-dev-client',
  'build-client'
N
nkzawa 已提交
96 97 98 99 100 101 102
])

gulp.task('build-dev-client', ['compile-lib', 'compile-client'], () => {
  return gulp
  .src('dist/client/next-dev.js')
  .pipe(webpack({
    quiet: true,
103
    output: { filename: 'next-dev.bundle.js', libraryTarget: 'var', library: 'require' },
N
nkzawa 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
    module: {
      loaders: [
        {
          test: /eval-script\.js$/,
          exclude: /node_modules/,
          loader: 'babel',
          query: {
            plugins: [
              'babel-plugin-transform-remove-strict-mode'
            ]
          }
        }
      ]
N
nkzawa 已提交
117
    }
N
nkzawa 已提交
118 119 120 121 122
  }))
  .pipe(gulp.dest('dist/client'))
  .pipe(notify('Built dev client'))
})

N
nkzawa 已提交
123
gulp.task('build-client', ['compile-lib', 'compile-client'], () => {
N
nkzawa 已提交
124 125 126 127
  return gulp
  .src('dist/client/next.js')
  .pipe(webpack({
    quiet: true,
128
    output: { filename: 'next.bundle.js', libraryTarget: 'var', library: 'require' },
N
nkzawa 已提交
129 130 131 132 133
    plugins: [
      new webpack.webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
N
nkzawa 已提交
134 135
      }),
      new webpack.webpack.optimize.UglifyJsPlugin()
N
nkzawa 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    ],
    module: {
      loaders: [
        {
          test: /eval-script\.js$/,
          exclude: /node_modules/,
          loader: 'babel',
          query: {
            plugins: [
              'babel-plugin-transform-remove-strict-mode'
            ]
          }
        }
      ]
    }
N
nkzawa 已提交
151 152 153 154 155
  }))
  .pipe(gulp.dest('dist/client'))
  .pipe(notify('Built release client'))
})

156
gulp.task('test', ['compile', 'copy', 'compile-test', 'copy-test-fixtures'], () => {
N
nkzawa 已提交
157 158 159 160
  return gulp.src('dist/test/*.js')
  .pipe(ava())
})

161
gulp.task('bench', ['compile', 'copy', 'compile-bench', 'copy-bench-fixtures'], () => {
D
Dan Zajdband 已提交
162 163 164 165 166 167
  return gulp.src('dist/bench/*.js', {read: false})
  .pipe(benchmark({
    reporters: benchmark.reporters.etalon('RegExp#test')
  }))
})

N
nkzawa 已提交
168 169 170 171
gulp.task('watch', [
  'watch-bin',
  'watch-lib',
  'watch-server',
N
nkzawa 已提交
172 173
  'watch-client',
  'watch-pages'
N
nkzawa 已提交
174 175
])

N
nkzawa 已提交
176
gulp.task('watch-bin', () => {
N
nkzawa 已提交
177 178 179 180 181
  return gulp.watch('bin/*', [
    'compile-bin'
  ])
})

N
nkzawa 已提交
182
gulp.task('watch-lib', () => {
N
nkzawa 已提交
183 184
  return gulp.watch('lib/**/*.js', [
    'compile-lib',
N
nkzawa 已提交
185
    'build'
N
nkzawa 已提交
186 187 188
  ])
})

N
nkzawa 已提交
189
gulp.task('watch-server', () => {
N
nkzawa 已提交
190 191 192 193 194
  return gulp.watch('server/**/*.js', [
    'compile-server'
  ])
})

N
nkzawa 已提交
195
gulp.task('watch-client', () => {
N
nkzawa 已提交
196 197
  return gulp.watch('client/**/*.js', [
    'compile-client',
N
nkzawa 已提交
198
    'build'
N
nkzawa 已提交
199 200 201
  ])
})

N
nkzawa 已提交
202 203 204 205 206 207
gulp.task('watch-pages', () => {
  return gulp.watch('pages/**/*.js', [
    'copy-pages'
  ])
})

N
nkzawa 已提交
208
gulp.task('clean', () => {
N
nkzawa 已提交
209 210 211 212 213
  return del('dist')
})

gulp.task('clean-test', () => {
  return del('dist/test')
N
nkzawa 已提交
214 215 216 217 218
})

gulp.task('default', [
  'compile',
  'build',
N
nkzawa 已提交
219
  'copy',
N
nkzawa 已提交
220
  'test',
N
nkzawa 已提交
221 222 223
  'watch'
])

N
nkzawa 已提交
224 225 226
gulp.task('release', (cb) => {
  sequence('clean', [
    'compile',
N
nkzawa 已提交
227
    'build',
N
nkzawa 已提交
228
    'copy',
N
nkzawa 已提交
229 230 231
    'test'
  ], 'clean-test', cb)
})
N
nkzawa 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245

// avoid logging to the console
// that we created a notification
notify_.logLevel(0)

// notification helper
function notify (msg) {
  return notify_({
    title: '▲ Next',
    message: msg,
    icon: false,
    onLast: true
  })
}