How to Add Script Type to registerScriptFile

I have inherited ex developers code. The app itself is javascript based, however, the backend is managed by Yii.

The changelog file reports a version 1.1.13.

All the js files are registered in a file called: AController.php, located at ‘protected/controllers/’

I have added a javascript transformer library into the <head>. The library in particular is babel IO. This javascript transformer library requires the javascript files to have the script type specified as ‘text/babel’. I.e. the script tag of all js files which will be transformed by babel will look something like this:


<script type="text/babel" src=""></script>

The solution I tried so far include instructions as per the link -> (add the yiiframework website address here)/doc/api/1.1/CClientScript#registerScriptFile-detail

However, to no avail. When I inserted this instruction into the above AController.php file:


$scripts->registerScriptFile ($baseUrl . '/js/views/Components.js?'. $this->version,CClientScript::POS_HEAD, array('type'=>'text/babel'));



The Component.js file on localhost still reports the type="text/javascript" instead of type"text/babel" & babel is unable to transform this file.

Any help would be much appreciated.

By your writing I guess that AController.php should somehow generate the Component.js file… but does it generate it every time?

Your solution should work, the rendered script tag should have "text/babel" but could it be that your Component.js file does not get recreated every time.

Thanks for getting back to me. AController.php (appears to) register all the js files to load. Here is some of the content of the file:


class AphroditeController extends Controller {


          public $defaultAction = 'initialize';

          public $layout = '//layouts/skeleton';

          public $features;

          public $templates;

          public $user_id;

          public $user_name;

          public $user_email;

          public $version;

          private $DBConnection;


          /**

           * Action to render Index

           */

          public function actionIndex() {

                    $this->render('index');

          }


          public function actionInitialize() {


                    $this->version = 93;

                    $this->verifyUserAgent();

                    $this->DBConnection = Yii :: app()->db;

                    $this->features = $this->getAllFeatures();

                    $this->templates = $this->getAlltemplates();

                     $this->user_id = 1;


                    $this->includeStyles();

                    $this->includeScripts();

                    $this->actionIndex();

          }

          public function includeStyles() {

                    $baseUrl = Yii::app()->baseUrl;

                    $styles = Yii::app()->getClientScript();


                    $styles->registerCssFile($baseUrl . '/ css / styles.css?' . $this->version);


          }

          public function includeScripts() {


                    $baseUrl = Yii::app()->baseUrl;

                    $scripts = Yii::app()->getClientScript();

                    /**

                     * Adding Libraries

                     */

                   $scripts->registerScriptFile($baseUrl . ' / js / libraries / greensock / velocity.min.js');

                   $scripts->registerScriptFile('h t t p s : / / cdnjs.cloudflare.com / ajax / libs / babel-core/5.8.23/browser.min.js');

... 

                    /**

                     * Adding js files

                     */       

                   $scripts->registerScriptFile($baseUrl . '/js/controllers/SocialShareController.js?'. $this->version);

...

                  $scripts->registerScriptFile($baseUrl . '/js/views/Components.js?'. $this->version,CClientScript::POS_HEAD, array('type'=>'text/babel'));

...



Notes:

You can see the babel-core/5.8.23 being loaded in the libraries section of js files of public function includeScripts().

There are lots and lots of js files. All of the them of the type as per the ‘SocialShareController.js’, its only the special js files that needs to transformed by Babel library above that need the ‘type=text/babel’.

Components.js is a new file, I do not think its being cached. I have added the Components.js file, it did not exist before.

Many thanks