Best practice is to use the PHP methods which generate the required wrappers. Have a look at ResourceLoader::makeLoaderConditionalScript() and ResourceLoader::makeInlineCodeWithModule().
Alternatively, if it's possible, it is ideal to put the initialization code into another module and load it with addMobules() etc. as usual, instead of inlining the code in the HTML source.