0

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?

Alejandro García Iglesias
  • 16,222
  • 11
  • 51
  • 64

2 Answers2

1

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.

dwerner
  • 6,462
  • 4
  • 30
  • 44
  • This is simply not viable whenever you start using namespace, which is quite common in any non-trivial javascript project. – HoLyVieR May 10 '12 at 21:33
  • I agree, but it was a criticism of the OP's own requirements - how he describes his 'concatenating PHP class' to work. The idea is to point out that the thinking around how JS might 'break' was incorrect. – dwerner May 10 '12 at 21:33
  • Namespacing is not a native feature of JavaScript (yet) anyway, unless you are further touting Closure compiler. – dwerner May 10 '12 at 21:35
  • @GarciaWebDev Namespaces are usually defined like `window.MyNamespace = {}` and when you want to add something to that namespace you do `MyNamespace.MyObject = 'something'`. When you use something like that your function won't be hoisted and you need to define your function before using it. – HoLyVieR May 10 '12 at 21:36
  • Thats an Object, not a namespace. – dwerner May 10 '12 at 21:37
  • @dwerner: So I can have `$.fn.myPlugin = ...` before jQuery in the same script? Sounds that I don't have to worry for that. But I am using namespaces for my modules code. – Alejandro García Iglesias May 10 '12 at 21:37
  • Edit: yes, that is what I am saying. – dwerner May 10 '12 at 21:39
  • @dwerner thanks. I confirm I'm using "namespaces" (or whatever name) for my code, so I'm still with trouble... I have all my objects inside window. – Alejandro García Iglesias May 10 '12 at 21:41
  • What exactly do you mean by namespaces? There is no such native feature in JavaScript (yet, anyway.); Holyvier is mis-naming object literals as 'namespaces' here. – dwerner May 10 '12 at 21:42
  • Sorry, you're right, I (and @HoLyVieR I believe) meant 'object namespacing' (just to put it a name) using objects. Like `window.MyModule = Object.create({ my: prototype });`. See fiddle: http://jsfiddle.net/eKAwH/ – Alejandro García Iglesias May 10 '12 at 21:46
  • @dwerner It's not misleading, that's just how it's commonly called. You can see this : http://stackoverflow.com/questions/881515/javascript-namespace-declaration – HoLyVieR May 10 '12 at 21:46
  • A pattern is not a language feature. These are objects. – dwerner May 10 '12 at 21:47
0

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.

HoLyVieR
  • 10,985
  • 5
  • 42
  • 67