VModulewebpack插件VModule是一个用于创建虚拟模块的webpack插件。大致有以下几种使用场景:动态计算的结果需要在构建阶段生成到一个模块中。构建后的运行时代码需要引用一些环境变量或者其他构建阶段的数据。安装$npmivmodule-webpack-plugin--save以在webpack中使用在.config.js中配置VModuleconstwebpack=require('webpack');constVModulePlugin=require('vmodule-webapck-plugin');module.exports={...plugins:[//返回对象的虚拟模块newVModulePlugin({name:'demo-module1',handler:function(){return{name:'demo',env:process.env.NODE_ENV};}}),//返回代码的虚拟模块newVModulePlugin({name:'demo-module2',code:truehandler:function(){reutrn`exports.say=function(text){console.log('say:',text);};`;}})]...};在代码中使用虚拟模块constmodule1=require('demo-module1');constmodule2=requrie('demo-module2');module2.say(JSON.stringify(module1));//打印//say:{"name":"demo","env":"valueofNODE_ENV"}
