条件RequireJs配置(以编程方式加载requirejs-config.js吗?)


15

我只想在某些条件下(例如,基于配置)替换RequireJs组件。有什么方法可以以编程方式防止模块的加载requirejs-config.js或以其他方式实现该模块的加载?


1
您找到解决此问题的方法了吗?
stevensagaar

不幸的是,@ stevensagaar不是
Fabian Schmengler

2
如果找到一个,我将在此处添加答案
Fabian Schmengler,

3
@Alex如果有2.2或2.3的解决方案,我也很高兴:D更新了标签。另外,感谢您的悬赏!
Fabian Schmengler

2
您是否尝试过在vendor / magento / framework / RequireJs / Config.php中重写getConfig函数,还是需要在requirejs中编写插件requirejs.org/docs/plugins.html
Arshad M,

Answers:


5

基于@Arshad M注释,您可以添加带有以下内容的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">

    <preference for="Magento\Framework\RequireJs\Config" type="<Vendor>\<ModuleName>\RequireJs\Config"/>

</config>

在<供应商> \ <ModuleName> \ RequireJs \ Config.php中,重写getConfig函数,添加条件和不想加载requirejs的模块名称(可能是从ScopeConfigInterface加载):

   <?php

namespace <Vendor>\<ModuleName>\RequireJs;

use Magento\Framework\Filesystem\File\ReadFactory;
use Magento\Framework\View\Asset\Minification;
use Magento\Framework\View\Asset\RepositoryMap;

class Config extends \Magento\Framework\RequireJs\Config
{
    /**
     * @var \Magento\Framework\RequireJs\Config\File\Collector\Aggregated
     */
    private $fileSource;
    /**
     * @var ReadFactory
     */
    private $readFactory;
    /**
     * @var \Magento\Framework\Code\Minifier\AdapterInterface
     */
    private $minifyAdapter;
    /**
     * @var Minification
     */
    private $minification;
    /**
     * @var \Magento\Framework\View\DesignInterface
     */
    private $design;

    public function __construct(\Magento\Framework\RequireJs\Config\File\Collector\Aggregated $fileSource, \Magento\Framework\View\DesignInterface $design, ReadFactory $readFactory, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Code\Minifier\AdapterInterface $minifyAdapter, Minification $minification, RepositoryMap $repositoryMap)
    {
        parent::__construct($fileSource, $design, $readFactory, $assetRepo, $minifyAdapter, $minification, $repositoryMap);
        $this->fileSource = $fileSource;
        $this->readFactory = $readFactory;
        $this->minifyAdapter = $minifyAdapter;
        $this->minification = $minification;
        $this->design = $design;
    }

    public function getConfig()
    {
        $distributedConfig = '';
        $customConfigFiles = $this->fileSource->getFiles($this->design->getDesignTheme(), self::CONFIG_FILE_NAME);
        foreach ($customConfigFiles as $file) {
            //Your condition
            if(true){
                if($file->getModule() == "Vendor_ModuleName"){
                    continue;
                }
            }

            /** @var $fileReader \Magento\Framework\Filesystem\File\Read */
            $fileReader = $this->readFactory->create($file->getFileName(), \Magento\Framework\Filesystem\DriverPool::FILE);
            $config = $fileReader->readAll($file->getName());


            $distributedConfig .= str_replace(
                ['%config%', '%context%'],
                [$config, $file->getModule()],
                self::PARTIAL_CONFIG_TEMPLATE
            );
        }

        $fullConfig = str_replace(
            ['%function%', '%usages%'],
            [$distributedConfig],
            self::FULL_CONFIG_TEMPLATE
        );


        if ($this->minification->isEnabled('js')) {
            $fullConfig = $this->minifyAdapter->minify($fullConfig);
        }

        return $fullConfig;
    }
}

更新

在@Alex和@Daniel发表评论之后:您可以从Magento \ Framework \ RequireJs \ Config \ File \ Collector \ Aggregated中为getFiles创建一个after插件,因此采用这种方法的新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\RequireJs\Config\File\Collector\Aggregated">
        <plugin name="requirejsConfigPlugin"
                type="<Vendor>\<ModuleName>\Plugin\RequireJs\AfterFiles"
                sortOrder="100"
        />
    </type>
</config>

在\ <供应商> \ <ModuleName> \ Plugin \ RequireJs \ AfterFiles,您可以设置条件和模块,以确保requirejs不会被加载:

<?php

namespace <Vendor>\<ModuleName>\Plugin\RequireJs;

class AfterFiles
{
    public function afterGetFiles(
        \Magento\Framework\RequireJs\Config\File\Collector\Aggregated $subject,
        $result
    ){
        //Your condition
        if(true) {
            foreach ($result as $key => &$file) {
                //Module to exclude
                if ($file->getModule() == "Vendor_OtherModuleName") {
                    unset($result[$key]);
                }
            }
        }
        return $result;
    }
}

真好!我认为我们可以通过$ fullConfig = parent :: getConfig()进行改进,然后修改$ fullConfig以复制并粘贴更少的代码。你怎么看?也许我们应该为此在github上制作一个mini-FOSS模块?
亚历克斯

1
还是可以改写$ this-> fileSource-> getFiles?只是为了不复制太多代码……
亚历克斯

3
@Alex您也可以使用插件并使用aroundGetConfig()or afterGetConfig()方法来实现有条件的加载,那么我们不必覆盖它
Daniel Daniel

看起来很有希望,谢谢!我已经投票了,在接受答案之前会尽快尝试
Fabian Schmengler 18/12/15

2
@Alex根据您的建议,我在github中制作了一个小模块,您可以在其中选择通过magento后端禁用requirejs的模块。检查出来,也许贡献 github.com/MNGemignani/magento2_requirejs_disable
gemig_hol
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.