admin管理员组文章数量:1336293
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
1 Answer
Reset to default 7Found 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.
本文标签: javascriptHow to dynamically add a moduledependency with a webpack pluginStack Overflow
版权声明:本文标题:javascript - How to dynamically add a moduledependency with a webpack plugin? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405994a2468831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论