1+ var path = require ( "path" )
2+
3+ var gulp = require ( "gulp" )
4+ var minify = require ( "gulp-minify" )
5+ var gutil = require ( "gulp-util" )
6+ var replace = require ( 'gulp-replace' )
7+ var clean = require ( 'gulp-clean' )
8+
9+ var webpack = require ( "webpack" )
10+
11+ var pkg = require ( "./package.json" )
12+ var webpackConfig = require ( "./webpack.config.js" )
13+
14+ // config
15+ var config = {
16+ src : path . join ( __dirname ) ,
17+ configPath : path . join ( __dirname , 'config.js' ) ,
18+ sdkPath : path . join ( __dirname , "examples" , "browser" , "scripts" )
19+ } ;
20+
21+ gulp . task ( "copy-to-sdk" , function ( callback ) {
22+ // copy the generated files to sdk quick start
23+ gulp
24+ . src ( path . join ( webpackConfig . output . path , webpackConfig . output . filename ) )
25+ . pipe ( gulp . dest ( config . sdkPath ) )
26+ gulp
27+ . src ( path . join ( webpackConfig . output . path , webpackConfig . output . filename . replace ( '.js' , '.min.js' ) ) )
28+ . pipe ( gulp . dest ( config . sdkPath ) )
29+ } )
30+
31+ gulp . task ( "watch" , function ( callback ) {
32+ gulp . watch ( config . src + '/**/*.js' , [ "build" ] )
33+ } )
34+
35+ gulp . task ( "webpack:clean" , function ( callback ) {
36+ return gulp . src ( [
37+ path . join ( webpackConfig . output . path , webpackConfig . output . filename ) ,
38+ path . join ( webpackConfig . output . path , webpackConfig . output . filename . replace ( '.js' , '.min.js' ) ) ,
39+ path . join ( config . sdkPath , webpackConfig . output . filename ) ,
40+ path . join ( config . sdkPath , webpackConfig . output . filename . replace ( '.js' , '.min.js' ) )
41+ ] )
42+ . pipe ( clean ( { force : true } ) )
43+ . pipe ( gulp . dest ( webpackConfig . output . path ) )
44+ callback ( )
45+ } )
46+
47+ // Production build
48+ gulp . task ( "build" , [ "webpack:minbuild" , "copy-to-sdk" ] ) ;
49+
50+ gulp . task ( "webpack:minbuild" , [ "webpack:build" ] , function ( callback ) {
51+ var _config = JSON . parse ( JSON . stringify ( webpackConfig ) ) ;
52+ gulp . src ( path . join ( _config . output . path , _config . output . filename . replace ( '.js' , '.min.js' ) ) )
53+ . pipe ( replace ( "stag-api.contentstack.io" , "api.contentstack.io" ) )
54+ . pipe ( gulp . dest ( _config . output . path ) ) ;
55+
56+ gulp
57+ . src ( path . join ( _config . output . path , _config . output . filename ) )
58+ . pipe ( replace ( "stag-api.contentstack.io" , "api.contentstack.io" ) )
59+ . pipe ( minify ( {
60+ ext :{
61+ src :'.js' ,
62+ min :'.min.js'
63+ } ,
64+ mangle : false
65+ } ) )
66+ . pipe ( gulp . dest ( _config . output . path ) ) ;
67+ } ) ;
68+
69+ gulp . task ( "webpack:build" , [ "webpack:clean" ] , function ( callback ) {
70+ // run webpack
71+ webpack ( webpackConfig , function ( err , stats ) {
72+ if ( err ) throw new gutil . PluginError ( "webpack:build" , err ) ;
73+ gutil . log ( "[webpack:build]" , stats . toString ( {
74+ colors : true
75+ } ) ) ;
76+ callback ( ) ;
77+ } ) ;
78+ } ) ;
79+
80+ gulp . task ( "default" , [ "watch" ] ) ;
0 commit comments