Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Gulp config:
  2.  
  3. import gulp from 'gulp';
  4. import fs from 'fs';
  5. import ftp from 'vinyl-ftp';
  6. import plugins from 'gulp-load-plugins';
  7. import buffer from 'vinyl-buffer';
  8. import del from 'del';
  9. import merge from 'merge-stream';
  10. import webpackStream from 'webpack-stream';
  11. import webpack2 from 'webpack';
  12. import webpackConfig from './webpack.config.js';
  13.  
  14. const $ = plugins();
  15.  
  16. const isProduction = (() => {
  17.     return process.env.NODE_ENV === 'production';
  18. })();
  19.  
  20. /**
  21.  * @param {boolean} watch
  22.  */
  23. const compileJs = ({ watch = false }) => {
  24.     const config = Object.assign({
  25.         watch,
  26.     }, {}, webpackConfig);
  27.  
  28.     return gulp.src('./src/js/app.js')
  29.         .pipe(webpackStream(config, webpack2))
  30.         .pipe(gulp.dest('dist/'));
  31. };
  32.  
  33. gulp.task('js:build', (done) => {
  34.     compileJs({
  35.         watch: false
  36.     });
  37.     done();
  38. });
  39.  
  40. gulp.task('js:watch', (done) => {
  41.     compileJs({
  42.         watch: true
  43.     });
  44.     done();
  45. });
  46.  
  47. const onErrorHandler = $.notify.onError({
  48.     message: "Error: <%= error.message %>"
  49. });
  50.  
  51. const srcPaths = {
  52.     images: {
  53.         sprites: {
  54.             all: './src/img/sprites/**/*.png',
  55.             retina: './src/img/sprites/**/*@2x.png'
  56.         },
  57.         images: [
  58.             './src/img/images/**/*'
  59.         ]
  60.     },
  61.     fonts: [
  62.         './src/font/**/*'
  63.     ]
  64. };
  65.  
  66. // Compiles SCSS to CSS
  67. gulp.task('sass', () => {
  68.     return gulp
  69.         .src(['./src/scss/app.scss'])
  70.         .pipe($.plumber())
  71.         .pipe($.if(!isProduction, $.sourcemaps.init()))
  72.         .pipe($.sass({
  73.             precision: 8, // Required for bootstrap
  74.             outputStyle: 'expanded'
  75.         }))
  76.         .on('error', onErrorHandler)
  77.         .on('error', $.sass.logError)
  78.         .pipe($.postcss())
  79.         .pipe($.if(isProduction, $.cleanCss()))
  80.         .pipe($.if(!isProduction, $.sourcemaps.write()))
  81.         .pipe(gulp.dest('./dist/css/'))
  82.         .pipe($.notify('sass task complete'))
  83.         .pipe($.size({ title: 'css' }));
  84. });
  85.  
  86. gulp.task('sass-lint', () => {
  87.     return gulp
  88.         .src('./src/scss/*.scss')
  89.         .pipe($.plumber())
  90.         .pipe($.sassLint())
  91.         .pipe($.sassLint.failOnError())
  92. });
  93.  
  94. gulp.task('copy-fonts', () => {
  95.     return gulp
  96.         .src(srcPaths.fonts)
  97.         .pipe(gulp.dest('./dist/font/'));
  98. });
  99.  
  100. gulp.task('copy-images', () => {
  101.     return gulp
  102.         .src(srcPaths.images.images)
  103.         .pipe($.imagemin({
  104.             progressive: true
  105.         }))
  106.         .pipe(gulp.dest('./dist/img'))
  107.         .pipe($.size({ title: 'images' }));
  108. });
  109.  
  110. gulp.task('sprites', () => {
  111.     const spriteData = gulp.src(srcPaths.images.sprites.all)
  112.         .pipe($.spritesmith({
  113.             imgName: '../img/sprite.png',
  114.             retinaSrcFilter: [srcPaths.images.sprites.retina],
  115.             retinaImgName: '../img/sprite.png@2x.png',
  116.             cssName: 'sprite.scss'
  117.         }))
  118.         .on('error', onErrorHandler);
  119.  
  120.     const spriteStream = spriteData.img
  121.         .pipe(buffer())
  122.         .pipe($.imagemin({
  123.             progressive: true
  124.         }))
  125.         .pipe(gulp.dest('./dist/img/'));
  126.  
  127.     const cssStream = spriteData.css.pipe(gulp.dest('./temp/'));
  128.  
  129.     return merge(spriteStream, cssStream);
  130. });
  131.  
  132. gulp.task('ftp', () => {
  133.  
  134.     try {
  135.         const config = JSON.parse(fs.readFileSync('./ftp.json', 'utf8'));
  136.         const files = JSON.parse(fs.readFileSync('./deploy.json', 'utf8'));
  137.         const remoteDir = config.dir;
  138.  
  139.         const conn = ftp.create({
  140.             host: config.host,
  141.             user: config.user,
  142.             password: config.password,
  143.             log: $.util.log
  144.         });
  145.  
  146.         return gulp.src(files, {
  147.             base: '.',
  148.             buffer: false
  149.         })
  150.             .pipe(conn.newerOrDifferentSize(remoteDir))
  151.             .pipe(conn.dest(remoteDir));
  152.     }
  153.     catch (e) {
  154.         onErrorHandler(e);
  155.     }
  156.  
  157. });
  158.  
  159. gulp.task('clean', () => {
  160.     return del([
  161.         'dist/**',
  162.     ]);
  163. });
  164.  
  165. gulp.task('build', gulp.series('clean', gulp.parallel(
  166.     'js:build',
  167.     'sass-lint',
  168.     'copy-images',
  169.     'copy-fonts',
  170.     gulp.series(
  171.         'sprites',
  172.         'sass'
  173.     )
  174. )));
  175.  
  176. gulp.task('deploy', gulp.series(
  177.     'build',
  178.     'ftp'
  179. ));
  180.  
  181. gulp.task('watch', () => {
  182.  
  183.     gulp.watch([
  184.         srcPaths.images.images,
  185.         './src/scss/**/*.scss'
  186.     ], gulp.parallel('sass'));
  187.  
  188.     gulp.watch(srcPaths.images.images, gulp.parallel('copy-images'));
  189.  
  190.     gulp.watch([
  191.         srcPaths.images.sprites.all,
  192.         srcPaths.images.sprites.retina
  193.     ], gulp.parallel('sprites'));
  194.  
  195.     gulp.watch(srcPaths.fonts, gulp.parallel('copy-fonts'));
  196.  
  197.     compileJs({
  198.         watch: true
  199.     });
  200.  
  201. });
  202.  
  203. gulp.task('default', gulp.series('build', 'watch'));
  204.  
  205. // Webpack config:
  206. const path = require('path');
  207. const webpack = require('webpack');
  208. const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
  209.  
  210. module.exports = {
  211.     entry: '', // See Gulp config
  212.     output: {
  213.         path: path.resolve(__dirname, './dist'),
  214.         filename: 'js/app.js'
  215.     },
  216.     module: {
  217.         rules: [
  218.             {
  219.                 test: /\.vue$/,
  220.                 loader: 'vue-loader',
  221.                 options: {
  222.                     loaders: {
  223.                         'scss': 'vue-style-loader!css-loader!postcss-loader!sass-loader',
  224.                     }
  225.                 }
  226.             },
  227.             {
  228.                 test: /\.js$/,
  229.                 loader: 'babel-loader',
  230.                 exclude: /node_modules/
  231.             }
  232.         ]
  233.     },
  234.     resolve: {
  235.         alias: {
  236.             vue$: 'vue/dist/vue.esm.js',
  237.             jquery: 'jquery/src/jquery'
  238.         },
  239.         modules: [
  240.             'node_modules'
  241.         ]
  242.     },
  243.     plugins: [
  244.         new FriendlyErrorsWebpackPlugin()
  245.     ],
  246.     devServer: {
  247.         historyApiFallback: true,
  248.         noInfo: true
  249.     },
  250.     performance: {
  251.         hints: false
  252.     },
  253.     devtool: '#source-map'
  254. };
  255.  
  256. if (process.env.NODE_ENV === 'production') {
  257.     module.exports.devtool = '#source-map';
  258.     module.exports.plugins = (module.exports.plugins || []).concat([
  259.         new webpack.DefinePlugin({
  260.             'process.env': {
  261.                 NODE_ENV: '"production"'
  262.             }
  263.         }),
  264.         new webpack.optimize.UglifyJsPlugin({
  265.             sourceMap: true,
  266.             compress: {
  267.                 warnings: false
  268.             }
  269.         }),
  270.         new webpack.LoaderOptionsPlugin({
  271.             minimize: true
  272.         })
  273.     ])
  274. }
  275.  
  276. // package.json
  277.   "scripts": {
  278.     "dev": "cross-env NODE_ENV=development gulp && gulp watch",
  279.     "build": "cross-env NODE_ENV=production gulp",
  280.     "deploy": "yarn run build && gulp deploy"
  281.   },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement