美文网首页
webpack-dev-server

webpack-dev-server

作者: 姜治宇 | 来源:发表于2023-07-04 15:48 被阅读0次

    1、安装

    npm init 
    npm i -D webpack@5 webpack-cli webpack-dev-server
    

    2、创建webpack的配置文件webpack.config.js:

    const path = require('path');
    module.exports = {
        entry: './src/index.js',
        output: {
            publicPath: '/',
            filename: 'bundle.js'
        },
        devServer: {
            port: 8080,
            static: {  //服务器所加载文件的目录
                directory: path.join(__dirname, 'www'),
            },
        },
    };
    

    publicPath就是虚拟路径的意思,bundle.js文件只生成在内存中,不会物理生成。
    3、配置package.json:

    {
      "name": "snabbdom-test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
       "build": "webpack",
        "dev": "webpack serve"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "snabbdom": "^3.5.1"
      },
      "devDependencies": {
        "webpack": "^5.88.1",
        "webpack-cli": "^5.1.4",
        "webpack-dev-server": "^4.15.1"
      }
    }
    
    

    4、创建www目录,创建index.html。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="bundle.js"></script>
    </head>
    <body>
        <div>hello world</div>
    </body>
    </html>
    

    5、创建入口文件src/index.js
    6、运行npm run dev

    相关文章

      网友评论

          本文标题:webpack-dev-server

          本文链接:https://www.haomeiwen.com/subject/clqhudtx.html