1. inject-loader
A Webpack loader for injecting code into modules via their dependencies
inject-loader
Package: inject-loader
Created by: plasticine
Last modified: Sun, 19 Jun 2022 01:18:54 GMT
Version: 4.0.1
License: MIT
Downloads: 74,438
Repository: https://github.com/plasticine/inject-loader

Install

npm install inject-loader
yarn add inject-loader
 

💉📦
inject-loader

A Webpack loader for injecting code into modules via their dependencies

build status Gemnasium npm version npm downloads

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.

Usage

Documentation: Using loaders

Use the inject loader by adding the inject-loader! inline loader 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:

 // 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:

 // 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 folder.

License

MIT (http://www.opensource.org/licenses/mit-license.php)

Dependencies

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin