I was wondering how big companies tend to modularize components on their page. Facebook is a good example:
There\'s a team working on Search that has it
There is a lot to be said for CSS Frameworks like Shaun Inman's CSScaffold. It lets you subdivide stylesheets, define constants, easily handle local contexts, etc. It doesn't solve your problem, but it can be a big help.
On the Javascript side, it is possible to work with local contexts, although I've rarely if ever run into problems.
Naming of HTML elements is definitely tricky, but HTML 5 should help (fewer id's and classes are necessary) and precompilation as you mention can be another option.