如何将 JavaScript 文件连接成一个文件?
我想为我的网站创建一个已编译的 JavaScript 文件。 对于开发,我更愿意将 JavaScript 保存在单独的文件中,并且作为我的自动化脚本的一部分,将文件连接成一个文件并对其运行压缩器。
我的问题是,如果我使用旧的 DOS 复制命令,它还会放入压缩器抱怨的 EOF 标记:
copy /A *.js generated.js /Y
其他人在做什么?
I want to create a compiled JavaScript file for my website. For development I would prefer to keep the JavaScript in separate files and just as part of my automated scripts concatenate the files into one and run the compressor over it.
My problem is that if I use the old DOS copy command it also puts in the EOF markers which the compressor complains about:
copy /A *.js compiled.js /Y
What are other people doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我建议使用 Apache Ant 和 YUI Compressor。
http://ant.apache.org/
http://yui.github.com/yuicompressor/
将类似的内容放入 Ant 构建 xml 中。
它将创建两个文件:application.js 和 application-min.js。
I recommend using Apache Ant and YUI Compressor.
http://ant.apache.org/
http://yui.github.com/yuicompressor/
Put something like this in the Ant build xml.
It will create two files, application.js and application-min.js.
要在没有 EOF 的情况下进行复制,请使用二进制模式:
如果生成的文件仍然具有 EOF,则该文件可能来自原始文件之一,
它可以通过以下变体修复:
/A 从原始文件中删除尾部 EOF(如果有),/B 防止将 EOF 附加到结果文件中。 如果 EOF 不在末尾,则源文件将在此处被截断。 开关的顺序很重要。 如果您编写
- 源文件中的 EOF 将不会被删除,但仍然不会附加生成的 EOF。
自己尝试一下,这就是我得到的。 DOS命令很奇怪。
To copy without EOF use binary mode:
If the resulting file still has EOFs, that might have come from one of original files,
it can be fixed by this variant:
/A removes trailing EOFs from original files if any and /B prevents appending EOF to the resulting file. If an EOF is not at the end, the source file will be truncated at it. The order of switches is important. If you write
- EOFs in source files won't be removed but still resulting EOF won't be appended.
Try it yourself, thats where I get it. DOS commands are weird.
在 asp.net AJAX 中,您可以使用“CompositeScript”标签。 这会将所有脚本合并到 1 个大 js 文件中,通过减少 http 304 和可能的 http 401 的数量来节省带宽。
示例:
有关更多信息,请参见此处:
http://msdn.microsoft.com/en-us/library/cc488552.aspx
In asp.net AJAX, you can use the 'CompositeScript' tag. This will combile all your scripts into 1 big js file, saving bandwidth by reducing the number of http 304s and possibly http 401s.
Sample:
For more info, see here:
http://msdn.microsoft.com/en-us/library/cc488552.aspx
这是一个非常老的问题,但我想提一下,还有一些方法可以使用 javascript 连接 javascript! 显然是用nodejs...例如,有一些工具发布为npm模块,例如
这个 还有grunt 和 gulp 插件也是如此。
我还想提一下一种非常非常有趣的技术,该技术正在 jQuery 和 Modernizr 等大型项目中使用。 这两个项目都是完全使用 requirejs 模块开发的,然后他们使用 requirejs 优化器 作为一个非常好的解决方案。智能串联器。 有趣的是,正如您所看到的,jQuery 和 Modernizr 都不需要 requirejs 才能工作,而发生这种情况是因为它们删除了 requirejs 语法仪式,以便摆脱代码中的 requirejs。 因此,他们最终得到了一个使用 requirejs 模块开发的独立库! 因此,除了其他优势之外,他们还能够对其库执行精简构建。 这里是一篇博客文章,解释了所有内容这个更详细。
This is a very old question, but I want to mention that there are also ways to concatenate javascript using javascript! with nodejs obviously... For example there are tools published as npm modules like
this and there are also grunt and gulp plugins too.
I also want to mention a very, VERY, interesting technique that is being used in huge projects like jQuery and Modernizr. Both of this projects are entirely developed with requirejs modules and then they use the requirejs optimizer as a very smart concatenator. The interesting thing is that, as you can see, neither jQuery nor Modernizr needs on requirejs to work, and this happen because they erase the requirejs syntatic ritual in order to get rid of requirejs in their code. So they end up with a standalone library that was developed with requirejs modules!. Thanks to this they are able to perform cutsom builds of their libraries, among other advantages. Here is a blog post that explains all this more in detail.
在你的机器上安装压缩器uglifyjs:
然后可以使用以下命令连接并压缩所有js文件。
Install the compressor uglifyjs on your machine:
Then the following command can be used to concatenate and compress all js files.
我们创建了一个由以下部分组成的机制:
它可能无法满足您的需求,但为了回答您的问题询问其他人在做什么,其工作原理如下:
/css.aspx?package=core
例如声明
包“core”包含文件
/js/mootools.js 和
/js/swfobject.js)
开发环境我们没有
想要缩小的 js 内容是
服务出来了,而是写下
原始文件。 对于js来说这是完成的
通过脚本的 document.writes
包含,对于 css,我们编写导入规则。
所有这些都被分解为单独的服务。 jsminificationwriter 服务中注入了一个缓存服务。 这利用了仅处理缩小规则的原始缩小服务。
这种方法的好处是:
希望这可以帮助。 如果您愿意,我可以发布一些代码片段来进一步说明。
We have created a mechanism consisting of the following parts:
It may be too much for your needs, but as to answer your question what others do, here is how it works:
/css.aspx?package=core
for instance declares that the
package "core" contains the files
/js/mootools.js and
/js/swfobject.js)
development environment we don't
want the minified js contents to be
served out, but instead write the
original files. For js this is done
by document.writes of script
includes, and for css we write import rules.
All this is broken up in separate services. There is a cache service injected in the jsminificationwriter service. This makes use of the original minificationservice that solely takes care of the minification rules.
What's nice in this approach is:
Hope this helps. I can post some code fragments to illustrate it more if you like.
您还可以这样做:
You can also do:
我会第二个 yuicompressor,但我使用 /packer/
http://johannburkard.de/blog/programming/javascript/automate-javascript-compression-with-yui-compressor-and-packer.html
这对我来说真的非常好。
I'll second yuicompressor, but I use /packer/
http://johannburkard.de/blog/programming/javascript/automate-javascript-compression-with-yui-compressor-and-packer.html
It's been really excellent for me.
我知道这是一个非常老的问题,但为了完整起见,我将提到使用 Browserify 的选项。 它允许您使用 NPM 的
require
函数将项目构建为不同的模块来解决依赖关系,然后解决这些依赖关系并将整个项目连接到一个文件中。例如,假设您的项目名为 FooBar,并且您想要输出名为
foobar.js
的文件。 您将创建一个main.js
文件作为项目的入口点,需要包含所有应包含的模块。main.js
然后运行:
或者,要在每次源文件更改时自动执行此操作,您也可以使用 Watchify。
Browserify 还将解决模块之间的依赖关系。 例如,如果 doBar.js 依赖于
doQux.js
...doBar.js
那么 Browserify 将确保包含
doQux.js
到foobar.js
中,这样您就不会出现任何损坏的依赖关系。I know this is a very old question, but for the sake of completeness I'll mention the option of going with Browserify. It allows you to build your project as different modules using NPM's
require
function to resolve dependencies, and then it resolves those dependencies and concatenates your entire project into an single file.For example, say your project is called FooBar and you want to output a file called
foobar.js
. You'd create amain.js
file as the entry point for the project, requiring all the modules that should be included.main.js
Then run:
Or, to do it automatically every time a source file changes you could also use Watchify.
Browserify will also resolve dependencies between modules. So for example, if doBar.js depends on
doQux.js
...doBar.js
Then Browserify will make sure to include
doQux.js
intofoobar.js
so that you won't have any broken dependencies.您还可以尝试wro4j(java的Web资源优化器),它可以用作构建工具(maven插件)、运行时解决方案(使用过滤器)或命令行工具。 它允许您轻松地组织资源并使用十几个机器人类型资源的压缩器为您处理合并:js 和 css。
定义要合并的资源很简单:
免责声明:我是该项目的提交者。
You can also try wro4j (web resource optimizer for java), which can be used as a build tool (maven plugin), runtime solution (using filter) or command line tool. It allows you to easily keep resources organized and handle the merging for you using a dozen of compressors for resources of bot types: js and css.
Defining resources to merge is easy as:
Disclaimer: I'm a commiter of this project.
这就是我使用 glob 解决相同任务的方法:
This is how I resolved the very same task using glob: