tattersoftware / codeigniter4-assets

Asset handling for CodeIgniter 4
MIT License
50 stars 12 forks source link

Problem with Upgrade to v3 #50

Closed jozefrebjak closed 2 years ago

jozefrebjak commented 2 years ago

Hi @MGatner ,

I'm trying to upgrade to v3. I already changed service behaviour to filter, but I have a problem with publishers.

I'm tried to setup own publisher but I got an error like:

/app/vendor/almasaeed2010/adminlte/dist/js/adminlte.min.js fails the following restriction for /app/public/: #\.(?css|js|map|htm?|xml|json|webmanifest|tff|eot|woff?|gif|jpe?g|tiff?|png|webp|bmp|ico|svg)$#i```

then I tried to use your Frontend library and it's same. Can you help me what I'm doing wrong.

I'm running dev in docker.

jozefrebjak commented 2 years ago

@MGatner Here is my publisher for example for Alpine.js

<?php

namespace App\Publishers;

use CodeIgniter\Publisher\Publisher;

class AlpinePublisher extends Publisher
{
    /**
     * Tell Publisher where to get the files.
     * Since we will use Composer to download
     * them we point to the "vendor" directory.
     *
     * @var string
     */
    protected $source = \ROOTPATH . 'node_modules/alpinejs';

    /**
     * FCPATH is always the default destination,
     * but we may want them to go in a sub-folder
     * to keep things organized.
     *
     * @var string
     */
    protected $destination = \FCPATH . 'vendor/alpinejs';

    /**
     * Use the "publish" method to indicate that this
     * class is ready to be discovered and automated.
     *
     * @return boolean
     */
    public function publish(): bool
    {
        return $this
            // Add all the files relative to $source
            ->addPath('dist')

            // Indicate we only want the minimized versions
            ->retainPattern('*.min.*')

            // Merge-and-replace to retain the original directory structure
            ->merge(true);
    }
}

Problem no.1

I must specify \ROOTPATH & \FCPATH. Is default ok ? Docs are say that it's default.

Problem no.2

What is expected ? Publisher will create directories vendor & alpinejs ?

Error

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-02-22 18:15:26 UTC+01:00

[CodeIgniter\Files\Exceptions\FileException]

__construct expects a valid directory.

at SYSTEMPATH/Files/FileCollection.php:50

Backtrace:
  1    SYSTEMPATH/Files/FileCollection.php:50
       CodeIgniter\Files\Exceptions\FileException::forExpectedDirectory('__construct')

  2    SYSTEMPATH/Publisher/Publisher.php:160
       CodeIgniter\Files\FileCollection::resolveDirectory('/app/public/vendor/alpinejs')

  3    SYSTEMPATH/Publisher/Publisher.php:119
       CodeIgniter\Publisher\Publisher()->__construct()

  4    SYSTEMPATH/Commands/Utilities/Publish.php:77
       CodeIgniter\Publisher\Publisher::discover('Publishers')

  5    SYSTEMPATH/CLI/Commands.php:63
       CodeIgniter\Commands\Utilities\Publish()->run([])

  6    SYSTEMPATH/CLI/CommandRunner.php:70
       CodeIgniter\CLI\Commands()->run('publish', [])

  7    SYSTEMPATH/CLI/CommandRunner.php:56
       CodeIgniter\CLI\CommandRunner()->index([])

  8    SYSTEMPATH/CodeIgniter.php:823
       CodeIgniter\CLI\CommandRunner()->_remap('index', 'publish')

  9    SYSTEMPATH/CodeIgniter.php:412
       CodeIgniter\CodeIgniter()->runController(Object(CodeIgniter\CLI\CommandRunner))

 10    SYSTEMPATH/CodeIgniter.php:320
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

 11    SYSTEMPATH/CLI/Console.php:48
       CodeIgniter\CodeIgniter()->run()

 12    ROOTPATH/spark:63
       CodeIgniter\CLI\Console()->run()

exit status 9

Problem no.3

If I create folders manually then

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-02-22 18:19:42 UTC+01:00

App\Publishers\AlpinePublisher failed to publish to /app/public/vendor/alpinejs/!
/app/node_modules/alpinejs/dist/cdn.min.js
/app/node_modules/alpinejs/dist/cdn.min.js fails the following restriction for /app/public/: #\.(?css|js|map|htm?|xml|json|webmanifest|tff|eot|woff?|gif|jpe?g|tiff?|png|webp|bmp|ico|svg)$#i
MGatner commented 2 years ago

It looks like you are having some issues with Publisher that aren't related to this library. As far as I can tell you have everything configured correctly, but the framework's security restrictions are triggering when you try to publish to the public path. But if you look at the error it essentially says "your .js file does not meet the restrictions: a bunch of extensions, including .js"

Try adding your own Publisher Config in app/Config and change FCPATH to allow everything, see if that works for starters.

As follow-up you will need to take this issue to the framework for troubleshooting, unless you discover something else that points to this library.

jozefrebjak commented 2 years ago

@MGatner If I edit Publisher conf like:

 public $restrictions = [
        ROOTPATH => '*',
        FCPATH   => '*',
    ];

It's trying to publish to /app/public/alpinejs/ what is wrong. I don't understand why is there /app/ in front of.

But cdn.min.js is in place.

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-02-23 20:13:08 UTC+01:00

App\Publishers\AlpinePublisher published 1 file(s) to /app/public/alpinejs/.
jozefrebjak commented 2 years ago

@MGatner Ok I spent few hours to get it working.

Here is my final Publisher

<?php

namespace App\Publishers;

use CodeIgniter\Publisher\Publisher;

class AlpineJSPublisher extends Publisher
{
    /**
     * Tell Publisher where to get the files.
     * Since we will use Composer to download
     * them we point to the "vendor" directory.
     *
     * @var string
     */
    protected $source = \ROOTPATH . '/node_modules';

    /**
     * FCPATH is always the default destination,
     * but we may want them to go in a sub-folder
     * to keep things organized.
     *
     * @var string
     */
    protected $destination = \FCPATH . '/assets/vendor';

    /**
     * Use the "publish" method to indicate that this
     * class is ready to be discovered and automated.
     *
     * @return boolean
     */
    public function publish(): bool
    {
        return $this
            ->addPath('alpinejs')
            ->retainPattern('*.min.*')
            ->merge(true);
    }
}

I must specify ROOTPATH & FCPATH because of erro in file SYSTEMPATH/Files/FileCollection.php:50.

I tried to achieve to auto creation of folders with assets. If I specify in protected $source = \ROOTPATH . '/node_modules/alpinejs'; then it won't work. So I tried to put folders directly to addPath and it works!

Something must be wrong with this filter #\.(?css|js|map|htm?|xml|json|webmanifest|tff|eot|woff?|gif|jpe?g|tiff?|png|webp|bmp|ico|svg)$# because with that it wont work. I'm not so happy to don't use any filter now, but at least I can publish my assets and use them with your assets library.

MGatner commented 2 years ago

Glad you got it fixed! If you have a version of your code/repo that you believe is "bugged" then submit it as a bug report to the CodeIgniter repo.