SailsJS loads assets from the local server when environment = DEV and loads assets from CDN when environment = PROD

Hi, I have a web application, and when I work on my computer and in dev mode in the countryside, I need to be able to use assets from a local folder. But when I transfer my code to the PROD server, I need to switch the assets that will be downloaded from the CDN.

Is this possible anyway with sails.environment global?

eg

<% if(sails.environment == 'PROD') { %> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-cookies.min.js"></script> <% } else { %> <script src="/js/jquery.min.js"></script> <script src="/js/bootstrap.min.js"></script> <script src="/js/angular.min.js"></script> <script src="/js/angular-resource.min.js"></script> <script src="/js/angular-cookies.min.js"></script> <% } %> 

Thanks.

+5
source share
2 answers

These tags are inserted by Grunt.

I think you can change tasks/config/sails-linker.js to achieve what you want:

there is a devJS and prodJs , I think you should change them to suit your requirements.

Please note that I do not know if sailing is good practice.

Edit a few more things that might help:

sails lift -> Runs the default job (tasks / register / default.js).

sails lift --prod -> Runs the prod task (tasks / register / prod.js).

from http://sailsjs.org/#/documentation/concepts/Assets/TaskAutomation.html

0
source

You can achieve what you did in your example using sails.config.environment or process.env.NODE_ENV , but the best solution would be to use environment-specific configuration files to specify the path to the resources. For example, in config/development.js you might have:

 "assets_path": "/js" 

and config/production.js :

 "assets_path": "//ajax.googleapis.com/ajax/libs/jquery/1.10.2" 

Then, in your opinion:

 <script src="<%=sails.config.assets_path%>/jquery.min.js"></script> 
0
source

Source: https://habr.com/ru/post/1210787/


All Articles