zoukankan      html  css  js  c++  java
  • 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看一下效果

  • 相关阅读:
    上传图片,将图片保存在腾讯云(2种方式)
    由ping所引发的思考~
    php面试上机题(2018-3-3)
    【八】jqeury之click事件[添加及删除数据]
    【七】jquery之属性attr、 removeAttr、prop[全选全不选及反选]
    【六】jquery之HTML代码/文本/值[下拉列表框、多选框、单选框的选中]
    【五】jquery之事件(focus事件与blur事件)[提示语的出现及消失时机]
    小白懂算法之基数排序
    mysql_sql199语法介绍
    Python基本编程快速入门
  • 原文地址:https://www.cnblogs.com/famLiu/p/7383688.html
Copyright © 2011-2022 走看看