Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Une this script to covert your index.html of create-react-app into index.html cordova ready !
let FS = require('fs');
// read the index.html from build folder
let data = FS.readFileSync('./build/index.html', 'utf8');
function insertContent(fullContent, beforeWhat, newContent) {
// get the position before which newContent has to be added
const position = fullContent.indexOf(beforeWhat);
// since splice can be used on arrays only
let fullContentCopy = fullContent.split('');
fullContentCopy.splice(position, 0, newContent);
return fullContentCopy.join('');
}
// will add the <meta> tags needed for cordova app
const afterAddingMeta = insertContent(data, '<link',
'<meta http-equiv="Content-Security-Policy" content="default-src *; style-src \'self\' http://* \'unsafe-inline\'; script-src \'self\' http://* \'unsafe-inline\' \'unsafe-eval\'" />' +
'<meta name="format-detection" content="telephone=no">' +
'<meta name="msapplication-tap-highlight" content="no">');
// will add <script> pointing to cordova.js
const afterAddingScript = insertContent(afterAddingMeta, '<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"', '<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script>');
// updates the index.html file
FS.writeFile('./build/index.html', afterAddingScript, 'utf8', (err) => {
if (err) {
throw err;
};
console.log('Cordova metas and scripts has been added !');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.