大家都知道webpack打包十分方便,但是在工作中,前端写好的项目需要后端进行部署,就需要有一个配置文件。

  使用插件 :  generate-asset-webpack-plugin,

  使用方法 : 1  在项目中安装 :  npm install --save-dev generate-asset-webpack-plugin

         2  在webpack.config.js 中使用插件:  

const GenerateAssetPlugin=require('generate-asset-webpack-plugin'); 

var createServerConfig=function(compilation){
  let cfgJson={
      "title" : "UI",
      "logo" : "****"
  };
  return JSON.stringify(cfgJson);
}

       在 plugins 中配置 :

        

        3 : 运行 npm run build :进行打包;打包完成之后就会生成一个serverConfig.json文件 ; 生产环境可以通过你的配置文件里的参数修改进行部署;

        4 : 在我的项目中 ,在main.js中获取了配置参数,然后通过window变量进行使用。

axios.get("serverConfig.json").then((result)=>{
        window.initJson = result.data;
        new Vue({
            router,
            el: '#app',
            render: h => h(App),
        });
}).catch((error)=>{console.log(error)});

 

          

        

posted on 2017-07-18 14:45  Facker  阅读(373)  评论(0编辑  收藏  举报