Jasmine src_file wildcard usage in Rails - javascript

I'm having an issue when it comes to requiring individual Javascripts for Jasmine file. My application.js.coffee file doesn't include require_tree as I am loading page specific javascript in a tag in each view.
I added this:
src_files:
- assets/application.js
- assets/*.js
which does not seen to load any JS files that are located in assets/. However, the files are clearly there because if I use
src_files:
- assets/application.js
- assets/feedback.js
The feedback.js file is loaded correctly and the test passes. Why does the wildcard not load the files in directory? Also assets/*/.js doesn't work either for folders.

When the jasmine gem looks up your files on disk it uses Dir.glob to expand out the wildcards (*) in the path. In the case that Dir.glob doesn't find any files, and the path specified doesn't have any wildcards in it, then jasmine will just add that file anyways. This basically assumes you're using sprockets and this will work.
This means, if all of you top-level includes are coffescript files they won't actually match *.js.
You can either:
Add an additional top-level .js file that includes all of your javascript and use that in jasmine.yml
Add each of your top-level files to jasmine.yml individually
Add a *.js.coffee rule to you jasmine.yml and then a negative matcher to remove things you don't want.
The last option would look something like this (the ! tells jasmine to remove files matching this glob from the list of files to include):
src_files:
- assets/application.js
- assets/*.js.coffee
- !assets/subfolder/*.js.coffee

Related

Include AngularJs javascript files into Angular cli

I have a legacy Angular JS application and now working in tandem with few new Angular 5 components using upgrade module. Following this post here
Currently, I need to include all my AngularJs code into my index.html.
But, I want to include all JS files (more than 200) in my angular-cli.json in scripts section like below:
"scripts": [
"../appjs/**"
],
But, ng-build gives me error no such file or directory:\appjs\**.
How to include all the files in the folder in on go avoiding to include all the files one by one.
Here is the image of the folder structure.
Please guide. Thanks.
Unfortunately the scripts: [] attribute doesn't accept globbing patterns. This is alluded to in the documentation:
scripts: An object containing JavaScript script files to add to the
global context of the project. The scripts are loaded exactly as if
you had added them in a tag inside index.html.
So the alternative is to use a powershell script or DOS cmd to list the files and output them to the screen/file and then add all the necessary quotes to paste them into the scripts attribute in the config file.

Rails Assets Pipeline load JavaScript from controllers and methods

I want to stay DRY in my code so I want to auto-load my javascripts file when it matches a controller or/and a method and the .js exists. I added this to my layout
= javascript_include_tag params[:controller] if ::Rails.application.assets.find_asset("#{params[:controller]}.js")
= javascript_include_tag "#{params[:controller]}/#{params[:action]}" if ::Rails.application.assets.find_asset("#{params[:controller]}/#{params[:action]}.js")
So now when I add javascripts/my_controller/my_method.js it automatically loads it, which's nice.
Sadly I must add another line to precompile the asset otherwise an error is thrown (which says I must precompile my .js file) and I didn't find any way around this.
Rails.application.config.assets.precompile += %w( orders/checkout.js )
Does anyone has a solution to avoid tu add manually elements in this configuration ?
NOTE : I already tried to use require_tree . which was just loading all the files on every page and was not working in my case.
You can use a wildcard to allow all JS files included in your views to be precompiled:
config.assets.precompile << '*.js'

Asset Pipeline in Ruby on Rails with Javascript

I've been having trouble using the Asset Pipeline. Although I read an excellent guide here I still have trouble.
I'm trying a javascript solution for adding form elements "on the fly" from this [guide] (http://jyrkis-blogs.blogspot.com/2014/06/adding-fields-on-fly-with-ruby-on-rails.html#code4Div)
When I put the javascript into a <script> tag at the bottom of my page, everything works as expected.
If I simply copy and paste the script into application.js the code also works.
However, when I try to move the code to app/assets/javascripts/people.js my site throws a Reference Error.
This is a similar problem to the questions on SO titled 'Ruby on Rails 3.1 RC1 Javascript Asset Pipeline Problem' (not enough rep for the link), but I only have one script, so the alphabetical solution doesn't help. Moving the file to vendor/assets/javascript also didn't help.
Application.js currently looks like:
This is a manifest file that'll be compiled into application.js, which will include all the files
listed below.
Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
compiled file.
Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
about supported directives.
= require people.js
= require_tree .

Exclude map files from rails assets pipeline

My Rails project contains TypeScript files which are being translated into JavaScript. For every translated .js file I also get a .js.map file. An example file structure is as follows:
/assets/javascripts/resources/Setting.ts
/assets/javascripts/resources/Setting.js
/assets/javascripts/resources/Setting.js.map
And in my application.js I simply do
//= require_tree .
The problem is that when this whole things gets rendered I get the following HTML (for each TypeScript file I have):
<script src="/assets/resources/Setting.js?body=1"></script>
<script src="/javascripts/resources/Setting.js.map.js"></script>
As you can see - for some reason Rails thinks that Settings.js.map file is to be included and automatically adds .js to it.
If this means anything - the TypeScript compilation happens only at IDE level, so it is not integrated into the Rails in any way.
Rails version: 4.2.1
So.. how do I exclude those map files?

Non global coffee script and asset pipeline

I have a coffee script user.js.coffee, that is only used in certain views. I achieved this by using the following answer:
https://stackoverflow.com/a/6795533/784318
Now I have excluded the script from the application.js. I also removed the //= require_tree . entry.
So my file is available here: http://localhost:3000/assets/user.js however, when I deploy this to the server the assets will be combined in one application.js so how can I make sure that the user.js will be available on production like so: http://myserver.com/assets/user.js?
In environments/production.rb (or the environment you need precompile to occur) uncomment or add this file to the precompile array:
# environments/production.rb
config.assets.precompile += %w( user.js )
Other entries might be already present, just add any other file that you need to access separately.
This file will not get compiled in one big application.js file and will be accessible separately as user.js
You can try putting your user.js file to the public folder of your application directly and configure your asset pipeline to exclude it from the "zipping" process.

Categories