Giter Club home page Giter Club logo

Comments (6)

Stolz avatar Stolz commented on May 18, 2024

@fisharebest, for those cases what you need is an assets filter/preprocessor. The library doesn't include a built in one but it offers a way to provide your custom one.

Simply use a closure in your fetch_command config option to process your assets before the get pipelined. Take a look at #23 for more details. Following that example you only need to add url replacing logic to the process() function.

I didn't know about the prependRelativePath option. To be honest, I'm using the CSS minifier with default options. I'm going to research more and see if I can make something to, somehow, pass options from the config file to the minification libraries.

from assets.

fisharebest avatar fisharebest commented on May 18, 2024

As a quick-and-dirty proof of concept, I have this, which works.

self::$asset_manager = new \Stolz\Assets\Manager(array(
    'css_dir' => '.',
    'js_dir' => '.',
    'pipeline' => is_writable(WT_ROOT . 'asset-pipeline'),
    'pipeline_dir' => 'asset-pipeline',
    'pipeline_gzip' => 9,
    'public_dir' => WT_ROOT,
    'fetch_command' => function ($asset) {
        $unprocessed = file_get_contents($asset);
        $prefix = str_replace(WT_ROOT . '/', '.', dirname($asset)) . '/';
        $processed = str_replace('url(', 'url(' . $prefix, $unprocessed);
        return $processed;
    },
));

Obviously, I'll need a more sophisticated processor, to rewrite only local files (not data: urls, etc.), but that should be straightforward.

Thanks!

from assets.

rajivseelam avatar rajivseelam commented on May 18, 2024

This is my fetch command for Laravel:

'fetch_command' => function ($asset) {
        $unprocessed = file_get_contents($asset);
        $prefix = str_replace(public_path() . '/', '/', dirname($asset)) . '/';
        $unprocessed = preg_replace("/\burl\('([^']+)'\)/", "url($1)", $unprocessed);
        $processed = str_replace('url(', 'url(' . $prefix, $unprocessed);
        return $processed;
    },

I have added this additional line:

$unprocessed = preg_replace("/\burl\('([^']+)'\)/", "url($1)", $unprocessed);

font-awesome.css has urls like:

src: url('../fonts/fontawesome-webfont.eot?v=4.1.0')

In this case the above mentioned won't work

from assets.

Stolz avatar Stolz commented on May 18, 2024

According to http://www.w3.org/TR/CSS2/syndata.html#value-def-uri:

The format of a URI value is 'url('
followed by optional white space
followed by an optional single quote (') or double quote (") character
followed by the URI itself,
followed by an optional single quote (') or double quote (") character
followed by optional white space
followed by ')'.

A non greedy PHP regex to match that specification could be '~url\(\s?[\'"]?(.*?)[\'"]?\s?\)~' (not tested).

So here is a possible approach to rewrite relative CSS URLs:

'fetch_command' => function ($asset) {

    $content = file_get_contents($asset);
    $regex = '~url\(\s?[\'"]?(.*?)[\'"]?\s?\)~';
    $filter = function ($match) {

        // Do not process absolute URLs
        if('http://' === substr($match[1], 0, 7) or 'https://' === substr($match[1], 0, 8) or '//' === substr($match[1], 0, 2))
            return $match[0];

        // Add your filter logic here
        return 'url("../'. $match[1] . '")';
    };

    // Apply filter
    return preg_replace_callback($regex, $filter, $content);
},

from assets.

rajivseelam avatar rajivseelam commented on May 18, 2024

I am not well versed with regex but I have faced some problems with yours (which I was facing with mine too):

I had to change my regex to:

$regex = "/\burl\(\s?[\'\"]?(([^';]+)\.(jpg|eot|jpg|jpeg|gif|png|ttf|woff|svg).*?)[\'\"]?\s?\)/";

This included all the rules you have covered and adds more conditions that the resource is an image or a font file. This way I won't touch urls which don't need any fixing. For example url('data:image/png..') and some other in javascript files like: behavior:url(#default#VML)

Thanks for your input, right now my fetch_command is:

'fetch_command' => function ($asset) {

        $content = file_get_contents($asset);
        $prefix = str_replace(public_path() . '/', '', dirname($asset)) . '/';

        $regex = "/\burl\(\s?[\'\"]?(([^';]+)\.(jpg|eot|jpg|jpeg|gif|png|ttf|woff|svg).*?)[\'\"]?\s?\)/";

        $filter = function ($match) use ($prefix) {

            // Do not process absolute URLs
            if('http://' === substr($match[1], 0, 7) or 'https://' === substr($match[1], 0, 8) or '//' === substr($match[1], 0, 2))
            {
                return $match[0];
            }

            // Add your filter logic here
            return 'url(\''. $prefix.$match[1] . '\')';
        };

        // Apply filter
        return preg_replace_callback($regex, $filter, $content);
    },

from assets.

lbausch avatar lbausch commented on May 18, 2024

I used Assetic to do the rewrite stuff. This way I don't have to mess around with RegEx and can rely on battle-tested solutions. Anyway I'm sure the following snippet can be improved:

'fetch_command' => function ($asset) {    
    $content = file_get_contents($asset);

    $info = pathinfo($asset);

    $sourceRoot = 'assets/'. $info['extension'];
    $sourcePath = $info['extension'] . DIRECTORY_SEPARATOR . $info['basename'];

    $rewrite = new Assetic\Filter\CssRewriteFilter();            
    $assetic = new Assetic\Asset\StringAsset($content, [$rewrite], $sourceRoot, $sourcePath);
    $assetic->setTargetPath('assets/min');

    return $assetic->dump();
},

from assets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.