Skip to content

Instantly share code, notes, and snippets.

@claudia-romano
Last active December 29, 2022 09:33
Embed
What would you like to do?
CodyFrame gulp config file for a PHP project
var gulp = require('gulp');
var sass = require('gulp-sass')(require('sass-embedded'));
var sassGlob = require('gulp-sass-glob');
var browserSync = require('browser-sync');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var connect = require('gulp-connect-php');
var projectPath = 'localhost:8888/projectName'; // 👈 make sure to replace 'projectName' with the name of your project folder
var purgecss = require('gulp-purgecss');
// js file paths
var utilJsPath = 'main/assets/js';
var componentsJsPath = 'main/assets/js/components/*.js'; // component js files
var scriptsJsPath = 'main/assets/js'; //folder for final scripts.js/scripts.min.js files
// css file paths
var cssFolder = 'main/assets/css'; // folder for final style.css/style-fallback.css files
var scssFilesPath = 'main/assets/css/**/*.scss'; // scss files to watch
function reload(done) {
browserSync.reload({notify: false});
done();
}
gulp.task('sass', function() {
return gulp.src(scssFilesPath)
.pipe(sassGlob())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(postcss([autoprefixer()]))
.pipe(gulp.dest(cssFolder))
.pipe(browserSync.reload({
stream: true,
notify: false
}));
});
gulp.task('scripts', function() {
return gulp.src([utilJsPath+'/util.js', componentsJsPath])
.pipe(concat('scripts.js'))
.pipe(gulp.dest(scriptsJsPath))
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(gulp.dest(scriptsJsPath))
.pipe(browserSync.reload({
stream: true,
notify: false
}));
});
gulp.task('watch', gulp.series(['sass', 'scripts'], function () {
connect.server({}, function (){
browserSync({
proxy: projectPath, // 👈 this contains the name of your project folder
notify: false
});
});
gulp.watch('**/*.php', gulp.series(reload));
gulp.watch('main/assets/css/**/*.scss', gulp.series(['sass']));
gulp.watch(componentsJsPath, gulp.series(['scripts']));
}));
/* Gulp dist task */
// create a distribution folder for production
var distFolder = 'dist/';
var assetsFolder = 'dist/assets/';
gulp.task('dist', async function(){
// remove unused classes from the style.css file with PurgeCSS and copy it to the dist folder
await purgeCSS();
// minify the scripts.js file and copy it to the dist folder
await minifyJs();
// copy any additional js files to the dist folder
await moveJS();
// copy all the assets inside main/assets/img folder to the dist folder
await moveAssets();
// copy all html files inside main folder to the dist folder
await moveContent();
console.log('Distribution task completed!')
});
function purgeCSS() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(cssFolder+'/style.css')
.pipe(purgecss({
content: ['main/*.php', scriptsJsPath+'/scripts.js'],
safelist: {
standard: ['.is-hidden', '.is-visible'],
deep: [/class$/],
greedy: []
},
defaultExtractor: content => content.match(/[\w-/:%@]+(?<!:)/g) || []
}))
.pipe(gulp.dest(distFolder+'/assets/css'));
stream.on('finish', function() {
resolve();
});
});
};
function minifyJs() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(scriptsJsPath+'/scripts.js')
.pipe(uglify())
.pipe(gulp.dest(distFolder+'/assets/js'));
stream.on('finish', function() {
resolve();
});
});
};
function moveJS() {
return new Promise(function(resolve, reject) {
var stream = gulp.src([scriptsJsPath+'/*.js', '!'+scriptsJsPath+'/scripts.js', '!'+scriptsJsPath+'/scripts.min.js'], { allowEmpty: true })
.pipe(gulp.dest(assetsFolder+'js'));
stream.on('finish', function() {
resolve();
});
});
};
function moveAssets() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(['main/assets/img/**'], { allowEmpty: true })
.pipe(gulp.dest(assetsFolder+'img'));
stream.on('finish', function() {
resolve();
});
});
};
function moveContent() {
return new Promise(function(resolve, reject) {
var stream = gulp.src('main/*.php')
.pipe(gulp.dest(distFolder));
stream.on('finish', function() {
resolve();
});
});
};
@jfcog
Copy link

jfcog commented Jun 14, 2022

Hi Claudia,

On the file paths you are mostly starting with 'asset' instead of 'main' this causes gulp to look for the scss in another folder and then it won't compile. Below the lines that need correction plus line 61.

Regards,
Jose

// js file paths
var utilJsPath = 'main/assets/js';
var componentsJsPath = 'assets/js/components/.js'; // component js files
var scriptsJsPath = 'assets/js'; //folder for final scripts.js/scripts.min.js files
// css file paths
var cssFolder = 'assets/css'; // folder for final style.css/style-fallback.css files
var scssFilesPath = 'assets/css/**/
.scss'; // scss files to watch

@claudia-romano
Copy link
Author

Hi Jose, thanks for the heads-up! It should be fixed now.

@391P
Copy link

391P commented Dec 14, 2022

Hello Claudia.
I have been using Codyhouse's code for some time now. Granted that it is a bit difficult for beginners to start everything with the terminal (eg installation of homebrew, npm, ...) however, you can learn the basics here. Now I move from my HTML projects to a PHP project. Unfortunately I'm facing a problem again.

  1. After my numerous failures, I think that here in the example line 17 instead of:

var scriptsJsPath = '/main/assets/js'; //folder for final scripts.js/scripts.min.js files
should read as shown here (without / befor the main):
var scriptsJsPath = 'main/assets/js'; //folder for final scripts.js/scripts.min.js files

  1. Could you please give an example of the project folder to enter in the before projectPath? Maybe based on my structure:

/Users/david/HTML/New_Project/codyhouse-php

Below this is the Codyhouse Framework structure:
/Users/david/HTML/New_Project/codyhouse-php/main
/Users/david/HTML/New_Project/codyhouse-php/node_modules
/Users/david/HTML/New_Project/codyhouse-php/gulpfile.js
/Users/david/HTML/New_Project/codyhouse-php/packages.json
/Users/david/HTML/New_Project/codyhouse-php/packages-lock.json
/Users/david/HTML/New_Project/codyhouse-php/LICENSE.md
/Users/david/HTML/New_Project/codyhouse-php/README.md

What specifically needs to be entered in the path (Path and Port)? If I follow all the steps in the instructions and remove the "/" the process stops after the [broswersync] Access URLs: step and nothing else happens. The browser shows nothing, the user interface under port 3001 is accessible. I have tried different inputs but all without success. It would be great to get support here, because not all of your users have the know-how to work out the result themselves.

@claudia-romano
Copy link
Author

claudia-romano commented Dec 15, 2022

Hi there, if you are using PHP for your project, then I assume you are using some kind of local server (e.g., you are using MAMP or similar). If you do, then those tools give you a default localhost url you can use to view your project. That's what you should use as projectPath. Does that make sense?

@391P
Copy link

391P commented Dec 19, 2022

Hello Claudia. Thanks for the feedback!
Now it works for me with Codyhouse and the implementation in PHP. I would have one wish for the future. Can you give a hint in advance in your documentation in the tutorial, which pre-condition is necessary to proceed with the step-by-step description you gave. The hint in the documentation at (https://codyhouse.co/ds/docs/framework) would have been a good improvement and would have saved the need to ask about the projectPath. There are certainly other points where these infomrations are useful.

@claudia-romano
Copy link
Author

Hi there, great to hear it works.
Could you clarify which info you think are missing in the PHP section? Thank you!

@391P
Copy link

391P commented Dec 28, 2022

Hi Claudia. Maybe something like this?

If you want to extend your PHP project with our Codyhouse Framework, then use the following instructions. Required is a webserver (e.g. MAMP or XAMPP) to be able to use all necessary PHP functions local. The projectPath you use in the browser (e.g. localhost/myproject), you need to add to the gulp config.

@claudia-romano
Copy link
Author

We'll look into it! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment