Webpack plugin for static dependency injection analysis

Usage no npm install needed!

<script type="module">
  import extractDiWebpackPlugin from 'https://cdn.skypack.dev/extract-di-webpack-plugin';


extract-di-webpack-plugin Build Status


npm i -S extract-di-webpack-plugin


Step 1

Add plugin to webpack config:

// webpack.config.js
const ExtractDIPlugin = require('extract-di-webpack-plugin');

module.exports = {
    // ...
    plugins: [
        // ...
        new ExtractDIPlugin()

ExtractDIPlugin accepts 2 arguments:

 * @param {string} [moduleName = 'di-definitions] - used for requiring definitions in your code
 * @param {{}} options
 * @param {string} [options.exportName = '__diDefinitions'] - used for parsing definitions from your modules

Step 2

In your modules use following syntax to export definitions:

// module.js
Object.defineProperty(exports, '__diDefinitions', {
    currentUser: ['User', {
        request: 'Requst'

Exported definitions from all modules will be merged into one object and available for requiring from any place of a code.

Note, it is not necessary to write such definitions manually. Consider using babel-plugin-extract-dependency-definitions to export definitions automatically.

Step 3

Now you can require parsed definitions anywhere you want.

// entry.js
const definitions = require('di-definitons');