Tartalomjegyzék

< Gulp

Gulp - Szerver indítása

Szerver feladat

A child_process használatához nem kell telepíteni semmit.

...
const exec = require('child_process').exec;
 
...
function server() {
    exec('npm start', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        cb(err);
    });
}
...
 
exports.server = server;

Teljes gulpfile.js

gulpfile.js
const { src, dest, series } = require('gulp');
const exec = require('child_process').exec;
const cleanCss = require('gulp-clean-css');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
 
function createHTML(cb) {
    src(['src/*.html'])
    .pipe(dest('public'));
    cb();
}
 
function createCSS(cb) {
    src(['src/css/*.css'])
    .pipe(cleanCss())
    .pipe(rename({extname: ".min.css"}))
    .pipe(dest('public/css'));
    cb();
}
 
function createJS(cb) {
    src(['src/js/*.js'])
    .pipe(uglify())
    .pipe(rename({extname: ".min.js"}))
    .pipe(dest('public/js'));
    cb();
}
 
function server() {
    exec('npm start', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        cb(err);
    });
}
 
exports.html = createHTML;
exports.css = createCSS;
exports.js = createJS;
exports.server = server;
exports.default = series(createHTML, createCSS, createJS, server);

A lite-server beállítása

bs-config.json
{
    "server": ["src"],
    "port": 8000
}
package.json
{
...
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "lite-server"
  },
... 
}