我的问题是,如果我使用旧的DOS复制命令,它会插入压缩程序投诉的EOF标记:
copy /A *.js compiled.js /Y
其他人都在做什么?
我建议使用Apache Ant和YUI Compressor。
http://yui.github.com/yuicompressor/
在Ant构建xml中添加以下内容,它将创建两个文件application.js和application-min.js。
<target name="concatenate" description="Concatenate all js files">
<concat destfile="build/application.js">
<fileset dir="src/js" includes="*.js" />
</concat>
</target>
<target name="compress" depends="concatenate" description="Compress application.js to application-min.js">
<apply executable="java" parallel="false">
<filelist dir="build" files="application.js" />
<arg line="-jar" />
<arg path="path/to/yuicompressor-2.4.2.jar" />
<srcfile />
<arg line="-o" />
<mapper type="glob" from="*.js" to="build/*-min.js" />
<targetfile />
</apply>
</target>
若要无 EOF 复制,请使用二进制模式:
copy /B *.js compiled.js /Y
copy /A *.js compiled.js /B /Y
/A 会从原始文件中删除任何尾随的 EOF,而/B 则防止将 EOF 添加到结果文件。如果 EOF 不在结尾,则源文件将被截断。开关的顺序很重要。
copy /A *.js /B compiled.js /Y
源文件中的EOF不会被删除,但生成的EOF也不会被添加。
你可以自己试试,这就是我得到的。DOS命令很奇怪。
<asp:ScriptManager ID="ScriptManager1" runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Path="~/Scripts/Script1.js" />
<asp:ScriptReference Path="~/Scripts/Script2.js" />
<asp:ScriptReference Path="~/Scripts/Script3.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
我还想提到一种非常、非常有趣的技术,它被用于像jQuery和Modernizr这样的大型项目中。这两个项目都是完全使用requirejs模块开发的,然后它们使用requirejs优化器作为一个非常智能的连接器。有趣的是,正如你所看到的,jQuery和Modernizr都不需要依赖requirejs才能工作,这是因为他们消除了requirejs语法仪式,以便在他们的代码中摆脱requirejs。所以他们最终得到了一个独立的库,该库是使用requirejs模块开发的!由于这一点,他们能够执行自定义构建,以及其他优点。这里有一篇博客文章,更详细地解释了所有这些。
sudo npm -g install uglify-js
cat myAppDir/*.js | uglifyjs > build/application.js
我同意使用yuicompressor,但我使用/packer/
对我来说真的非常棒。
type *.js > compiled.js
我知道这是一个非常老的问题,但为了完整起见,我将提及使用Browserify的选项。它允许您使用NPM的require
函数构建不同模块的项目以解决依赖关系,然后解决这些依赖关系并将整个项目连接成单个文件。
例如,假设您的项目名为FooBar,您想要输出一个名为foobar.js
的文件。您需要创建一个main.js
文件作为项目的入口点,需要引用所有应包含的模块。
main.js
require("./doFoo");
require("./doBar");
browserify main.js -o foobar.js
或者,为了在源文件更改时自动执行此操作,您还可以使用Watchify。
watchify main.js -o foobar.js
doBar.js
require("./doQux");
const doBar = ()=>{
//Do some bar stuff.
}
exports.doBar = doBar;
doQux.js
包含在foobar.js
中,以便您不会有任何损坏的依赖关系。这是我使用glob解决相同任务的方法:
const fs = require('fs');
const glob = require('glob');
const outputFile = './output/output.js';
const filesToConcat = './path/*.js';
// delete output file if exists (otherwise it will continue appending)
fs.unlink(outputFile, function(err) {
console.log('output.js is removed');
});
// concat all js files into cam-benefits-ui.js
glob(filesToConcat, function(err, files) {
files.forEach(file => {
fs.readFile(file, 'utf8', function(err, data){
fs.appendFileSync(outputFile, data);
});
});
});