I'm new with gulp and I hope I explain myself clearly.
I'm using gulp's browser-sync with MAMP to create a WordPress theme. When browser-sync is executed through gulp task, my WordPress site opens under localhost:3000/mytheme but without images because images are inside localhost:8888... What I'm doing wrong?
gulpfile.js is inside my theme folder:
htdocs/wordpress/wp-content/themes/mytheme/gulpfile.js
MAMP settings,
Apache port: 8888 / Nginx port: 8888 / MySQL port: 8889
gulpfile.js
var gulp = require('gulp'),
watch = require('gulp-watch'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer'),
cssvars = require('postcss-simple-vars'),
nested = require('postcss-nested'),
cssImport = require('postcss-import'),
browserSync = require('browser-sync').create();
gulp.task('style', function(){
return gulp.src('./modules/style/style.css')
.pipe(postcss([cssImport, cssvars, nested, autoprefixer]))
.pipe(gulp.dest('./'));
});
gulp.task('browser-sync', function () {
var files = [
'./*.php',
'./style.css'
];
browserSync.init(files, {
proxy: "localhost:8888/my-theme",
});
});
gulp.task('watch', function(){
watch('./modules/style/**/*.css', function(){
gulp.start('style');
});
});
gulp.task('default', ['style', 'browser-sync', 'watch'], function(){
gulp.watch('./modules/style/**//**.css', ['watch']);
});
Now I can run Mamp/Wordpress with Gulp and here is how I've done...
Firstly I setted up a vhost with Mamp. These tutorials explain well the steps.
http://www.dennisdeacon.com/web-design/virtual-hosts-for-mamp-based-local-web-development-on-macos-sierra/
https://www.taniarascia.com/setting-up-virtual-hosts/
After setting up a virtual host, I could access to my local site via http://localhost/your-theme/ and http://my-theme.test/.
Next, I installed npm and gulp inside a WP theme folder (Node.js is installed globally) as I mentioned in my question. But I'm not sure if it's better to install these tools inside the htdocs folder or inside a WP theme folder. I did inside a theme folder because it seems easier for me.
To run the browser-sync, I used vhost url for proxy.
After this step, I could access to my site via http://localhost:3000/ with browser-sync.
To set up a vhost, I had to prepare a new WP environment with a new database inside Mamp.
Once a site is opend via localhost:8888, I could not reopen via vhost url. The site is redirected to the localhost:8888 url.
Now I can access to my site (front page) via http://localhost:3000/, but once I go to another pages, my site is redirected to http://localhost/your-theme/another-page and not http://localhost:3000/another-page. That means I can't run browser-sync task outside the front page and I don't know what is the solution...
Related
I’m building a website with Next.js and I need to add a page called "Index" under xyz.com/index
It works fine locally but when I deploy the website to vercel, /index shows the home page which is located under xyz.com/
Is there a way to make this work through rewrites or configuration on vercel?
Would you try to create a folder named Index, then under Index folder create js file called index.js.
You can see this image--
As of now (next 12.0.3) the solution that works for me is using a rewrite:
Name the page something like "indexPage":
/pages/indexPage.js
Rewrite /index to /indexPage:
In next.config.js:
module.exports = {
// …
async rewrites() {
return [
{
source: '/index',
destination: '/indexPage',
},
]
},
// …
}
Using the following folder structure only worked locally for me but not when deployed to vercel:
/pages/index/index.js
I want to create directory called 'app' in my public directory and make it root of my application instead of 'public' dir. What should I do except adding:
"extra": {
"public-dir": "public/app/",
...
}
I got a vue app that should be placed in 'app' directory and a wordpress instance that will be placed in 'public' directory so when I enter www.mywebsite.com I get wordpress website, and when I enter www.mywebsite.com/app I got my symfony/vue app.
So far I edited my webpack.config.js with:
.setOutputPath("public/app/build/")
.setPublicPath("/build/")
And it think it works on dev env but in prod env when i hit www.mywebsite.com/app I got:
<script src="/build/app.js"></script>
Which wrongly points to: www.mywebsite.com/build/app.js instead of www.mywebsite.com/app/build/app.js
But whenever I change webpack.config.js to:
.setOutputPath("public/app/build/")
.setPublicPath("/app/build/")
It works on production but not on dev env.
What am I doing wrong here?
Going by your problem description I'm assuming your application is accessed at / in your dev environment, but /app in your prod environment as explained. So you have to take that into account when calling .setConfigPath() by checking the environment you are running in your webpack config:
.setPublicPath(Encore.isProduction() ? '/app/build' : '/build')
I'm following a basic tutorial for Gulp from here and I got stuck on the browser-sync implementation. I have copied the code exactly as it is in the example, however when I run the watch task, even though sass task executes no problem (new version of the css file is created), the browser doesn't want to refresh! Here is my code:
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
gulp.task('sass', function() {
return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss
.pipe(sass())
.pipe(gulp.dest('app/css'))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('browserSync', ['sass'], function() {
browserSync.init({
server: {
baseDir: 'app'
},
})
})
gulp.task('watch', ['browserSync'], function() {
gulp.watch('app/scss/**/*.scss', ['sass']);
// Other watchers
});
And here is my index.html
<!doctype html>
<html>
<head>
<title>test</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>
My styles.scss files has only one style:
body {
background: blue;
}
And here is the output from the console window when I update the styles.scss
So I can see that the browser-sync is aware of the change being made, but it doesn't reload the page in the browser. Why is that? I have looked through the comments under the tutorial and no one seemed to face similar issue. I have also seen couple of other tutorials, but none of them works for me. I use latest version of chrome as my default browser, browser-sync version 5.6.0 and gulp version 3.9.1
I've never heard of piping files through browserSync.reload as a way to reload the dev server, and couldn't find any examples of that particular technique in the browserSync documentation. That doesn't necessarily mean that the method is invalid, but maybe the API has changed since CSS-Tricks published their tutorial in 2015? Three years can be an eternity in tooling time.
Anyway, I know I've used Gulp/browserSync/SASS successfully before so I dug up the code relevant to your situation from one of my old gulpfiles.
//watch task
gulp.task('watch', function () {
//spin up dev server
browserSync.init({
server: {
baseDir: "./"
},
});
//when scss files change, run sass task first and reload browserSync second
gulp.watch('./sass/*.scss', ['sass']).on('change', function () {
browserSync.reload();
});
});
//call watch task
gulp.task('default', ['watch']);
Basically when you call the watch task it immediately spins up the development server and then listens for changes to your scss files. When a change is detected, it calls the sass task as a dependency and runs whatever code you have for preprocessing (which you said is currently working in your file). After the sass task is completed, browserSync.reload is called via the .on method.
I haven't used this particular configuration in awhile but let me know if it works and if not I'd be happy to troubleshoot it with you. It's good boilerplate for any dev to have on hand.
EDIT: The above snippet was taken from a much larger gulpfile and upon second inspection I identified some parts that prevented it from working in a standalone context. Edited snippet to correct this.
I am running a MAMP site and this worked for me
//define task
gulp.task('bsync', function () {
//spin up dev server
browserSync.init({
proxy: "dev.sitename.local",
hostname: "dev.sitename.local",
port: 3000, //even if apache is running on 80 or something else
});
//when css files change, reload browserSync
gulp.watch('./css/*.css').on('change', function () {
browserSync.reload();
});
});
//call task with 'gulp runbsync'
gulp.task('runbsync', ['bsync']);
https://gist.github.com/petergus/31d75a5145e062d4eaa42eb04ce23aea
I've built a gulp project that allows me to build and watch a wordpress theme (bones) in a wordpress installation that is outside the working folder:
Working folder: localhost/sites/testbed/
- dev-bones
- node_modules
- gulpfile.js
- package.json
- package-lock.json
Target folder: localhost/sites/my-wordpress-site/wp-content/themes/
- bones
I installed 'del' and 'run-sequence' so that I could delete the target folder before the build sequence began.
del task:
gulp.task('clean:dirbuild', function() {
// If build directory is outside the working folder:
return del.sync(dir.build, {force: true});
// If inside:
// return del.sync(dir.build);
});
build task:
gulp.task('build', function (callback) {
runSequence('clean:dirbuild',
['php', 'css', 'js', 'copyroot', 'copytranslation'],
callback
)
});
default task:
gulp.task('default', ['build', 'watch']);
After typing 'gulp watch' the browser launches and the wordpress site loads. But I get a white screen with a message saying that the path to bones.php doesn't exist.
I checked the target folder and it does exist.
After scratching my head for a while I manually refreshed the browser and the site loaded correctly.
I've gone through a number of permutations of the code above, always with the same result.
I found this post here on Stack Overflow : run-sequence doesn't run gulp tasks in order, but it didn't seem to fit my case.
I've uploaded the full gulpfile to Github.
I'd be very grateful if someone could give it the once-over and see where I'm going wrong.
UPDATE
I went back to a tutorial I got the 'del' task from. In the comments the author had pointed out that he'd missed something from the task: the callback.
So I updated the code:
gulp.task('clean:dirbuild', function(callback) {
// If build directory is outside the working folder:
return del.sync(dir.build, {force: true}, callback);
// If inside:
// return del.sync(dir.build, callback);
});
and typed 'gulp'.
This time all I got was a white screen and manually refreshing didn't load the site.
OK I figured it out. The problem was with the order of execution of the 'default' task: some of the 'watch' tasks where firing before, during and after the 'build' task.
So I left everything as it was except I changed:
default task
gulp.task('default', ['build', 'watch']);
to
gulp.task('default', function (callback) {
runSequence('build', ['watch'], callback)
});
which ensures that the 'watch' task will run after the 'build' task.
Now, everything works as expected.
Note: I've deleted the example gulpfile.js from GitHub. When I've completed my project and got it on GitHub I'll post a link to it from here.
I'm trying to use gruntjs livereload with a wordpress theme development.
For WP theme development I would normally use MAMP and view the site at localhost in the browser.
If I'm using grunt to create a server do I still need MAMP running.
Do I still need the wordpress folder in the MAMP root folder to connect to the database.
I'm using this simple gruntfile.js that is in the root of the wordpress folder but if I run grunt the browser loads but loads a search engine searching for 0.0.0.0
'use strict';
module.exports = function(grunt){
require('load-grunt-tasks')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
connect: {
options: {
port: 9000,
},
livereload: {
options: {
open: true,
}
}
}
});
grunt.registerTask('default', ['connect']);
}
Is it possible to use grunt livereload with Wordpress without using MAMP.
I've been trying to work this out all day - any help would be greatly appreicated.
Hey so I was also running into this issue and I couldn't get it to work? I feel it has something to do with the fact that MAMP runs your PHP server on a different port. Perhaps you can make your default to port 35729 because that's where the script lives
A really hackish workaround I did was I created a little PHP script that inserts itself when on localhost. I also check to make sure a variable is declared.
livereload.php
<?php
// Live reload script added only on localhost and if $livereload set to true
// add specific whitelist options
$whitelist = array(
'127.0.0.1',
'::1',
'localhost'
);
if(in_array($_SERVER['REMOTE_ADDR'], $whitelist) && isset($livereload)){
echo '<script src="http://localhost:35729/livereload.js?snipver=1"></script>';
}
?>
In my main PHP file, I would do something like
index.php
<?php
$livereload = true;
?>
// Markup, content
<?php
include_once('livereload.php');
?>
Hope this helps.
I'm not sure I understood your problem completely, but you won't be able to run wordpress on "grunt" server, cause node server is in JS and does not support PHP nor mysql (out of the box at least), i can help you to have a working mamp + watch setup
so to have a proper setup including mamp and grunt taking your example as a basis
'use strict';
module.exports = function(grunt){
require('load-grunt-tasks')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
options: {
livereload: true,
},
files: {
files: ['theDirYouWantToReload/*.scss'], // or whaterver globbing pattern you would need
tasks: ['yourNeededTask'],
},
}
});
grunt.registerTask('default', ['watch']);
}
you should have a look at grunt-watch-repo for additional information on how watch and reload works.
this was the grunt part, on the WP side you should add the livereload script to your pages
2 solution here, use the browser's extension of LR, and activate it on the browser when you look at these pages, or add the live reload script to WP enqueue
the default address of the script is http://localhost:35729/livereload.js?snipver=1
this is a barebone example, you can fine grain actions on the watch task.