Include javascript file in cms - SilverStripe 4.1

Silverstripe Version:
4.1

Question:

Hi,

I create for my customer module with ModelAdmin + DataObject.
My ModelAdmin class

<?php

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\View\Requirements;
use SilverStripe\Core\Manifest\Module;

class MultisiteAdmin extends ModelAdmin
{
    private static $url_segment = "mymodule";
    private static $managed_models = [
	...
    ];
    public $showImportForm = false;
    public $showSearchForm = false;



protected function init()
    {
        parent::init();
        Requirements::javascript('mysite/javascript/mymodule.js');
        
    }

}

Mymodule.js file was not include
I have read about modules https://docs.silverstripe.org/en/4/developer_guides/extending/how_tos/publish_a_module/

I can`t publish a module that is written for the client.

Are you using the public directory with your installation?
If so, then you’ll need to expose any assets that you want to be publicly accessible (they’ll end up in the /public/resources directory as a rule).

So, in your composer.json file you’d end up with a section along the lines of:

"expose": [
  "mysite/javascript"
]

If you’re just building a site without using themes, then you can just create a public/javascript directory and put the scripts straight in there. You should then just be able to use something like Requirements::themedJavascript('mymodule') and SilverStripe ought to work it out for you.

Note: everything above is completely untested!

Thank you for answer.
I built a trait that embeds links outside of public

use SilverStripe\View\Requirements;
use SilverStripe\Assets\Filesystem;

trait buildPublicLink
{
    public function publicLinkBuilder(Array $assets )
    {
        if(!is_array($assets)) return false; 
        $package = __CLASS__;

        foreach($assets as $type => $files)
        {
            $target_folder = 'resources/vendor/<your_vendor_name>/'.$package.'/client/'.$type;
            $data = Filesystem::makeFolder($target_folder);
        
                foreach($files as $file)
                {
                    $file_path = '../'.$file;
                    if(file_exists($file_path)) 
                    {
                        $link_name = basename($file_path);
                        $target_link = $target_folder.'/'.$link_name;
                        if(!file_exists($target_link))
                            link($file_path,$target_link);

                        Requirements::$type($target_link);
                        
                    }
                } 
        } 
    } 
}

Now, include trait on my ModelAdmin

class MyModelAdmin extends ModelAdmin
{
 use buildPublicLink;
 public function init()
{
 parent::init();
      $requirements = [
        'css' => [
          '<path_to_css_file>'
        ],
        'javascript' => [
          '<path_to_js_file>'
        ]
      ];
      $this->publicLinkBuilder($requirements);
}
}

Now I have auto-create hardlinks for my css/js files on public + autoincldue with Requirements::<javascript|css>()

I’m curious why you took that approach, rather than just exposing the files using the existing functionality?

This is a bad approach, I’m sorry to say. You should use the “expose” API via composer configuration. https://docs.silverstripe.org/en/4/changelogs/4.1.0/#expose-root-files