1

How to make disable JavaScript merge only for Admin panel?

Is it possible to make on coding level?

3

In order to achieve this I had to create a plugin for the admin area only and change the methods that read the settings.

Even if you have the possibility to choose different values on default and store view levels, the class that reads these settings values is not using the correct store code, and always using the store view level value.

The solution is to create a module, and add a di.xml for adminhtml area to forcely use the desired settings. The core class responsible for this is Magento\Framework\View\Asset\Config

Let's make a plugin for that:

  1. Create the module along with registration.php and etc/module.xml files

  2. Create the adminhtml di.xml file

app/code/NameSpace/ModuleName/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Asset\Config">
        <plugin name="mergeminify_asset_config" type="NameSpace\ModuleName\Plugin\View\Asset\Config" />
    </type>
</config>
  1. Create the plugin class app/code/[NameSpace]/[ModuleName]/Plugin/View/Asset/Config.php

with the following content:

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;

class Config
{
    protected $_registry;
    /**
     * XML path for CSS files merge configuration
     */
    const XML_PATH_MERGE_CSS_FILES = 'dev/css/merge_css_files';

    /**
     * XML path for JavaScript files merge configuration
     */
    const XML_PATH_MERGE_JS_FILES = 'dev/js/merge_files';

    /**
     * XML path for asset minification adapter configuration
     */
    const XML_PATH_JS_BUNDLING = 'dev/js/enable_js_bundling';

    /**
     * XML path for HTML minification configuration
     */
    const XML_PATH_MINIFICATION_HTML = 'dev/template/minify_html';

    /**
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    public function __construct(
        \Magento\Framework\Registry $registry,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->_registry = $registry;
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * Check whether merging of CSS files is on
     *
     * @return bool
     */
    public function afterisMergeCssFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MERGE_CSS_FILES,
            ScopeInterface::SCOPE_STORE,
            0 // Force default value
        );
    }

    /**
     * Check whether bundling of JavScript files is on
     *
     * @return bool
     */
    public function afterisBundlingJsFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_JS_BUNDLING,
            ScopeInterface::SCOPE_STORE,
            0 // Force default value
        );
    }

    /**
     * Check whether merging of JavScript files is on
     *
     * @return bool
     */
    public function afterisMergeJsFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MERGE_JS_FILES,
            ScopeInterface::SCOPE_STORE,
            0 // Force default value
        );
    }

    /**
     * Check whether minify of HTML is on
     *
     * @return bool
     */
    public function afterisMinifyHtml()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MINIFICATION_HTML,
            ScopeInterface::SCOPE_STORE,
            0 // Force default value
        );
    }
}

This way the backend will always use the default value and the frontend will use the store view value

6

Try below extension:

  1. app/code/[VendorName]/[ModuleName]/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[VendorName]_[ModuleName]',
    __DIR__
);
  1. app/code/[VendorName]/[ModuleName]/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[VendorName]_[ModuleName]" setup_version="1.0.0"/>
</config>
  1. app/code/[VendorName]/[ModuleName]/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Asset\Config">
        <plugin name="mergeminify_asset_config" type="[VendorName]\[ModuleName]\Plugin\View\Asset\Config" />
    </type>
</config>
  1. app/code/[VendorName]/[ModuleName]/Plugin/View/Asset/Config.php
namespace [VendorName]\[ModuleName]\Plugin\View\Asset;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;

class Config
{
    protected $_registry;
    /**
     * XML path for CSS files merge configuration
     */
    const XML_PATH_MERGE_CSS_FILES = 'dev/css/merge_css_files';

    /**
     * XML path for JavaScript files merge configuration
     */
    const XML_PATH_MERGE_JS_FILES = 'dev/js/merge_files';

    /**
     * XML path for asset minification adapter configuration
     */
    const XML_PATH_JS_BUNDLING = 'dev/js/enable_js_bundling';

    /**
     * XML path for HTML minification configuration
     */
    const XML_PATH_MINIFICATION_HTML = 'dev/template/minify_html';

    /**
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * Default store view code
     */
    const DEFAULT_STORE = 'default';

    /**
     * State
     *
     * @var \Magento\Framework\App\State
     */
    protected $state;

    protected $storeScope = null;

    public function __construct(
        \Magento\Framework\Registry $registry,
        ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\State $state
    ) {
        $this->_registry = $registry;
        $this->scopeConfig = $scopeConfig;
        $this->state = $state;
    }

    public function afterisMergeCssFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MERGE_CSS_FILES,
            $this->getStoreScope()
        );
    }

    /**
     * Check whether bundling of JavScript files is on
     *
     * @return bool
     */
    public function afterisBundlingJsFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_JS_BUNDLING,
            $this->getStoreScope()
        );
    }

    /**
     * Check whether merging of JavScript files is on
     *
     * @return bool
     */
    public function afterisMergeJsFiles()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MERGE_JS_FILES,
            $this->getStoreScope()
        );
    }

    /**
     * Check whether minify of HTML is on
     *
     * @return bool
     */
    public function afterisMinifyHtml()
    {
        return (bool)$this->scopeConfig->isSetFlag(
            self::XML_PATH_MINIFICATION_HTML,
            $this->getStoreScope()
        );
    }

    /**
    * Check store scope is admin
    *
    * @return bool
    */
    public function getStoreScope()
    {
        if($this->storeScope == null) {
            $this->storeScope = (($this->state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) ? self::DEFAULT_STORE : ScopeInterface::SCOPE_STORE);
        }
        return $this->storeScope;
    }
}
1
  • 2
    Ayo baki market ma :) Nov 27 '17 at 5:51
1

Without doing code,you can this.

First goto admin>Store>Configuration>Advance>Developer Then Select current scope Default Config.

enter image description here

Then goto Make JavaScript Settings and Merge JavaScript Files select it value to NO

This changes remove merge from all area both frontend and admin.

Now, goto admin>Store>Configuration>Advance>Developer>Make JavaScript Settings Then Select current scope Main Website.

Then change Merge JavaScript Files select it value to Yes

Then

flush magento cache and

run static content deploy: bin/magento setup:static-content:deploy

This solution will only disable js merge for admin

2
  • 1
    your solution is not working for me. :(
    – Bhavik
    Mar 14 '17 at 8:27
  • 1
    Yeah seems like it should work but it doesn't Admin still minified for some reason.
    – harri
    Jun 9 '17 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.