将多个 JavaScript 文件合并为一个 JS 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5511989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:33:01  来源:igfitidea点击:

Combine multiple JavaScript files into one JS file

javascriptjqueryjquery-ui

提问by Chandrasekhar

I am using jquery in my web application and I need to load more jquery script files into a single page.

我在我的 Web 应用程序中使用 jquery,我需要将更多 jquery 脚本文件加载到单个页面中。

Google suggested I combine all the jquery script files into a single file.

Google 建议我将所有 jquery 脚本文件合并到一个文件中。

How can I do this?

我怎样才能做到这一点?

采纳答案by dfsq

On linux you can use simple shell script https://github.com/dfsq/compressJS.shto combine multiple javascript files into the single one. It makes use of the Closure Compiler online service so the resulting script is also effectively compressed.

在 linux 上,您可以使用简单的 shell 脚本https://github.com/dfsq/compressJS.sh将多个 javascript 文件合并为一个。它利用了 Closure Compiler 在线服务,因此生成的脚本也被有效地压缩。

$ ./compressJS.sh some-script.js another-sctipt.js onemore.js

回答by Prydie

Just combine the text files and then use something like the YUI Compressor.

只需组合文本文件,然后使用类似YUI Compressor 的东西。

Files can be easily combined using the command cat *.js > main.jsand main.js can then be run through the YUI compressor using java -jar yuicompressor-x.y.z.jar -o main.min.js main.js.

文件可以使用命令容易地组合cat *.js > main.js和main.js然后可以通过YUI压缩机使用中运行java -jar yuicompressor-x.y.z.jar -o main.min.js main.js

Update Aug 2014

2014 年 8 月更新

I've now migrated to using Gulpfor javascript concatenation and compression as with various plugins and some minimal configuration you can do things like set up dependencies, compile coffeescript etc as well as compressing your JS.

我现在已经迁移到使用Gulp进行 javascript 连接和压缩,就像使用各种插件和一些最小配置一样,您可以执行诸如设置依赖项、编译 coffeescript 等以及压缩 JS 之类的操作。

回答by Gary Green

You can do this via

你可以通过

  • a. Manually:copy of all the Javascript files into one, run a compressor on it (optional but recommended) and upload to the server and link to that file.
  • b. Use PHP:Just create an array of all JS files and includethem all and output into a <script>tag
  • 一种。手动:将所有 Javascript 文件复制到一个,在其上运行压缩器(可选但推荐)并上传到服务器并链接到该文件。
  • 湾 使用 PHP:只需创建一个包含所有 JS 文件的数组并将include它们全部输出到一个<script>标签中

回答by Gary Green

I usually have it on a Makefile:

我通常把它放在一个Makefile

# All .js compiled into a single one.
compiled=./path/of/js/main.js

compile:
    @find ./path/of/js -type f -name "*.js" | xargs cat > $(compiled)

Then you run:

然后你运行:

make compile

I hope it helps.

我希望它有帮助。

回答by eloone

I use this shell script on Linux https://github.com/eloone/mergejs.

我在 Linux https://github.com/eloone/mergejs上使用这个 shell 脚本。

Compared to the above scripts it has the advantages of being very simple to use, and a big plus is that you can list the js files you want to merge in an input text file and not in the command line, so your list is reusable and you don't have to type it every time you want to merge your files. It's very handy since you will repeat this step every time you want to push into production. You can also comment files you don't want to merge in the list. The command line you would most likely type is :

与上面的脚本相比,它的优点是使用起来非常简单,而且一个很大的优点是你可以在输入文本文件中而不是在命令行中列出你想要合并的js文件,所以你的列表是可重用的和您不必每次要合并文件时都键入它。这非常方便,因为您每次想要投入生产时都会重复此步骤。您还可以在列表中注释不想合并的文件。您最有可能输入的命令行是:

$ mergejs js_files_list.txt output.js

And if you want to also compress the resulting merged file :

如果您还想压缩生成的合并文件:

$ mergejs -c js_files_list.txt output.js

This will create output-min.jsminified by Google's closure compiler. Or :

这将创建output-min.js由谷歌的闭包编译器缩小的。或者 :

$ mergejs -c js_files_list.txt output.js output.minified.js

If you want a specific name for your minified file named output.minified.js

如果你想为你的缩小文件命名一个特定的名称 output.minified.js

I find it really helpful for a simple website.

我发现它对一个简单的网站很有帮助。

回答by SasHok.Tk

Copy this script to notepad and save as .vbs file. Drag&Drop .js files on this script.

将此脚本复制到记事本并另存为 .vbs 文件。在此脚本上拖放 .js 文件。

ps. This will work only on windows.

附:这仅适用于 Windows。

set objArgs = Wscript.Arguments
set objFso = CreateObject("Scripting.FileSystemObject")
content = ""

'Iterate through all the arguments passed
for i = 0 to objArgs.count  
    on error resume next
    'Try and treat the argument like a folder
    Set folder = objFso.GetFolder(objArgs(i))
    'If we get an error, we know it is a file
    if err.number <> 0 then
        'This is not a folder, treat as file
        content = content & ReadFile(objArgs(i))
    else
        'No error? This is a folder, process accordingly
        for each file in folder.Files
            content = content & ReadFile(file.path)
        next
    end if
    on error goto 0
next

'Get system Temp folder path
set tempFolderPath = objFso.GetSpecialFolder(2)
'Generate a random filename to use for a temporary file
strTempFileName = objFso.GetTempName
'Create temporary file in Temp folder
set objTempFile = tempFolderPath.CreateTextFile(strTempFileName)
'Write content from JavaScript files to temporary file
objTempFile.WriteLine(content)
objTempFile.Close

'Open temporary file in Notepad
set objShell = CreateObject("WScript.Shell")
objShell.Run("Notepad.exe " & tempFolderPath & "\" & strTempFileName)


function ReadFile(strFilePath)
    'If file path ends with ".js", we know it is JavaScript file
    if Right(strFilePath, 3) = ".js" then       
        set objFile = objFso.OpenTextFile(strFilePath, 1, false)
        'Read entire contents of a JavaScript file and returns it as a string
        ReadFile = objFile.ReadAll & vbNewLine
        objFile.Close
    else
        'Return empty string
        ReadFile = ""
    end if  
end function

回答by Capsule

Script grouping is counterproductive, you should load them in parallel using something like http://yepnopejs.com/or http://headjs.com

脚本分组适得其反,您应该使用类似http://yepnopejs.com/http://headjs.com 的内容并行加载它们

回答by Jim Blackler

You can use the Closure-compiler as orangutancloud suggests. It's worth pointing out that you don't actually needto compile/minify the JavaScript, it ought to be possible to simply concatenate the JavaScript text files into a single text file. Just join them in the order they're normally included in the page.

您可以按照 orangutancloud 的建议使用 Closure-compiler。值得指出的是,您实际上并不需要编译/缩小 JavaScript,应该可以简单地将 JavaScript 文本文件连接成一个文本文件。只需按照它们通常包含在页面中的顺序加入它们即可。

回答by technoTarek

If you're running PHP, I recommend Minifybecause it does combines and minifies on the fly for both CSS and JS. Once you've configured it, just work as normal and it takes care of everything.

如果您正在运行 PHP,我推荐Minify,因为它可以对 CSS 和 JS 进行动态组合和缩小。配置完成后,就可以正常工作了,它会处理所有事情。