34.自己配一个webpack

1.创建项目并生成package.json

$ mkdir webpack-demo0 && cd webpack-demo0
$ npm init -y
$ npm install lodash --save-dev

生成的package.json

{
  "name": "webpack-demo0",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.4"
  }
}

 

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

 

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script src="./dist/bundle.js" charset="utf-8"></script>
  </body>
</html>

 

 

在index.js里面随便写点什么

//index.js
console.log("webpack--v")

 

调整一下,项目结构大概是这个样子

 

 编译运行

打开index.html

这就完成了一个很简单的webpack配置,基本上到这里没什么用,完善一下

加载css的配置

$ npm install --save-dev style-loader css-loader

修改一下webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  moudles:{
    rules:[
     test:/\.css$/,//匹配css文件的正则
      use: ['style-loader','css-loader']
    ]
  }
};

安装一下插件

$ npm install --save-dev style-loader css-loader

src目录下添加一个index.css

.test{
  color: red;
  font-size: 20px;
  font-weight: bold;
}

修改一下index.js

import './index.css'
console.log("webpack--v")
console.log("--watch")

为了看出效果,在修改一下index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="test">
      loadcss
    </div>
    <script src="./dist/bundle.js" charset="utf-8"></script>
  </body>
</html>

添加 npm script-->package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
},

重新刷新一下网页

 

 加载图片配置

$ npm install --save-dev file-loader
//webpack.config.js
{
         test: /\.(png|svg|jpg|gif)$/,
         use: [
           'file-loader'
         ]
 }

字体文件

{
         test: /\.(woff|woff2|eot|ttf|otf)$/,
         use: [
           'file-loader'
         ]
}

完毕。。

最后再完善一下,装几个webpack插件

$ npm install webpack-dev-server --save
$ npm install webpack --save
$ npm install html-webpack-plugin

在webpack.config.js中配置一下插件

const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin')
// const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules:[
      {
        test:/\.css$/,
        use:['style-loader','css-loader']
      },
      {
        test:/\.(png|svg|jpg|gif)$/,
        use:['file-loader']
      },
      {
        test:/\.(woff|woff2|eot|ttf|otf)/,
        use:['file-loader']
      },
      {
        test:/\.(csv|tsv)$/,
        use:['csv-loader']
      },
      {
        test:/\.xml$/,
        use:['xml-loader']
      }
    ]
  },
  plugins:[
    // new CleanWebpackPlugin(['dist']),
    
    new htmlWebpackPlugin({
      title:'output manager',
      template:'./index.html'
    }),
    new webpack.HotModuleReplacementPlugin()
  ]
};

在package.json中添加几条常用指令

{
  "name": "webpack-demo0",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "clean": "rimraf dist/*",
    "start": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.4",
    "webpack": "^3.5.5"
  },
  "devDependencies": {
    "css-loader": "^0.28.5",
    "style-loader": "^0.18.2"
  }
}

修改一下index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="test">
      loadcss
    </div>
    <script src="bundle.js" charset="utf-8"></script>
  </body>
</html>

真正完毕!!

运行一下npm start看一下效果

转载于:https://www.cnblogs.com/famLiu/p/7383688.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值