This is used to prepend some code to the produced bundle.
Particularly useful if you want to share some global context, or to automatically inject some SDK.
It gives you access to the context.inject()
function.
All the injections will be resolved during the buildStart
hook,
so you'll have to have submitted your injection prior to that.
Ideally, you'd submit it during your plugin's initialization.
You can give it a distant file.
Be mindful that a 5s timeout is enforced.
context.inject({
type: 'file',
value: 'https://example.com/my_file.js',
});
You also give it a local file.
While you can use either a relative or absolute path, it's best to use an absolute one.
Remember that the plugins are also bundled before distribution.
context.inject({
type: 'file',
value: path.resolve(__dirname, '../my_file.js'),
});
Or give it any kind of string.
Be mindful that the code needs to be executable, or the plugins will crash.
context.inject({
type: 'code',
value: 'console.log("My un-invasive code");',
});