amd-inject-loader
v0.5.0
Published
A simple webpack inject loader for AMD-style code
Downloads
90
Readme
AMD injection loader for webpack
What Is It
This is a simple webpack loader that allows you to override any dependency requested at the top of your code file in a normal define
function block. It is currently very limited in scope. It doesn't handle nested require
statements, and it doesn't overwrite or shim the require
method itself.
If you are looking for something that works with CommonJS code, try the inject-loader
.We tried that loader first, but it didn't work with AMD code and didn't work with our code formatting style, so we whipped this up to meet our immediate testing needs. The API is similar, but you don't need to specify ahead of time which dependencies you want to inject or leave alone. Simply pass in whatever injections you want each time you call the factory method. It can even change between tests in the same file.
Usage
If working with a code file that looks like this:
myfile.js
define( [ "lodash", "customApi" ], function () {
return {
init: function () {
customApi.run()
}
}
});
You could inject a different customApi
like this:
myfile.spec.js
var myfileInjector = require( "amd-inject!./myfile" );
var myfile = myfileInjector({
customApi: {
run: function () {
console.log( "A new run definition" );
}
}
});
myfile.init(); // Outputs A new run definition
Options
To customize options, add a amdInjectLoader
key to your webpack config file:
{
loaders: [ ... ],
...
amdInjectLoader: {
istanbul: true,
stripComments: true
}
}
istanbul
-true
orfalse
(Defaults tofalse
) – Include istanbul ignore blocks on the lines that handle the injectionstripComments
-true
orfalse
(Defaults tofalse
) – Strip comments from multiline dependency declarations