webpack的使用

1.先装node,配置好环境变量

2.在终端进入空文件中 执行 npm init  //自动创建package.json文件

3. 安装webpack    npm install --save-dev webpack //局部安装webpack

4.手动配置 webpack.config.js文件

const webpack=require('webpack');
const HtmlWebpackPlugin=require('html-webpack-plugin'); //基本作用就是生成html文件

const OpenBrowserPlugin = require('open-browser-webpack-plugin')   //自动打开浏览器

module.exports = {  

  //entry: __dirname + "/app/a.js",//唯一入口文 

  entry:{     //多入口文件配置

    aA:__dirname + "/app/a1.js",
    bB:__dirname + "/app/a2.js",
  },
  output: {
    path: __dirname + "/public",//打包后的文件存放的地方
    //filename: "dist.js"//打包后输出文件的文件名
    filename: "[name].js"//打包后输出文件的文件名
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }]
  },
  devServer: {
    inline:true,
    port: 804
  },
  plugins:[
    new webpack.optimize.UglifyJsPlugin(),  //压缩js
    new OpenBrowserPlugin({url:'http://localhost:804'}),
    //new HtmlWebpackPlugin({template:'./pub/index.html'}),
    new HtmlWebpackPlugin({filename:'index.html',title:'index',template:'./pub/index.html',chunks:['aA']}),  //chunks包含的文件,在html中生成一个aA的入口文件
    new HtmlWebpackPlugin({filename:'index2.html',title:'.index2',template:'./pub/index2.html',chunks:['bB']})
  ]
}

在package.json配置

"scripts": {
  "cs": "webpack",  //npm run cs  打包
  "start": "webpack&&webpack-dev-server --hot --inline"  //npm start  打开浏览器及文件变动刷新

 

 

github:https://github.com/RLflash/webpackDemo

 

如有不对的地方,请多多指教!      ——RLflash

 

转载于:https://www.cnblogs.com/RLflash/p/8359105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值