sprockets - precompiling a standalone asset - ruby-on-rails-4

I am trying to make sprokets compile a single standalone js asset, so it will uglify and minify it and be part of the entire rails projects.
I need that this js to have a non-digest name, so it's will not change (i.e. embedded in other websites, etc)
I can't seem to force rails (4) /sprockets to do my bidding.
What I tried:
Adding the asset (script.js) in a misc folder unders assets/javascripts and not load it in the sprockets javascript manifest. While this keeps it in the project, it doesn't get uglified and minified, and doesn't get automatically loaded via asset-sync.
Tried adding another manifest called scripts-manifest.js to //= require script.js asset, to add its path in the precompile path in application.rb, but the problem is that rails 4 adds digest to all assets no matter what (doesn't work like that in rails 3)
Tried using https://github.com/alexspeller/non-stupid-digest-assets to add a non digest version of the asset. I may have done it incorrectly, as it doesn't work or do anything..
I add the initializer NonStupidDigestAssets.whitelist = ["script.js"] and tried putting it in app/assets/javascripts/misc and in public/ but it still won't work/
I have read that this gem should help in most cases, and I am sure I am doing something wrong with either path definition or no including it somewhere

One way to do this is to add an initializer that generates the compiled versions directly.
Add your js file to a subfolder in /app/assets/javascripts. Don't include this in application.js so it isn't added to the compiled assets.
Create an initializer in /config/initializers that uses uglify directly
output_file = "#{Rails.root}/public/public_script.js"
input_file = "#{Rails.root}/app/assets/javascripts/non_digest/public_script.js"
uglified = Uglifier.compile(File.read(input_file))
File.open(output_file, 'w') {|f| f.write(uglified) }
Include the public js file (in this example: /public/public_script.js) in your application layout
This way you have direct access to make custom changes to how uglify handles your js and the location of the file never changes for your external services accessing them.
I did all this locally and tested that it worked using the beta version of Rails 4.2

Just wanted to add my own solution based off Ken's answer.
I created non_digest.rb in config/initializers:
Dir["#{Rails.root}/app/assets/javascripts/non_digest/*"].each do |asset|
asset_name = File.basename(asset)
asset_output = "#{Rails.root}/public/external/#{asset_name}"
asset_uglified = Uglifier.compile(File.read(asset))
File.open(asset_output, 'w') {|a| a.write(asset_uglified) }
end
Don't forget to stub the file in javascripts/application.js. as we probably don't want it compiled with the rest of our JS and we can continue to use //= require_tree .:
//= stub non_digest/external_bookmarklet

the way you would do this with rails 4 is the following:
add it to the precompile list config.assets.precompile += %w(your_file_name.js)
make sure it's not referenced in application.js (directly or via require_tree)
symlink the digested file on deployment
read the manifest.yml to get the actual filename
ln -s digested-filename.js actual-filename.js
since rails 4, generation of non-digested assets has been removed (for good reasons) and this is a simple and straight forward way to implement the desired behavior.

Related

How to load local CSS with react-styleguidist 7 and Webpack 4

I want to load local 3rd party css (eg font-awesome icons) as part of my styleguide. As many components uses the same CSS, I want it to be loaded automatically and included as part of the static build.
I settled on using mini-css-extract-plugin to accompolish this, but styleguidist maintainer #sapegin basically told me that I have no idea what I am doing and stop complaining to him without providing any help. So I was hoping the stack-overflow community would give me a hand in pointing out what is the correct way:
styleguidist discussion: https://github.com/styleguidist/react-styleguidist/pull/985#issuecomment-389422909
My github repo demonstrating the three ways of doing it:
https://github.com/bugzpodder/styleguidist-local-css-example/tree/master
master: uses mini-css-extract-plugin + require config, build and devserver works as expected.
require: I couldn't get it to work without mini-css-extract-plugin
template: devserver works, but build does not. The only workwaround I can think of is to use publicPath and copy the css there, but publicPath isn't allowed unless you dangerouslyupdatewebpackconfig https://github.com/styleguidist/react-styleguidist/pull/956

How to compile project's CSS files into one file

By default ember-cli seems to be set up not to compile css files into one (as it does with the JS files).
What is the best way to configure ember-cli to merge all files in app/styles (and subdirectories) into one app.css file (and then fingerprint etc)? Is this possible via the Brocfile or do I need to override EmberApp.styles()?
Update: As far as I can see there are the following (not very elegant) options:
1) Using SASS and #import the CSS files into app.scss individually. The downside of this is that I need to use an extra plugin (SASS) and that SASS does not seem to allow for globbing patterns in #import (e.g. #import('**/*.scss')), which makes this solution cumbersome for big projects.
2) Overriding EmberApp.styles() such that it does not copy the CSS files (this is currently being done by a wrapper around broccoli-static-compiler) and configuring Broccoli such that it concatenates the css files into app.css. This solution seems a bit hacky though and there is a risk of incompatibility with newer versions of ember-cli.
3) Workaround: Use broccoli-funnel and broccoli-concat to do the concatenation yourself.
In Brocfile.js:
var appTree = app.toTree()
var concatenated = concat(appTree, {
inputFiles: [
'**/*.css'
],
outputFile: '/assets/app.css',
});
module.exports = mergeTrees([appTree, concatenated], { overwrite: true });
This will create a new app.css with all our concatenated CSS in /assets/app.css.However, this file not fingerprinted. Our assets directory now looks something like this:
/assets/app.css
/assets/app-<fingerprint>.css
So a - admittedly hacky - second step is to 1) get the filename of the fingerprinted app-<fingerprint>.css, 2) delete app-<fingerprint>.css and 3) rename app.css to app-<fingerprint>.css. This last step can be automated using Grunt or gulp.
Personally, I think SCSS would be the way to go. It is the simplest solution and there are other advantages to using SCSS besides importing things into one file (variables for repeated patterns, for example).
In addition, manually adding files allows the developer to configure exactly where each piece of code is included. I don't see that as a con, but I can understand the other point of view and I've definitely spent 5m trying to figure out why my styles weren't being applied until I realized it was because I didn't include a new style file.
Edit: There are some node-sass globbing solutions available that could probably be added if that is a big show stopper.

Referencing asset in javascript

The Ember CLI guide describes how assets can be referenced in the templates and the CSS - but what is the proper way of referencing an asset (say an image) from my javascript code?
Specifically I am concerned about the asset path getting fingerprinted correctly when building assets for production. It seems like ember-cli is using broccoli-asset-rev for this, but according to its documentation, only <script> tags in the HTML and url() in CSS will be fingerprinted. Is there any way (probably through another broccoli plugin) to get asset paths in the .js files fingerprinted, too?
I placed an image called car.jpeg under public/assets/images and then was able to reference it in my application.js route file as assets/images/car.jpeg
Works great
UPDATE
One picture is worth a thousand words... :)
I found the issue. This works out of the box as expected - it turned out that my asset (image) was not in the right location, so occurrences of it's path in the JS files never got replaced with the fingerprinted version.

font-awesome-rails gem not loading pre compiled fonts

I am using the font-awesome-rails gem within my app and all is working in development, however when deployed in production the fonts do not show. I have tried browsing /assets/fontawesome-webfont.eot on the production site and get a 404 not found error. Looking on the server I can see the fonts are pre-compiled with a different name - e.g. /assets/fontawesome-webfont-e732c0065276ad722bded99096afaa19.eot
I have the
*= require font-awesome
line in my application.css file and when looking at the compiled css file can see it included:
Font Awesome 4.2.0 by #davegandy - http://fontawesome.io - #fontawesome
* License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License)
*/#font-face{font-family:'FontAwesome';src:url("/assets/fontawesome-webfont.eot?v=4.2.0");src:url("/assets/fontawesome-webfont.eot?#iefix&v=4.2.0") format("embedded-opentype"),url("/assets/fontawesome-webfont.woff?v=4.2.0") format("woff"),url("/assets/fontawesome-webfont.ttf?v=4.2.0") format("truetype"),url("/assets/fontawesome-webfont.svg?v=4.2.0#fontawesomeregular") format("svg");
The problem seems to be the difference in filenames between the file in the assets folder and the css call
I don't know how to call these font files within the CSS as the file name changes every time they are pre-compiled.
What is happening is Rails is giving your assets (in this case, the font file) a unique name by adding an MD5 hash to the filename. When you update an asset, this will ensure the users' browsers do not cache the old file.
As you've observed, the CSS file is not using the unique name. This is because Rails doesn't know to update that reference.
There are several approaches you can use; I'll cover two here.
Use an Asset Helper
In order to do this, you will need to rename your static CSS file to a ERB file (i.e., rename stylesheet.css to stylesheet.css.erb). Then in the font reference:
#font-face{
font-family:'FontAwesome';
src:url("/assets/fontawesome-webfont.eot?v=4.2.0");
src:url("/assets/fontawesome-webfont.eot?#iefix&v=4.2.0") format("embedded-opentype"),
url("/assets/fontawesome-webfont.woff?v=4.2.0") format("woff"),
url("/assets/fontawesome-webfont.ttf?v=4.2.0") format("truetype"),
url("/assets/fontawesome-webfont.svg?v=4.2.0#fontawesomeregular") format("svg");
You will use the asset_path helper:
url(<%= asset_path 'fontawesome-webfont.woff' %>)
url(<%= asset_path 'fontawesome-webfont.ttf' %>)
url(<%= asset_path 'fontawesome-webfont.svg' %>)
During runtime, rails will handle the unique names and insert the correct value in the CSS.
Use a CDN
For some common assets, like some fonts, jQuery, etc, using a CDN might make sense. With your external reference, Rails will not rename the file and your users may gain the benefits of caching.
When I search "fontawesome cdn", I get a link to a CDN that hosts both the CSS and the font files.

Django Bootstrap Toolkit CSS editing

I am using Django Bootstrap toolkit, and I am wondering where the CSS and JS files are stored so I can edit them.
Take a look at the example project and templatetags:
BOOTSTRAP_BASE_URL = 'http://twitter.github.com/bootstrap/assets/'
BOOTSTRAP_CSS_BASE_URL = BOOTSTRAP_BASE_URL + 'css/'
BOOTSTRAP_CSS_URL = BOOTSTRAP_CSS_BASE_URL + 'bootstrap.css'
BOOTSTRAP_JS_BASE_URL = BOOTSTRAP_BASE_URL + 'js/'
So you can define your own version of the files but per default django-bootstrap-toolkit embeds the files directly from the Github repository.
However, in most cases it is better to leave the original files alone and overwrite the necessary declarations in your own stylesheets. Check this answer to see how to do this.