欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

webpack初起步

程序员文章站 2022-07-26 07:49:53
webpack 用于编译 JavaScript 模块。一旦完成安装,就可以通过 webpack 的 CLI 或 API 与其配合交互。 首先创建一个目录,进入目录中,初始化npm 然后本地安装webpack,webpack-cli ,lodash 目录结构: index.html webpack.c ......

webpack 用于编译 javascript 模块。一旦完成,就可以通过 webpack 的 cli 或 api 与其配合交互。

首先创建一个目录,进入目录中,初始化npm 然后本地安装webpack,webpack-cli ,lodash

webpack初起步

webpack初起步

webpack初起步

目录结构:

webpack初起步

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>document</title>
</head>
<body>
    <script src="bundle.js"></script>
</body>
</html>

webpack.config.js

const path = require('path');

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

package,json

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "isc",
  "devdependencies": {
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3"
  },
  "dependencies": {
    "lodash": "^4.17.11"
  }
}

输入npm run build

webpack初起步

webpack初起步