Stolz / Assets

An ultra-simple-to-use assets management library for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build local link - no directory

mvodanovic opened this issue · comments

In the buildLocalLink method there should be a check if the $dir variable is empty or not. If it is, the asset shouldn't be changed. Example:

if ($package === false)
    return empty($dir) ? $asset : $dir . '/' . $asset;

I build my links using Laravel's Elixir & gulp which are already ok when they come to this point (so the dir options in config are set to empty strings), but with how it is now, this function prepends another slash to the beginning and breaks everything.

commented

I'm afraid I'm not able to reproduce your problem. I quote from the docs:

Note all local assets filenames are considered to be relative to you assets directory (configurable via css_dir and js_dir options).

So if your css_dir and js_dir options are empty it means you have no assets directory and therefore all your assets are relative to your webroot. In that scenario, if you add do Assets::add('foo.css') the link the library will generate will be

<link href="/foo.css" type="text/css" rel="stylesheet" />

which is correct. It's true it prepends a slash but that doesn't break anything since it's pointing to an asset in your webroot and your config options say your assets directory is the webroot.

Can you please elaborate to help me to understand where the problem is?. For instance provide the full absolute path of one of the conflicting assets and your current library configuration options (public_dir, css_dir and js_dir )and I'll try to figure out what is the proper way to load that asset.

Try this:

Assets::add('/foo.css');

Laravel's elixir function already puts a slash at the beginning of my asset's URL any the buildLocalLink adds another.

commented

I'm not familiar with Laravel Elixir so I'm not able to help with that. If you provide the details I asked for I may be able to help

Well, I provided all the details you need. Look at my last example. The problem is the slash being prepended at the front which can't be disabled in any way. I also provided you with the exact line which needs to be fixed and with the exact code which fixes it.

If you want me to be a bit broader, sure. Here is my exact configuration.

So, this is from my gulpfile.js:

var elixir = require('laravel-elixir');
elixir(function (mix) {
    mix.scripts([
        'node_modules/angular/angular.js',
        'node_modules/angular-sanitize/angular-sanitize.js',
        'node_modules/jquery/dist/jquery.js',
        'node_modules/bootstrap/dist/js/bootstrap.js',
        'node_modules/d3/d3.js',
        'node_modules/d3-tip/index.js',
        'resources/assets/js/core/app.js',
        'resources/assets/js/core/common.js',
        'resources/assets/js/core/messages.js',
        'resources/assets/js/core/spinner.js',
        'resources/assets/js/core/urls.js',
        'resources/assets/js/navbar/navbar.js'
    ], 'public/js/base.js', __dirname);

    mix.version([
        'public/js/base.js'
    ]);
});

When I run gulp in the shell, Javascript files get compiled and versioned. The resulting file is stored, for example like this: /public/build/js/base-51dcaaf9.js.

And then, in PHP, when i call elixir('js/base.js'), the output is the whole path to the built file, so this: /build/js/base-51dcaaf9.js. And this is great, but then I need to manually write the script tag in html templates to use it and I don't want to do that. Instead, I want to do this: Assets::addJs(elixir('js/base.js')) and then in the template simply do {!! Assets::js() !!}, but I can't do this because another slash gets prepended to my asset.

I'm also a bit surprised you're not familiar with Elixir since you mention Laravel 5 in your documentation and Elixir is the default asset management system of Laravel. With the current implementation of this plugin I don't see a way of using it together with Elixir, and the change required for them to be compatible is a 1-liner, as I see it.

commented

This is framework agnostic package. The library is working as it is designed to work. You seem to have a problem with the elixir() function. I don't see the reason for adding a workaround for a framework specific function to a perfectly working framework agnostic library. The suggested change may break existing apps that load relative assets and don't use a framework or a routing library that provides clear urls.

Since the problem is the format of the string generated by the elixir() function, the solution is easy:

Instead of reworking the library to be compatible with the format of the string returned by the elixir() function (which the library is totally unaware of), what you have to do is convert the format of the string returned by the elixir() function to the format expected by the library.

A simple string search and replace should do the trick.

Ok, no matter, don't worry, I'll implement what I need myself.

commented

Unfortunately I've have to change the same line, so I build my own ServiceProvider to Override/Extend the method.