LHJ

I'm a FE developer.

4월 - gulpfile.babel.js - 11st

30 Apr 2020 » node_gulp
  • {
      "presets": [ "@babel/preset-env" ]
    }
    
  • {
      "browser": {
        "root": "../",
        "port" : 90,
        "livereload": true,
        "browser": "chrome"
      },
      "path": {
        "html": {
          "src": "../html/*.{html, php}"
        },
        "sass": {
          "src": "../sass/**/*.{scss, sass}",
          "dest": "../css/"
        },
        "sprites": {
          "src": "../img/order_v3/sprites/**/*.png",
          "dest": "../img/order_v3/sprites/*.png"
        }
      }
    }
    
  • // 엄격 모드
    "use strict";
      
    // 모듈 호출
    import {src, dest, series, parallel, watch} from 'gulp';
    import sass from 'gulp-sass';
    import spritesmith from 'gulp.spritesmith-multi';
    import postcss from 'gulp-postcss';
    import autoprefixer from 'autoprefixer';
    import imagemin from 'gulp-imagemin';
    import buffer from 'vinyl-buffer';
    import config from './config.json';
    import merge from 'merge-stream';
    import compressCss from 'gulp-clean-css';
    import del from 'del';
      
    // php 서버
    import phpConnect from 'gulp-connect-php';
    import browserSync from 'browser-sync';
      
    // 이미지 삭제
    const cleanImg = () => {
        return del(config.path.sprites.dest, { force: true })
    }
      
    const sprites = () => {
        const opts = {
            spritesmith: function (options, sprite, icons){
                options.imgName = `sp_${sprite}.png`;
                options.imgPath =  `../../img/order_v3/sprites/${options.imgName}`;
                options.cssName = `_sp_${sprite}.scss`;
                options.cssTemplate = './scss-minimal.handlebars';
                options.cssSpritesheetName = sprite;
                options.padding = 10;
                options.retinaSrcFilter = null;
                options.retinaImgName = null;
                options.retinaImgPath = null;
                options.cssVarMap = function(sp) {
                    sp.name = `sp_${sprite}_${sp.name}`;
                };
                return options;
            }
        };
      
        const spriteData = src(config.path.sprites.src)
            .pipe(spritesmith(opts))
            .on('error', function (err) {
                console.log(err)
            })
      
        const imgStream = spriteData.img
            .pipe(buffer())
            .pipe(imagemin())
            .pipe(dest("../img/order_v3/sprites/"));
        const cssStream = spriteData.css
            .pipe(dest('../sass/order/sprites/'));
        return merge(imgStream, cssStream);
    }
      
    const sassBuild = () => {
        return src(config.path.sass.src, { sourcemaps: true })
            .pipe(sass({
                outputStyle: 'compact'
            })).on('error', sass.logError)
            .pipe(postcss([autoprefixer()]))
            .pipe(compressCss({ format: 'keep-breaks' }))
            .pipe(dest(config.path.sass.dest, {sourcemaps: true}))
    }
      
    const browserSyncReload = (done) => {
        browserSync.reload();
        done();
    }
      
    // php 서버
    const phpServer = () => {
        phpConnect.server({
            // browsersync가 프록시를 통해 연결되는 독립형 PHP 서버
            // 웹 서버에 액세스하려는 포트입니다. 포트가 이미 사용 중이면 작업이 실패합니다.
            port: 80,
            keepalive: true,
            base: "../"
        }, function () {
            browserSync({
                // php 서버 포트번호와 동일해야됩니다.
                proxy: 'localhost:80'
            })
        })
        watch("../html/**/*.html", browserSyncReload)
        watch([
            config.path.sprites.src,
            `!${config.path.sprites.dest}`
        ], series(cleanImg, sprites, sassBuild, browserSyncReload))
        watch([
            "../sass/**/**/*",
            "!../sass/**/sprites/*"
        ], series(sassBuild, browserSyncReload))
    }
      
    exports.default = series(cleanImg, sprites, sassBuild, phpServer);
    exports.build = series(cleanImg, sprites, sassBuild);
    
  • {
      "name": "",
      "version": "0.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": ""
      },
      "author": "",
      "license": "MIT",
      "homepage": "",
      "devDependencies": {
        "@babel/core": "^7.9.0",
        "@babel/preset-env": "^7.9.5",
        "@babel/register": "^7.9.0",
        "autoprefixer": "^9.7.6",
        "browser-sync": "^2.26.7",
        "del": "^5.1.0",
        "gulp": "^4.0.2",
        "gulp-clean-css": "^4.3.0",
        "gulp-connect": "^5.7.0",
        "gulp-connect-php": "^1.0.3",
        "gulp-imagemin": "^7.1.0",
        "gulp-open": "^3.0.1",
        "gulp-postcss": "^8.0.0",
        "gulp-sass": "^4.1.0",
        "gulp.spritesmith-multi": "^3.1.0",
        "merge-stream": "^2.0.0",
        "node-sass": "^4.14.0",
        "vinyl-buffer": "^1.0.1"
      },
      "browserslist": [
        "last 2 versions",
        "ie >= 9",
        "iOS >= 6",
        "Android >= 4"
      ],
      "dependencies": {}
    }
    
  •   
    {
      // Default options
      'functions': false,
      'variableNameTransforms': ['underscored']
    }
      
    {{#extend "scss"}}
    {{#content "sprites"}}
    {{#each sprites}}
    ${{strings.name}}: ({{px.x}}, {{px.y}}, {{px.offset_x}}, {{px.offset_y}}, {{px.width}}, {{px.height}}, {{px.total_width}}, {{px.total_height}}, '{{{escaped_image}}}', '{{name}}', );
    {{/each}}
    {{/content}}
    {{#content "spritesheet"}}
    ${{spritesheet_info.strings.name_sprites}}: ({{#each sprites}}${{strings.name}}, {{/each}});
    ${{spritesheet_info.strings.name}}: ({{spritesheet.px.width}}, {{spritesheet.px.height}}, '{{{spritesheet.escaped_image}}}', ${{spritesheet_info.strings.name_sprites}}, );
    {{/content}}
    {{/extend}}