Search code examples
symfonynginxgulpsymlink

Homestead nginx serve content from symlink generated via gulpfile and vinyl-fs


For my symfony project I manage my frontend assets using the following gulpfile.js:

var gulp = require('gulp');
var rename = require('gulp-rename');
var vfs = require('vinyl-fs');

/**
* Location where to store the 3rd party libraries
*/
var frontend_folder="./frontend"
var vendor_folder=`${frontend_folder}/vendor`
var frontent_dev_folder_js=`${frontend_folder}/js`
var frontent_dev_folder_js=`${frontend_folder}/css`

/*################################### Installing Dependencies ###############################*/

//Move Bootstrap
gulp.task('move_bootstrap',function(done){

  var bootstrap_dir='./node_modules/bootstrap/dist';
  var dest=`${vendor_folder}/bootstrap`;

  var css_files=[`${bootstrap_dir}/css/bootstrap.min.css`,`${bootstrap_dir}/css/bootstrap.min.css.map`];
  var js_files=[`${bootstrap_dir}/js/bootstrap.bundle.min.js`,`${bootstrap_dir}/js/bootstrap.bundle.min.js.map`];

  gulp.src(css_files).pipe(gulp.dest(`${dest}/css`));
  gulp.src(js_files).pipe(gulp.dest(`${dest}/js`));

  done();
})

//Jquery & miscellanous Javascript move
gulp.task('move_jquery',function(done){
  var jqueryFiles=['./node_modules/jquery/dist/jquery.min.js'];
  gulp.src(jqueryFiles).pipe(gulp.dest(vendor_folder));

  done();
});

//For fontawesome
gulp.task('move_fontawesome',function(done){
  var path='./node_modules/font-awesome';
  var dest=vendor_folder+'/font-awesome';

  gulp.src(path+'/fonts/*').pipe(gulp.dest(dest+'/fonts'));
  gulp.src(path+'/css/font-awesome.min.css').pipe(gulp.dest(dest+'/css'));
  done();
});


/******* Build Final Steps ****************************************************/

gulp.task('link_assets',function(done){

  vfs.src(frontend_folder+'/*', {followSymlinks: false})
        .pipe(vfs.symlink('./web/assets/'));

  return done();
});

/* ############################################ Installing Dependencies ##################################### */

gulp.task('move_frontend', gulp.series(['move_bootstrap','move_jquery','move_fontawesome'],(done)=>{done()}));
gulp.task('dev',gulp.series(['move_frontend','link_assets'],(done)=>{done();}));

gulp.task('default',gulp.series(['dev'],(done)=>{done()}));

There is a task that generates a symlinks fro any content that exists in a folder, located in my project root named frontend into a folder into the ./web/assets. In other words my project root is:

-
-- frontend
...
-- web
---assets
---- ^any folder located in frontedn symlinked here^

Furthermore I place the following configuration for assets in config.yml:

framework:
...
    assets:
        packages:
            vendor:
                base_path: '/assets/vendor'

And I try to make my nginx to serve the symlinked content (used for development purpoces) for now I tried to place the following settings (based on nginx documentation ):

location ~ \/web\/assets {
    disable_symlinks off;
    root /home/vagrant/code/web/assets;
}

But when I visit an asset for example http://192.168.10.10/assets/vendor/bootstrap/css/bootstrap.min.css I get error 404. For development I use the the homestead vagrant image and i run the gulp tasks outside the vagrant.


Solution

  • For development I use the the homestead vagrant image and i run the gulp tasks outside the vagrant.

    That is the problem! The symlinks outside of vagrant are being mapped using full paths, in other words your symlink is pointing to a non-existed folder so the nginx when follows the symlink visits a non-existent file and as expected serves no content.

    So I would suggest visiting via ssh the running vm:

    vagrant ssh
    

    Then go to folder ~/code

    cd ~/code
    

    Remove the symlink

    rm -rf ./web/assets/*
    

    And rerun the gulp tasks

    gulp
    

    That will fix any problem you may have.