我正在寻找一种方法来将一些信息添加到最小化文件中。
我找到了选项 here ,但它对我来说没有用,因为 uglifier 在添加包装器代码后运行。
最佳答案
你可以assign a function to the out
key后处理文件。通过设置此选项,结果不会自动写入文件,因此您必须自己执行此操作。例如:
({
// Let's optimize mainApp.js
name: "mainApp",
optimize: "uglify",
out: function(text) {
// Transform the compiled result.
text = '// Stuff to prepend \n' + text;
var filename = 'outputfile.js';
// By default, the name is resolved to the current working directory.
// Let's resolve it to the directory that contains this .build.js:
filename = path.resolve(this.buildFile, '..', filename);
// Finally, write the transformed result to the file.
file.saveUtf8File(filename, text);
}
})
注意:在前面的代码片段中,file.saveUtf8File
is an internal RequireJS API path
是 path
module从 Node.js 标准库导入(仅当您使用 Node.js 运行 r.js
时,而不是使用 Rhino 或在浏览器中运行时)。
如果将之前的内容保存为 test.build.js
,请创建一个名为 mainApp.js
的空文件并运行 `r.js -o test.build.js ,然后将创建一个名为“outputfile.js”的文件,其中包含以下内容:
// Stuff to prepend
define("mainApp",function(){});
关于javascript - RequireJS 优化器前置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479368/