I want to remove the html's crossorigin in offline, because offline doesn't support crossorigin.I do it in this way:
var fs = require('fs');
var path = require('path');
plugins: [
beforeZip: function() {
var webserverOfflineDir = path.join(__dirname, 'dist/offline', 'webserver'),
webserverFiles = fs.readdirSync(webserverOfflineDir);
webserverFiles.forEach(function(filename){
var fullname = path.join(webserverOfflineDir, filename);
var htmlRegex = './html$';
if(htmlRegex.test(htmlRegex)) {
var htmlContent = fs.readFileSync(fullname, 'utf8');
var crossoriginScriptRegex = /<script(\s)+(crossorigin)\s/g;
htmlContent = htmlContent.replace(crossoriginScriptRegex, '<script ');
fs.writeFileSync(fullname, htmlContent, 'utf8');
}
})
}
]
I should use fs api to read the webserver's directory,to read the html content, to write the html content.In my opinion, removing the html's crossorigin in offline is necessary,but I should use such a clumsy way to do this.
So is there a better way to change the asset's content in offline?Maybe it can do like this :
plugins: [
// read each asset in the callback
beforeZip: function(asset) {
var postfix = asset.postfix,
content = asset.content;
if(postfix === 'html') {
var crossoriginScriptRegex = /<script(\s)+(crossorigin)\s/g;
asset.content = content.replace(crossoriginScriptRegex, '<script ');
}
// return the asset which has been changed, or hasn't been changed
return asset;
}
]
Actually,this is one way to change assets.If this plugin can support any other way to do this,I think it can be more convenient to use.