Skip to content

Commit a466c7c

Browse files
authored
Merge pull request #10 from CottaCush/feature/node-gulp-flow
Node & Gulp flow
2 parents f452a4d + cbeaeb7 commit a466c7c

File tree

5 files changed

+4228
-1
lines changed

5 files changed

+4228
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ RoboFile.php
3939
build/logs
4040
build/pdepend
4141
build/coverage
42-
build/api
42+
build/api
43+
44+
# Node.js
45+
node_modules

gulpfile-config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Import Node.js modules */
2+
var environments = require('gulp-environments'),
3+
autoprefixer = require('autoprefixer');
4+
5+
6+
var config = {
7+
sourceDir: "./app/web",
8+
buildDir: "./app/web",
9+
styles: {
10+
sourceDir: "./app/web/less",
11+
sourceFiles: "./app/web/less/**/*.less",
12+
destinationDir: "./app/web/css",
13+
mapsDir: "./maps", // relative to the destination directory
14+
postcss: [
15+
autoprefixer({browsers: ["last 5 versions", "> .5% in NG", "not ie < 11"]})
16+
]
17+
},
18+
scripts: {
19+
sourceDir: "./app/web/js",
20+
sourceFiles: ["./app/web/js/**/*.js"],
21+
destinationDir: "./app/web/js"
22+
},
23+
images: {
24+
sourceDir: "./app/web/img",
25+
sourceFiles: "./app/web/img/**/*",
26+
destinationDir: "./app/web/img"
27+
}
28+
};
29+
30+
/* Add sourcemaps on all environments except production */
31+
config.sourcemaps = !(environments.production());
32+
33+
/* Minify build files on all environments except development */
34+
config.minify = !(environments.development());
35+
36+
37+
module.exports = config;

gulpfile.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/* Import our Node.js modules */
2+
const gulp = require('gulp'),
3+
cssmin = require('gulp-cssmin'),
4+
environments = require('gulp-environments'),
5+
gulpif = require('gulp-if'),
6+
imagemin = require('gulp-imagemin'),
7+
less = require('gulp-less'),
8+
sass = require('gulp-sass'),
9+
postcss = require('gulp-postcss'),
10+
rename = require('gulp-rename'),
11+
rimraf = require('rimraf'),
12+
sourcemaps = require('gulp-sourcemaps'),
13+
path = require('path'),
14+
config = require('./gulpfile-config');
15+
16+
17+
/* Declare our environments */
18+
const development = environments.development,
19+
staging = environments.make('staging'),
20+
production = environments.production;
21+
22+
/* Set gulp.series and gulp.parallel to constants for convenience sake */
23+
const series = gulp.series,
24+
parallel = gulp.parallel;
25+
26+
/* Extract some config properties for convenience */
27+
const shouldAddSourcemaps = config.sourcemaps,
28+
shouldMinify = config.minify;
29+
30+
31+
/* Declare our gulp tasks */
32+
gulp.task('build', parallel(series(styles, minifyStyles), images));
33+
gulp.task('default', series('build', watch));
34+
35+
/* Describe our gulp tasks */
36+
gulp.task('build').description = 'Clean out the build folder then compile styles, minify images, and copy assets into the build folder';
37+
gulp.task('default').description = 'Run the build task and watch for any changes';
38+
39+
40+
function clean(done) {
41+
rimraf(config.buildDir, done);
42+
}
43+
clean.description = 'Cleans the build folder';
44+
45+
46+
function styles() {
47+
let sConfig = config.styles,
48+
files = sConfig.sourceFiles,
49+
source = sConfig.sourceDir,
50+
dest = sConfig.destinationDir,
51+
mapsDir = sConfig.mapsDir,
52+
postcssConfig = sConfig.postcss;
53+
54+
return compileLess(source, dest, files, mapsDir, postcssConfig);
55+
}
56+
styles.description = 'Compiles SCSS files to CSS; adds source maps if specified';
57+
58+
59+
function minifyStyles(done) {
60+
if (shouldMinify) {
61+
let dir = config.styles.destinationDir;
62+
return minifyCSS(dir, dir, false);
63+
}
64+
done();
65+
}
66+
minifyStyles.description = 'Minify CSS files';
67+
68+
69+
function images() {
70+
let iConfig = config.images,
71+
sourceDir = iConfig.sourceDir,
72+
destDir = iConfig.destinationDir;
73+
74+
return minifyImages(sourceDir, destDir);
75+
}
76+
images.description = 'Minify images back into the same (source) folder';
77+
78+
79+
function watch(done) {
80+
if (development()) {
81+
gulp.watch(config.styles.sourceFiles, styles);
82+
}
83+
done();
84+
}
85+
watch.description = 'Watch relevant files and re-run their tasks (only in development environment)';
86+
87+
88+
/**
89+
* Generic function to compile LESS files
90+
* @param {String} sourceDir a string representing the path to the directory for the LESS files
91+
* @param {String} destDir a string representing the path to the directory where the compiled files would be saved
92+
* @param {String|Array} files string or array of strings representing the glob match for the source files
93+
* @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory)
94+
* @param {Array} postcssConfig an array of postcss processors
95+
* @returns {*} the gulp stream
96+
*/
97+
function compileLess(sourceDir, destDir, files, mapsDir, postcssConfig) {
98+
return compileStyles(sourceDir, destDir, files, mapsDir, less, postcssConfig);
99+
}
100+
101+
/**
102+
* Generic function to compile Sass files
103+
* @param {String} sourceDir a string representing the path to the directory for the Sass files
104+
* @param {String} destDir a string representing the path to the directory where the compiled files would be saved
105+
* @param {String|Array} files string or array of strings representing the glob match for the source files
106+
* @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory)
107+
* @param {Array} postcssConfig an array of postcss processors
108+
* @returns {*} the gulp stream
109+
*/
110+
function compileSass(sourceDir, destDir, files, mapsDir, postcssConfig) {
111+
return compileStyles(sourceDir, destDir, files, mapsDir, function () {
112+
return sass().on('error', sass.logError);
113+
}, postcssConfig);
114+
}
115+
116+
/**
117+
* A generic function to compile both LESS and Sass files
118+
* @param {String} sourceDir a string representing the path to the directory for the source files
119+
* @param {String} destDir a string representing the path to the directory where the compiled files would be saved
120+
* @param {String|Array} files string or array of strings representing the glob match for the source files
121+
* @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory)
122+
* @param {Function} buildFxn the build function to use. could be less or sass functions
123+
* @param {Array} postcssConfig an array of postcss processors
124+
*/
125+
function compileStyles(sourceDir, destDir, files, mapsDir, buildFxn, postcssConfig) {
126+
return gulp.src(files, {base: sourceDir})
127+
.pipe(gulpif(shouldAddSourcemaps, sourcemaps.init()))
128+
.pipe(buildFxn())
129+
.pipe(postcss(postcssConfig))
130+
.pipe(gulpif(shouldAddSourcemaps, sourcemaps.write(mapsDir)))
131+
.pipe(gulp.dest(destDir));
132+
}
133+
134+
/**
135+
* A generic function to copy files/directories to a different directory
136+
* @param {String|Array} source a string or array of string representing the glob match for the source files/folders
137+
* @param {String} destination the new directory to copy
138+
*/
139+
function copy(source, destination) {
140+
return gulp.src(source)
141+
.pipe(gulp.dest(destination));
142+
}
143+
144+
145+
/**
146+
* Generic function to minify CSS files
147+
* @param {string} sourceDir a string representing the path to the directory for the source files
148+
* @param {string} destDir a string representing the path to the directory where the minified files would be saved
149+
* @param {boolean} shouldRename determine whether the new file should be renamed or not
150+
* @returns {*} the gulp stream
151+
*/
152+
function minifyCSS(sourceDir, destDir, shouldRename) {
153+
let files = [path.join(sourceDir, '**/*.css'), path.join('!' + sourceDir, '**/*.min.css')];
154+
return minify(files, destDir, cssmin, shouldRename);
155+
}
156+
157+
/**
158+
* A generic function to minify images
159+
* @param {string} sourceDir a string representing the path to the directory for the source files
160+
* @param {string} destDir a string representing the path to the directory where the minified files would be saved
161+
* @returns {*} the gulp stream
162+
*/
163+
function minifyImages(sourceDir, destDir) {
164+
let files = path.join(sourceDir, '**/*');
165+
return minify(files, destDir, imagemin, false);
166+
}
167+
168+
/**
169+
* A generic function to minify different types of files
170+
* @param {string|array} source a string or array of strings representing the glob match for the source files/folders
171+
* @param {string} destDir a string representing the path to the directory where the minified files would be saved
172+
* @param {function} minifyFxn the minify function to use
173+
* @param {boolean} shouldRename determine whether the new file should be renamed or not
174+
*/
175+
function minify(source, destDir, minifyFxn, shouldRename) {
176+
if ('undefined' === typeof shouldRename) {
177+
shouldRename = true;
178+
}
179+
return gulp.src(source)
180+
.pipe(minifyFxn())
181+
.pipe(gulpif((shouldRename), rename({suffix: '.min'})))
182+
.pipe(gulp.dest(destDir));
183+
}

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "yii2-base-project",
3+
"version": "0.1.0",
4+
"description": "A Yii 2 Base Project Template",
5+
"main": "gulpfile.js",
6+
"scripts": {
7+
"start": "./node_modules/.bin/gulp",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/CottaCush/yii2-base-project.git"
13+
},
14+
"keywords": [
15+
"starter",
16+
"kit",
17+
"starter-kit",
18+
"front-end",
19+
"web",
20+
"development"
21+
],
22+
"author": "Ọlájídé Oyè <[email protected]>",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/CottaCush/yii2-base-project/issues"
26+
},
27+
"homepage": "https://github.com/CottaCush/yii2-base-project#readme",
28+
"dependencies": {
29+
"autoprefixer": "^6.7.7",
30+
"bower": "^1.8.0",
31+
"gulp": "github:gulpjs/gulp#4.0",
32+
"gulp-cli": "^1.2.2",
33+
"gulp-cssmin": "^0.1.7",
34+
"gulp-environments": "^0.1.2",
35+
"gulp-if": "^2.0.2",
36+
"gulp-imagemin": "^3.2.0",
37+
"gulp-less": "^3.3.0",
38+
"gulp-postcss": "^6.4.0",
39+
"gulp-rename": "^1.2.2",
40+
"gulp-sass": "^3.1.0",
41+
"gulp-sourcemaps": "^2.6.0",
42+
"path": "^0.12.7",
43+
"rimraf": "^2.6.1"
44+
}
45+
}

0 commit comments

Comments
 (0)