最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to dynamically add a moduledependency with a webpack plugin? - Stack Overflow

programmeradmin0浏览0评论

I feel like what i'm trying to achieve isn't that hard.. but the webpack docs are in a serious disarray and I'm burning many hours on this.

How do i inject a "dynamic" module into a webpack build? I want to create this module at build time.

For a simple example how do i inject this string as a new module at build?

"module.exports = new Date();"

Then lets say I want that module to have the name "myDate"

I would very much 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 purpose will be much more plex and involve reading files to create this "dynamic" module. I'm aware of the define plugin and it unfortunately does not suit my needs.

I would greatly appreciate any help. Thanks.

I feel like what i'm trying to achieve isn't that hard.. but the webpack docs are in a serious disarray and I'm burning many hours on this.

How do i inject a "dynamic" module into a webpack build? I want to create this module at build time.

For a simple example how do i inject this string as a new module at build?

"module.exports = new Date();"

Then lets say I want that module to have the name "myDate"

I would very much 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 purpose will be much more plex and involve reading files to create this "dynamic" module. I'm aware of the define plugin and it unfortunately does not suit my needs.

I would greatly appreciate any help. Thanks.

Share asked Oct 21, 2016 at 5:22 jennasjennas 2,4542 gold badges25 silver badges32 bronze badges 4
  • 1 One idea would be to do var myDate = require('./path/to/dynamically/generated/file'); then, at build time, you would create a file at that path using whatever means you wanted. Does that help at all? – jamesplease Commented Oct 21, 2016 at 5:35
  • @jmeas Yeah i did think of that too.. I wanted to avoid doing that because I'm adding cruft files into peoples file systems whom use my plugin. Also it has a long 'require' path to a physical file. Where i want more of an alias type thing. – jennas Commented Oct 21, 2016 at 5:39
  • 2 You could also write a webpack loader plugin that generates the js, and passes it on to the webpack pile process. – Chris Seufert Commented Dec 7, 2016 at 23:18
  • 2 @ChrisSeufert do you have an example? Or docs? I did look into this but was all so confusing. – jennas Commented Dec 8, 2016 at 2:35
Add a ment  | 

1 Answer 1

Reset to default 7

Found this plugin: virtual-module-webpack-plugin

Usage example 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 review implementation and try your own.

发布评论

评论列表(0)

  1. 暂无评论