https://github.com/plasticine/inject-loader
ππ¦ A Webpack loader for injecting code into modules via their dependencies.
https://github.com/plasticine/inject-loader
javascript loader webpack-loader
Last synced: 19 days ago
JSON representation
ππ¦ A Webpack loader for injecting code into modules via their dependencies.
- Host: GitHub
- URL: https://github.com/plasticine/inject-loader
- Owner: plasticine
- License: mit
- Created: 2014-10-08T10:19:48.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2022-12-03T03:16:17.000Z (about 3 years ago)
- Last Synced: 2025-12-20T23:45:52.152Z (about 2 months ago)
- Topics: javascript, loader, webpack-loader
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/inject-loader
- Size: 552 KB
- Stars: 484
- Watchers: 13
- Forks: 41
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
***
### Why
This is particularly useful for writing tests where mocking things inside your module-under-test is sometimes necessary before execution.
`inject-loader` was inspired by, and builds upon ideas introduced in [jauco/webpack-injectable](https://github.com/jauco/webpack-injectable).
### Usage
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
Use the inject loader by adding the `inject-loader!` [inline loader](https://webpack.js.org/concepts/loaders/#inline) when you use `require`, this will return a function that can used in test code to modify the injected module.
By default all `require` statements in an injected module will be altered to be replaced with an injector, though if a replacement it not specified the default values will be used.
### Examples
Given some code in a module like this:
```javascript
// MyStore.js
var Dispatcher = require('lib/dispatcher');
var EventEmitter = require('events').EventEmitter;
var handleAction = require('lib/handle_action');
Dispatcher.register(handleAction, 'MyStore');
```
You can manipulate itβs dependencies when you come to write tests as follows:
```javascript
// If no flags are provided when using the loader then
// all require statements will be wrapped in an injector
MyModuleInjector = require('inject-loader!MyStore')
MyModule = MyModuleInjector({
'lib/dispatcher': DispatcherMock,
'events': EventsMock,
'lib/handle_action': HandleActionMock
})
```
There are a few examples of complete test setups for both Webpack 1, 2, 3 & 4 in the [`example`](./example) folder.
## License
MIT (http://www.opensource.org/licenses/mit-license.php)