How to dynamically add module / dependency using webpack plugin?

It seems to me that I'm trying to achieve, it is not so difficult .., but the documents in the web package are in a serious mess, and I burn many hours on it.

How do I add a “dynamic” module to a webpack assembly? I want to create this module at build time.

For a simple example, how do I insert this line as a new module during assembly?

"module.exports = new Date();"

Then let's say that I want this module to have the name "myDate"

I would really like any other module in my application to be able to resolve it with:

var myDate = require('myDate');

Now this is a very simple example. My goal will be much more complicated and include reading files to create this “dynamic” module. I know the define plugin and, unfortunately, is not suitable for my needs.

I would really appreciate any help. Thank.

+4
source share
1 answer

Found this plugin: virtual-module-webpack-plugin

Example usage described in README.md

const VirtualModulePlugin = require('./virtual-module-webpack-plugin');

  // ...

  plugins: [
    new VirtualModulePlugin({
      moduleName: 'src/mysettings.json',
      contents: JSON.stringify({ greeting: 'Hello!' })
    })
  ]
}

You can view the implementation and try your own.

+3
source

Source: https://habr.com/ru/post/1658397/


All Articles