Reputation: 16412
I'm working on a PHP class that takes my JS assets and builds a minified and concatenated version to a one script file with all the assets inside ready for production. My only concern is that it may break something. For example, if the build concatenates a file after it's needed (for example, jQuery core after a jQuery plugin). I intend it to be the most automatic that I can (I was thinking reading the scripts directory), but this could lead some problems.
I also saw Buildr and alse seems a great solution. It does the same, builds everything that it's inside a directory, but maybe it has the same problem?
Any solution for this? Something like wrapping JS code somehow for this?
Upvotes: 0
Views: 73
Reputation: 6602
Why are you worried about concatenation order? If, in fact (jQuery, for example) were properly defined in the file, it does not matter what order they are concatenated in (assuming they constitute one file). This is because Javascript allows you to run code in the current file that the interpreter has not been evaluated yet. Rather any unresolved symbols are looked up in the global object when they are called (which presumably happens after the code has been parsed)
Consider the following:
dosomething(); function dosomething(){ console.log('yup'); }
dosomething has not been evaluated prior to calling it, but the interpreter can still see it.
Upvotes: 1
Reputation: 11134
What you need is dependency management. One of your possibility is to work with Google Closure Compiler. It provides dependency detection when you compress your scripts (See this). However for it to be working properly, you need to use their library which can be annoying if you are working with a projet that has a good size and isn't using it yet.
Upvotes: 0