Javascript proxies and minification questions

Hi there,

First off, I really really think Middleman is great. I’ve come from a Wordpress PHP background and it’s what coding should have always been like!

I’m working on a specific project at the mo that involves outputting the build in such a way devs can use the output for other projects. Probably not the typical use case I know. For this I would like to be able to duplicate an entire folder of javascript files into another folder. Is this possible. I’ve read about file proxies but that only seems to work with other files.

I would also like to be able to minify on demand separate js assets with the .min. I asked on github but it’s not an issue with Middleman as such I guess so I’m sorry if I’m repeating it here.

Thanks alot

I’m working in a similar environment right now, the build folder is used for the backend integrator as templates. It’s quite a bit of javascript in there (written in Coffeescript) so they asked for a non-minified, non-concatenated copy in a separate folder to get a better overview.
I have the coffeescript files lined up in a folder and use sprockets to concatenate them all, the coffeescipt files all start with a “", so Middleman is ignoring them.
In my case I just removed the "
” prefix from my coffeescript assets and I had a separate folder in build with all the original js modules in it.
I’m not sure if this is helpful as I don’t know your exact use-case for that extra JS folder.

P.S.: Middleman also has an “after_build” hook, used by e.g. middleman-sync, but I’ve never utilized it for anything.

P.P.S: For the same project, I wrote a little helper to post-process my assets. It was a requirement for the client all assets come with tabs instead of spaces (gasp!).

require 'find'

module ConvertToTabs

  class << self

    def registered(app)
      app.after_build do |builder|
        Find.find('build/') do |item|
          next if item == '.' or item == '..'

          if File.extname(item) == '.html' || File.extname(item) == '.css' || File.extname(item) == '.js'
            content = File.open(item, "r") { |f| f.read }
            content = content.gsub(/^( {2})+/) { |spaces| "\t" * (spaces.length / 2) }
            File.open(item, "w+") { |f| f.write(content) }
          end

        end
      end
    end

    alias :included :registered
  end

end

::Middleman::Extensions.register(:convert_to_tabs, ConvertToTabs)

It’s activated in config.rb like this:

require 'helpers/convert_to_tabs.rb'

configure :build do
  [...]
  activate :convert_to_tabs
end

I would like to be able to duplicate an entire folder of javascript files into another folder.

Can you give a more concrete example?

Sure,

WRT the Javascript, they all reside in a folder called “scripts”. On build, I am attempting to create 2 file structures for use in different projects so they have to be separate. In order for me to keep track of all the many js files, I was hoping there would be a way to include the javascript file structure in “scripts” to 2 places.

The closest I seem to have got is with using "after_build:

after_build do

File.rename(“build/scripts/vendor/etc/etc/example.js”, “build/scripts/vendor/etc/etc/example2.js”)

This does’t do it obviously but I thought there may be something there that may be applicable.

Okay, let’s see if I have this right. We have some kind of structure that looks like this:

source
  - scripts
         - example.js
         - another.js

And what you’d like to do is end up with is something like:

build
  - alpha
         - example.js
         - another.js
  - beta
         - example.js
         - another.js

Is that about right?

Hi Aupajo,

Yes that’s exactly it. Just realised how much easier it would have been if I had explained it like that. Many thanks for your help!

Michael

Thanks @thomasklokosch for your reply. My use case is that I need to be able to have multiple javascript folders which import from a common location. I have found out I can use the

lib/assets/javascripts/
location to store all js assets, unfortunately I can’t get mm to recognise 2 js locations, so I was thinking if I can duplicate them instead. Ideally I want to have free reign on where my js assets go, as there are multiple folder structures that need to be built on build. I have tried in the config.rb to set 2 asset paths but this seems to only work if you are importing to the specified js directory. I have also tried leaving the js_dir empty but this seems to do break the build path completely.

I’m at a loss what to do now. I’ve spent hours and hours researching this and it all seems to be to do with the sprocket paths, but I’m a bit of a newb!!!

@ottobyte I feel your pain. I’ve been there too. I’d like to see Middleman’s Sprockets integration improved so these things are easier to at least determine, much less achieve.

However, in this case, I’m fairly confident you could pull off this effect using manipulate_resource_list. Check out the docs on Custom Extensions.