Sprockets: How to use append_path

I’m using the following line in config.rb:

set :js_assets_paths, ['modules', 'assets/vendor']

However, this seems to be deprecated:

:js_assets_paths is deprecated. Call sprockets.append_path instead.

How do I use this inside config.rb? Is sprockets exposed there somehow?

Yes, if your version of middleman-sprockets is up to date, it’s available as: sprockets.

I believe you can only append one path at a time, but I may be wrong. I’d also use the full path, but I’m paranoid.

sprockets.append_path "#{root}/modules"
sprockets.append_path "#{root}/assets/vendor"

If I put this in config.rb I get the following error:

config.rb:44:in `initialize': undefined local variable or method `sprockets' for #<Middleman::Application::MiddlemanApplication1:0x10f974bd0> (NameError)

I’m using middleman-sprockets (3.0.11)

EDIT: It’s the same for middleman-sprockets (3.1.1)

Try putting it in an after_configuration block:

after_configuration do
  sprockets.append_path "#{root}/modules"  
  sprockets.append_path "#{root}/assets/vendor"  
end

Thanks a lot, that did the trick! However, I had to omit “#{root}/”:

after_configuration do
	sprockets.append_path "modules" 
	sprockets.append_path "assets/vendor" 
end

Hi guys, I’m really struggling with this, I would really appreciate someone’s help on this.

I have set up a location for scss and js: lib/assets/javascripts and lib/assets/stylesheets respectively . These both reside in the root of the project, not the source folder. This is because I need to have a dumping ground for the tons of scripts that may be needed but not every time.

From my intensive newb research on this the lib/assets/ dir is a location Middleman picks up automatically (correct me if I’m wrong!) In my config.rb file my sass dir is set :sass_dir, 'stylesheets' within the source folder and this imports scss from the lib location perfectly. My js is set to set :js_dir, 'scripts' and even with the above info, I have tried so many things and I cannot appear to get the require bit right.

To give an example, I have a file in /scripts in the source folder called test.js. I would like to be able to import a js file from the lib/assets/javascripts location. I have tried adding ../../../file/path etc and that errors and is a bit unmaintainable. It was my understanding that sprockets picks up a file anywhere within the structure defined by the location set in config.rb along with any further paths appended.

Please, please, pretty please can someone help me understand this. I’ve been searching forums and info for days on this.

Yours hopefully!

Michael

Hi Michael

I just set up a new project (using Middleman 3.1.5) and was able to import both a script file and a stylesheet:

  • To import lib/assets/javascripts/test.js: //= require "test.js"
  • To import lib/assets/stylesheets/test.scss: @import "test";

If this does not work I’d suggest posting your complete config.rb as well as the Gemfile to allow for debugging.

Best regards,
Thomas

Thanks Thomas for your reply, OK I’m going to look at upgrading to 3.1.5 - I’m on 3.1.4 before I do anything else. Maybe that will fix this…

Kind regards

Michael

Thank you very much Thomas!! It was the version. 3.1.5 has fixed this. What a relief, I was tearing my hair out!

This works for me

sprockets.append_path File.join “#{root}”, “bower_components/bootstrap-sass/lib”

I do not need to use the after configuration either

I can’t move past v3.4.0 because of this.