pug-php / pug-minify

One keyword to minify them all (the assets: JS, CSS, Stylus, LESS, minify, Coffee, React) in your pug-php template.
MIT License
5 stars 0 forks source link

Output with absolute url #4

Closed fdorantesm closed 7 years ago

fdorantesm commented 7 years ago

Im using CI2.

Recently i installed minify and make changes to the library, but can't add base_url to filepath because minify need's relative project path.

Works perfectly, but i cant use it.

Needed: link(href="/css/main.styl",rel="stylesheet") or link(href="base_url("css/main.styl"),rel="stylesheet")

What can i do?

Regards!

kylekatarnls commented 7 years ago

Hi,

It is not possible as it because multiple link/script tags in the view can become one tag in the output HTML.

But I just publish a new release including hooks. This may help you acheive your goal:

$pug = new Pug();
$minify = new Minify($pug);
$minify->on('post-minify', function ($params) {
    // Here you can append/prepend or modify parameter involved in the event.
    $params->outputFile = '/' . $params->outputFile;

    return $params; // Then return modified parameters.
});

Hope it do the trick for you.

fdorantesm commented 7 years ago

Thank you so much!

Need i to use $params->outputFile = base_url($params->outputFile) instead slash prepend?

Regards!

kylekatarnls commented 7 years ago

Hi,

If the function base_url is declared in the namespace where you set the hook ($minify->on...), you can use it instead. As this function does not belong to Pug, it's your own choice to use it or not if you need something more than just a slash.

Regards!

fdorantesm commented 7 years ago

Hi,

Im sorry, im forgot tell you that i'm using codeigniter. I've make a test with minify and found that error. I'll make the adjustment to verify that works properly.

Actually i'm using static compiled resources with stylus/nodejs but minify would be a great option.

Regards Kyle!

kylekatarnls commented 7 years ago

Hi,

Here is the pack I recommand to you for CodeIgniter, Pug, Stylus and minified CSS in production:

Install ci-pug in your CodeIgniter application: https://github.com/pug-php/ci-pug#installation-with-composer

Then install pug-assets:

cd application
composer require pug-php/pug-assets

Then you can create a controller base such as:

use Pug\Assets;

class ControllerBase extends CI_Controller {

  protected $assets;

  public function __construct()
  {
    parent::__construct();

    $this->load->library('pug');
    $pug = $this->pug->getEngine();
    $this->assets = new Assets($pug);

    $pug->setCustomOptions(array(
      'environment' => ENVIRONMENT,
      'assetDirectory'  => __DIR__ . '/../assets',
      'outputDirectory' => __DIR__ . '/../..',
    ));
    $this->assets->getMinify()->on('post-minify', function ($params) {
      $params->outputFile = base_url($params->outputFile)

      return $params;
    });
  }
}

Here we set the hook and the directories settings. Now you can create the directory assets in the CodeIgniter application directory and the css and js inside, then it will put the compiled files in css and js at the root, here are the commands to acheive that:

# at the website root directory
mkdir css
chmod 0777 css
mkdir js
chmod 0777 js
cd application
mkdir -p assets/css
mkdir -p assets/js

Then you can add your stylus files in application/assets/css.

Now instead of extending CI_Controller, extends your custom ControllerBase:

class Main extends ControllerBase {

  public function index()
  {
    $this->pug->view('myview');
  }
}

And in your application/views, you can have the displayed view that should probably extends a layout:

application/views/myview.pug:

extends layout

block body
  p Hello world!

And your assets can be loaded in the layout like this:

doctype html
html(lang='en')
  head
    title My Pug View
    minify app
      link(rel="stylesheet" href="css/banner.styl")
      link(rel="stylesheet" href="css/footer.styl")
      link(rel="stylesheet" href="css/blog.styl")
  body
    block body

Il will probably embed pug-assets directly in ci-pug some day like I did for pug-symfony.

Ask me if you find any trouble in implementing that.

fdorantesm commented 7 years ago

Hi,

Thanks for tutorial, that's very well explained.

I've make an update to your library, the last changes have not been yet.

https://github.com/WCLab/ci-pug-minify/blob/master/application/libraries/Jade.php

I made ci2 adaptation, removed get_vars() and sent render to output: https://github.com/WCLab/pug-ci2

Regards!

kylekatarnls commented 7 years ago

Hi, sorry to come back so late to this, but with the explanations given above do you still need to fork ci-pug and pug-minify? If yes can you explain the need to provide a solution with original projects, avoid splitting and allow every one to benefit of the improvement.