我发现可以使用 onBuildWrite 获取 js 文件的内容,但我需要一种方法将值注入(inject)到我们的根 html 页面中,这可能吗?例如,我们想将 less 文件替换为 css 版本。
最佳答案
使用 Node.js 是可能的:
var fs = require('fs');
// read html file
var fileContents = fs.readFileSync('index.html', 'utf8');
// replace rel attribute for less file with rel for css
fileContents = fileContents.replace(/stylesheet\/less/gi, 'stylesheet');
// replace file name
fileContents = fileContents.replace(/css\/application.less/gi, 'css/application.css');
// remove less runtime compiler (if needed)
fileContents = fileContents.replace(/<script.*?\bless\b[^"']*?\.js.*?<\/script>/g, '');
// write file back
fs.writeFileSync('index.html', fileContents, 'utf8');
只需将其添加为构建脚本的一部分,并调用 r.js。
关于node.js - 使用requirejs优化器时是否可以配置html文件中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000333/