Sharing JavaScript files between projects

Hello guys, i have the following problem (simplified for clarity):
file1.js is a file i want to use in my project, the catch here is that i don't want to add this file i want to share this file between different projects (like a library)
I don't want to have different copies of this file for each project that would be using it, i have however fiddle with the javascript library configuration under project settings and added my file as an library
i don't actually know if this is what i need and i found no way to referencing it inside a script tag on my project <script src="????" type="text/javascript"></script> how to do this?

6 comments
Comment actions Permalink

Hi,

Just to clarify - how would this file be served by web server if does not reside under a web root of one of the projects?

Kirill

0
Comment actions Permalink

yes that would be part of the problem,  i was wondering if some sort of link could be created between the file and the project where that file is copied to some directory inside the project automatically,  and re-copied upon any changes to the original file

0
Comment actions Permalink

That's a good question. I've wanted to do that before myself, but with JS, I have found no reasonable way to make it work.

0
Comment actions Permalink

Symbolic link anyone?

P.S.
This works perfectly fine with folders, with symlinks on files -- it may be "unlinked" when editing such file (depends on your settings, e.g. "safe write") -- have not tested files myself as there was no need (I'm always organizing my extra stuff in folders).

0
Comment actions Permalink

OK, so in the end one first needs to choose the way to make it work for the web server. Then we can discuss how can we support this in the IDE.

0
Comment actions Permalink

One way would be to actually link it as absolute URL from a server you use to serve shared resources. But that has its downfalls during development since you would have to be online because symlinks cannot be served (at least on my Apache/Win) and you would have to be pushing all changes to the server.

Other way, which I chose, is to do a little scripting. Place your shared resources somewhere in your file system, I use "apps" directory, which I created to be a sibling to my www (document root). Let's say you have

/apps/mypage/index.js

Now PHP (or other server side script) has the access to your shared file and you just need to somehow load it into the website - two options:

A) set script src to "serve-mypage-index.php", which which just reads the content of index.js and outputs it. (don't forget to set content type)

B) Implement your little simple package manager. Somewhere on the page, that needs the shared resources, include you package manager script/class and let it check appropriate resources (e.g. MyAwesomePackageManager::updateResources('mypage/index.js') ), which checks if new files appeared in the shared resources and if so, copy them inside your js folder. If you have some other way to watch for changes in shared directory or don't mind manually executing a script, you don't have to include the manager in every page.

0

Please sign in to leave a comment.