gpt4 book ai didi

grunt 构建期间的 XML 文件预处理

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:12 24 4
gpt4 key购买 nike

我们必须使用需要 configuration.xml 文件的公司 JavaScript 库。我们需要支持多个配置文件,每个环境一个。我们有多个文件并在我们的 grunt 构建期间执行文件移动/重命名过程以选择并重命名正确的文件。我们希望避免使用多个文件。

对于 HTML,我们使用 grunt-processhtml :

<!-- build:js app.min.js -->
<script src="my/lib/path/lib.js"></script>
<script src="my/deep/development/path/script.js"></script>
<!-- /build -->

<!-- changed to -->
<script src="app.min.js"></script>

我们只执行 Release模式的处理,因此开发人员将只使用顶部 block ,而发布将只使用底部 block 。

模板似乎不是一个好方法,因为我们需要的不仅仅是变量替换。

This article说明了一种方法,但 XML 修改将驻留在我不想要的代码中。我考虑过将这种方法与 XML 合并库相结合,但在我这样做之前,我想知道其他人如何处理这种情况。

提前致谢。

最佳答案

如果代码驻留在 Gruntfile 本身中,则模板将起作用。例如:

function dynIncludes(env)
{
// Script
var appjs = '<script src="foo.js"></script>\n';

// Minified script
var appminjs = '<script src="foo.min.js"></script>\n';

if(env !== "release")
return appjs;
else
return appminjs;
}


copy:
{
dev:
{
options: {
process: function (content, srcpath, destpath)
{
// Append script to each file in the src array
return content + "\n" + grunt.template.process(dynIncludes('dev') );
}
}
},
release:
{
options: {
process: function (content, srcpath, destpath)
{
return content + "\n" + grunt.template.process(dynIncludes('release') );
}
}
}
}

这使用复制任务的 process 选项:

The source file contents, source file path, and destination file path are passed into this function, whose return value will be used as the destination file's contents. If this function returns false, the file copy will be aborted.

这可以与 grunt.util.callbackify 结合用于后处理:

If the original function returns a value, that value will now be passed to the callback, which is specified as the last argument, after all other predefined arguments. If the original function passed a value to a callback, it will continue to do so.

使用 loDash 简化更复杂场景的模板。

引用资料

关于grunt 构建期间的 XML 文件预处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31254511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com