简体   繁体   中英

Laravel elixir copy not working on symbolic links

I'm creating a modular Laravel 5.2 app, which consists of a number of proprietary packages loaded on a Laravel installation using composer.

Each of the packages are responsible of managing its own assets (copy to public folder, compress, versioning, etc). To accomplish this, I have a gulpfile that uses elixir for each package, and then, they are loaded on the main gulpfile of the laravel installation.

This is the main gulpfile.js on the laravel installation:

var filesystem = require("fs");
var gulp = require('gulp');
var elixir = require('laravel-elixir');

gulp.task('default', function() {
    runPackagesGulpFiles();
});

function runPackagesGulpFiles() {
    var packagesPath = 'vendor/my-organization/';
    filesystem.readdirSync(packagesPath).forEach(function (file){
        try {
            var gulpFilePath = packagesPath + file + '/resources/assets/gulpfile.js';
            var fileStat = filesystem.statSync(gulpFilePath);
            require('./' + gulpFilePath);
        } catch (error) {
            if (error.message.indexOf('no such file or directory') < 0) {
                console.error(error.stack);
            }
        }
    });
}

All the main gulpfile.js does, is execute the packages gulpfiles if they exist using a require() function.

The following is an example of a package gulpfile:

var elixir = require('laravel-elixir');

elixir(function(mix) {
    mix.copy('vendor/my-organization/store-manager/resources/assets/js/', 'public/assets/js/elixir_components/store-manager/');
    mix.version([
        //Store structure versioning
        'assets/js/elixir_components/store-manager/stores/store.js',
        'assets/js/elixir_components/store-manager/stores/views.js',
        'assets/js/elixir_components/store-manager/stores/models.js',
        'assets/js/elixir_components/store-manager/stores/controllers.js',
        //Store resources versioning
        'assets/js/elixir_components/store-manager/resources/resources.js',
        'assets/js/elixir_components/store-manager/resources/views.js',
        'assets/js/elixir_components/store-manager/resources/models.js',
        'assets/js/elixir_components/store-manager/resources/controllers.js',
        //Store structure resources versioning
        'assets/js/elixir_components/store-manager/structures/structure.js',
        'assets/js/elixir_components/store-manager/structures/views.js',
        'assets/js/elixir_components/store-manager/structures/models.js',
        'assets/js/elixir_components/store-manager/structures/controllers.js',
    ]);
});

In a normal case scenario this works just fine. A normal case scenario being that the packages are loaded using composer.

However, for the development of the packages, I create a symbolic link in the vendor folder that points to the package folder in my local machine.

When I try to execute gulp in the development environment, I get a Cannot find module 'laravel-elixir' error:

[22:27:03] Using gulpfile ~/supermarket-cms/gulpfile.js
[22:27:03] Starting 'default'...
Error: Cannot find module 'laravel-elixir'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/home/vagrant/Code/store-manager/resources/assets/gulpfile.js:1:76)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
[22:27:03] Finished 'default' after 12 ms

Problem that I solved by installing laravel-elixir globally. But after I do so, the gulp task ends and my assets are not being copied.

[21:25:02] Using gulpfile ~/supermarket-cms/gulpfile.js
[21:25:02] Starting 'default'...
[21:25:02] Finished 'default' after 3.78 ms

No error whatsoever appears. Hope someone can help me. Thank you.

I think you're going about this the wrong way. This requires a level of knowledge about the workings of each vendor package, which goes against the purpose of package managers.

Instead, you should be allowing all of the assets from your vendor package to be published.

You'll start by making sure that in your boot method you publish your assets accordingly:

$this->publishes([
    __DIR__.'/path/to/assets' => resource_path('vendor/my-organization/'.$package_name),
], 'public');

//make sure to replace '/path/to/assets' with your packages asset path.

Then when executing

php artisan vendor:publish

Now all of your JavaScript and CSS will be pushed into it's respective location in your resource_path

resources/
    vendor/
        my-organization/
            <package_name>
                js/

Now just have your Gulp task watch concatenate and load all scripts from within /my-orginazation/**/js . So now you can just do something like this:

elixir(function(mix){
    'vendor/my-organization/**/js/*.js',
}, 'public/js/outputfile.js, 'resources');

这样做,在您说口吃之前,请安装npm install --global laravel-elixir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM