ishanvyas22 / asset-mix

Provides helpers functions for CakePHP to use Laravel Mix.
MIT License
33 stars 12 forks source link

Unable to find assets with external cssBaseUrl and jsBaseUrl #35

Closed amayer-hc closed 2 years ago

amayer-hc commented 3 years ago

Description

My assets are hosted on a CDN in production. According to the Cake Book the App.cssBaseUrl and App.jsBaseUrl should be the URL and path to the CDN. When I set App.jsBaseUrl to https://example.com/js/ I get the following error when trying to execute $this->AssetMix->script('app');:

Unable to locate AssetMix file: /https://example.com/js/app.js.

Steps To Reproduce

  1. Add the following lines to app_local.php. Combine the App array if one already exists.
    'App' => [
        'cssBaseUrl' => 'https://example.com/css/',`
        'jsBaseUrl' => 'https://example.com/js/',
    ]
  2. Compile javascript and CSS assets.
    mix.js('resources/js/app.js', 'webroot/js')
        .sass('resources/sass/main.scss', 'webroot/css');
  3. Add $this->AssetMix->js('app'); to a template file.

Expected behavior

The CSS file should load from https://example.com/css/main.css and the javascript should load from https://example.com/js/app.js.

Tests

I wrote these tests in case they would be useful.

/**
 * Test `css()` function returns proper url
 *
 * @return void
 */
public function testStyleTagWithExternalBaseUrl()
{
    Configure::write('App.cssBaseUrl', 'https://example.com/css/');

    $this->_copyWithoutVersion();

    $result = $this->AssetMix->css('main');

    $this->assertStringContainsString('https://example.com/css/main.css', $result);
}

/**
 * Test `script()` function returns proper url
 *
 * @return void
 */
public function testScriptTagWithExternalBaseUrl()
{
    Configure::write('App.jsBaseUrl', 'https://example.com/js/');

    $this->_copyWithoutVersion();

    $result = $this->AssetMix->script('app');

    $this->assertStringContainsString('https://example.com/js/app.js', $result);
}
ishanvyas22 commented 3 years ago

Hey @amayer-hc, thanks opening a bug report with detailed description. Would you mind submitting a PR?