Reputation: 425
I have an after_prepare
hook that replaces some placeholders with environment settings in my app's main javascript file. It works fine in Cordova 5.3.3 and below, but starting in Cordova 5.4.0 the changes to the file are undone by the time it makes it into my app. When I reread the file in the hook it looks like it wrote successfully, but when I open the file after the app run it has reverted.
The file is platforms/ios/www/static/angular/js/my-angular-stuff.js
.
I was using the hooks/after_prepare/
folder but since it's deprecated I tried using the config.xml
setting instead. I also started using the module.exports = function(context) {
style that Cordova recommends; neither of these seem to make any difference. Again, the script seems to run just fine, and confirms that changes were written properly. But something after it undoes the changes.
I'm happy to use a hook that isn't after_prepare
if there's something more appropriate. TBH I don't have a firm grasp on the various Cordova execution phases (build vs compile vs prepare vs run).
Upvotes: 1
Views: 1079
Reputation: 1150
I was trying to modify my iOS project file and had similar issues where the after_prepare him didn't work. I ended up going with the after_plugin_install instead. Not as good but a workable solution for my scenario.
Upvotes: 0
Reputation: 11721
I remember I had to refactor many things in my hooks when I switched to cordova 6, but I don't remember what exactly...
In case it helps, here' what I have that still works in 6.1 :
It's a android specific hook so it's in the android platform part of config.xml :
<platform name="android">
<preference name="AndroidLaunchMode" value="singleTop" />
<preference name="android-minSdkVersion" value="14" />
<preference name="KeepRunning" value="true"/>
<icon src="res/android/xhdpi.png" />
<icon density="ldpi" src="res/android/ldpi.png" />
<icon density="mdpi" src="res/android/mdpi.png" />
<icon density="hdpi" src="res/android/hdpi.png" />
<icon density="xhdpi" src="res/android/xhdpi.png" />
<hook type="after_prepare" src="scripts/android/myhookname.js" />
<hook type="after_platform_add" src="scripts/android/anotherhook.js" />
...
</platform>
...
and the myhookname.js looks like this :
#!/usr/bin/env node
module.exports = function(ctx) {
var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
xml = ctx.requireCordovaModule('cordova-common').xmlHelpers;
var manifestPath = path.join(ctx.opts.projectRoot, 'platforms/android/AndroidManifest.xml');
var doc = xml.parseElementtreeSync(manifestPath);
if (doc.getroot().tag !== 'manifest') {
throw new Error(manifestPath + ' has incorrect root node name (expected "manifest")');
}
//change the theme in Android manifest file
doc.getroot().find('./application/activity[@android:name="MainActivity"]').attrib['android:theme'] = '@android:style/Theme.DeviceDefault.Light';
//save the file
fs.writeFileSync(manifestPath, doc.write({indent: 4}), 'utf-8');
console.log('\nEnd of post-prepare hook');
}
But I don't have ios hooks, maybe your problem is platform specific or a bug in cordova ios, have you tried upgrading to the latest version of cordova ios?
Also I use hooks to modify native files, not files in www, so maybe I would have the same issue as you if I patched files in www.
To patch js files in www, I use the merges folder where I put specific js files per platform.
Upvotes: 1