admin管理员组

文章数量:1426066

New to JavaScript and am wondering if all JavaScript has to be "deployed" as individual .js files, or if there are ways to bundle/package multiple JS files as a ponent, like a Java .jar or a .NET .dll.

For instance, if I have a collection of, say, 30 JS files that make up a reusable library of JS objects and functions, is there a way to package these as a single deployable ponent or am I stuck copying-n-pasting all 30 files into every project where I want to use them?

What's the norm here? Thanks in advance!

New to JavaScript and am wondering if all JavaScript has to be "deployed" as individual .js files, or if there are ways to bundle/package multiple JS files as a ponent, like a Java .jar or a .NET .dll.

For instance, if I have a collection of, say, 30 JS files that make up a reusable library of JS objects and functions, is there a way to package these as a single deployable ponent or am I stuck copying-n-pasting all 30 files into every project where I want to use them?

What's the norm here? Thanks in advance!

Share Improve this question asked Mar 21, 2012 at 21:15 IAmYourFajaIAmYourFaja 57k186 gold badges489 silver badges778 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

2018 update: You can use CommonJS or ES modules and package them with an appropriate tool like Browserify, webpack, Parcel, etc.

JavaScript can't be piled (before sending it to the client) of course; the closest equivalent is just to minify them, then glue all the files together. For minification, I personally like Google Closure Compiler; as for the gluing, it's pretty easy to do with your mand-line tools. For example:

cat script1.js >> all.js
cat script2.js >> all.js
# etc.

If you plan to use all the content of all the files, you could consolidate their contents into a single js file using a text editor. Having 30 separate .js files will slow down a page load since 30 requests have to be made. You can further reduce the file size by using a JavaScript "minifier", such as http://jspress./ or http://www.minifyjavascript./.

Yes.

It's called namespace.
This is one explanations blog about it.
This is a nice tutorial .

... best you can hope for is to minify, bine into one file, maybe look into an automatic build system for it. Hover, when you go to build that single file, be very careful, because the pression/minification process can definitely break stuff. Consider running the sourcefiles through jsLint. Plan on spending a good deal of time on testing.

Although Javascript is an interpreted language, Google claims that it CAN be piled. This is what their closure piler does. It's a bit different than minimization because the code is also optimized for faster execution.

本文标签: Is there a JavaScript equivalent to a Java Jar or NET DLLStack Overflow