Gulp - Simple automate toolkit to grab files and compile CSS, SCSS, JavaScript or framework like non-standard JavaScript for browser.
Node Package Manager (NPM) - The source code of Gulp and other relative files and frameworks repository that need to use. package.json file - Keep track of all files. DNA explanation of a project.
Download and install - https://nodejs.org/en/
- Install Node Package Manager (NPM)
- After install check in CMD: node-v...-x.... from https://nodejs.org/en/
- Check in Command Prompt
- node -v for gulp
- npm -v for Node package Manager
- Create package.json file
- Create project folder and suppose named it 'Gulp'.
- In Command Prompt (D:\xapmm\htdocs\gulp>)
- npm init
- package name-> enter package name, basically folder name should be package name but you can put only enter (Space is not allowed).
- Fill up link, description, version.
- Entry point should be default value like index.js
- Test command, git repository, keyword, author, license (Simply press enter like blank) .
- Is this OK? Press enter.
- package.json file created in Gulp file.
- Install Gulp
- In Command Prompt inside the "Gulp folder"
- npm install --save-dev gulp -- Install gulp folder and update package.json file.
- After Install Gulp
- We get 'node_modules' folder and 'package-lock.json' file.
- Folder 'node_modules' is used only for the developers. In the local server 'node_moludes' folder is required when some changes going on the project. It's never uploaded on the remote server.
- But 'package-lock.json' file is required only when upload into the server, but folder 'node_modules' not required for remote server.
- Gulp/NPM version | Update
- E:\xampp\htdocs\Gulp>npm outdated
- Empty means all are updated.
- Only package.json file | Developer have only package.json file
- E:\xampp\htdocs\Gulp>npm install
- Inside package.json file we newly find "dependencies": { "gulp": "^4.0.2" }.
- Our custom gulp js file need to detect gulp module. So, add var gulp = require( 'gulp' ); in side custom gulp js file like gulpfile.js.
- 'gulp' is not recognized as an internal or external command
- npm install -g gulp-cli
- Install relevant modules | CMD | Inside Gulp file
- npm install --save-dev gulp-rename
- When you have only package.json file | When another developer have package.json file
- Ensure package.json file is inside in your project folder.
- In Command prompt -> npm install
- Get relevant files and folder for the project.
- Build Directory Structure | Sample architecture inside Gulp folder
- E:\xampp\htdocs\Gulp>mkdir src
- E:\xampp\htdocs\Gulp>mkdir dist
- E:\xampp\htdocs\Gulp\src>mkdir js
- E:\xampp\htdocs\Gulp\src>mkdir scss
- E:\xampp\htdocs\Gulp\dist>mkdir js
- E:\xampp\htdocs\Gulp\dist>mkdir css
- E:\xampp\htdocs\Gulp>touch gulpfile.js -- Custom js file.
- SCSS | gulpfile.js
- Install Scss module E:\xampp\htdocs\Gulp>npm install --save-dev gulp-sass
- Install autoprefixer module E:\xampp\htdocs\Gulp>npm install --save-dev gulp-autoprefixer
- Install source map module E:\xampp\htdocs\Gulp>npm install --save-dev gulp-sourcemaps
- require - This file interact with 'node_modules' folder. After install NPM and Gulp, inside gulpfile.js 'require' finds directly node_modules.
- gulp.task() - It's very inGulp just based on task function.
- gulp.task( 'name_of_the_task', function(){ // compile });
var gulp = require( 'gulp' ); var rename = require( 'gulp-rename' ); var sass = require( 'gulp-sass'); var autoprefixer= require( 'gulp-autoprefixer'); var sourcemaps = require( 'gulp-sourcemaps'); var styleSRC = './src/scss/style.scss'; var styleDIST = './dist/css/'; gulp.task('style', function(){ // compile gulp.src( styleSRC ) .pipe( sourcemaps.init()) .pipe( sass({ errorLogToConsole:true, outputStyle: 'compressed' }) ) .on( 'error', console.error.bind( console ) ) .pipe(autoprefixer({ cascade: false }) ) .pipe( rename( {suffix: '.min'} ) ) .pipe( sourcemaps.write( './' ) ) .pipe( gulp.dest( styleDIST ) ); return new Promise(function(resolve, reject) { console.log("HTTP Server Started"); resolve(); }); });
- Execute E:\xampp\htdocs\Gulp\dist>gulp style
- JS | gulpfile.js
- Browserify - Handles modules, capable to import files.
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev browserify
- Transform Babelify - Convert file with babelify
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev babelify
- E:\xampp\htdocs\Gulp\dist>npm install @babel-core --save-dev
- E:\xampp\htdocs\Gulp\dist>npm install @babel/preset-env --save-dev
- But var babelify = require( 'babelify' );
- Bundle all together
- Tap source code
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev vinyl-source-stream
- Rename
- Minified files
- Buffer
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev vinyl-buffer
- Initialize source map
- Uglify
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev gulp-uglify
- Write source map
- Save into dist folder
var jsSRC = 'src/js/script.js'; var jsDIST = './dist/js/'; var jsWATCH = 'src/js/**/*.js'; var jsFILES = [jsSRC]; gulp.task( 'js', function(){ jsFILES.map(function( entry ) { return browserify({ entries: [entry] }) .transform( babelify, {presets: ['env']}) .bundle() .pipe( source( entry ) ) .pipe( rename({ extname: '.min.js' }) ) .pipe( buffer() ) .pipe( sourcemaps.init({ loadMaps: true }) ) .pipe( uglify() ) .pipe( sourcemaps.write( './' ) ) .pipe( gulp.dest( jsDIST ) ) }); return new Promise(function(resolve, reject) { console.log("HTTP Server Started"); resolve(); }); });
//Simple js var jsSRC = 'src/js/script.js'; var jsDIST = './dist/js/'; var jsWATCH = 'src/js/**/*.js'; gulp.task( 'js', function(){ gulp.src( jsSRC ) .pipe( gulp.dest( jsDIST ) ); return new Promise(function(resolve, reject) { console.log("HTTP Server Started"); resolve(); }); });
- The default task | Execute all task together | Stage 1
- gulp.task('default', gulp.parallel('style', 'js' ) );
- Watch | Stage 2
- E:\xampp\htdocs\Gulp\dist>npm install --save-dev gulp-watch
- var watch = require( 'gulp-watch');
- We simultaneously changes our code, after every change use gulp or gulp style in CMD is very cumbersome. When we use watch module then every save gulp automatically update files.
- Execute E:\xampp\htdocs\Gulp\dist>gulp watch
- Want exit from running watch press ctrl+C in CMD.
gulp.task('watch', function() { gulp.watch(styleWATCH, gulp.series('style')), gulp.watch(jsWATCH, gulp.series('js')) });
- When creating a file Gulp doesn't automatically detect the newly created file. Only simple change can solve this problem. Remove a first dot with back shah from the path.
var jsDIST = './dist/js/'; -> var jsDIST = '/dist/js/';js'; - Browser-Sync Auto Reload HTTPS
- E:\xampp\htdocs\Gulp\dist>npm install browser--sync --save-dev
- Install Google code prettify
- E:\xampp\htdocs\Gulp>npm install code-prettify --save
No comments:
Post a Comment