Right now this is only possible on desktop, and it requires annoying code:
var veState = mw.loader.getState( 'ext.visualEditor.desktopArticleTarget.init' ); if ( veState === 'loading' || veState === 'loaded' || veState === 'ready' ) { mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).done( function () { mw.libs.ve.addPlugin( 'modulename' ); } }
https://gerrit.wikimedia.org/r/c/mediawiki/extensions/VisualEditor/+/638204 implements a hook that allows the following instead:
mw.hook( 've.loadModules' ).add( function ( addPlugin ) { addPlugin( 'modulename' ); } );
This works on desktop and mobile, but @DLynch correctly points out that it won't work for DiscussionTools, because ArticleTargetLoader isn't used there. We'll either have to accept this limitation, or figure out a way to make it work for DT as well (and all ways that VE can be loaded more generally).