首页 > 其他分享 >webpack - 开发模式

webpack - 开发模式

时间:2022-11-22 17:59:59浏览次数:80  
标签:const dev server webpack 开发 模式 path js

1. 设置webpack为开发模式

  • 设置模式(mode: development/production)
  • 设置source maps (devtool: 'inline-source-map')
const path = require('path');
 const HtmlWebpackPlugin = require('html-webpack-plugin');

 module.exports = {
   mode: 'development',
   entry: {
     index: './src/index.js',
     print: './src/print.js',
   },
   devtool: 'inline-source-map',
   plugins: [
     new HtmlWebpackPlugin({
      title: 'Development',
     }),
   ],
   output: {
     filename: '[name].bundle.js',
     path: path.resolve(__dirname, 'dist'),
     clean: true,
   },
 };

2. 使用webpack的Watch模式进行自动打包编译

  • 设置webpack 的Watch 模式
    package.json
{
   "name": "webpack-demo",
   "version": "1.0.0",
   "description": "",
   "private": true,
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "watch": "webpack --watch",
     "build": "webpack"
   },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
     "html-webpack-plugin": "^4.5.0",
     "webpack": "^5.4.0",
     "webpack-cli": "^4.2.0"
   },
   "dependencies": {
     "lodash": "^4.17.20"
   }
 }
  • 以watch模式运行webpack

npm run watch
注意:
使用watch模式开发,刷新浏览器后才能看到应用编译后的变化

3. 使用webpack-dev-server进行开发

  • 安装 webpack-dev-server

npm install --save-dev webpack-dev-server

  • 配置webpack-dev-server
const path = require('path');
 const HtmlWebpackPlugin = require('html-webpack-plugin');

 module.exports = {
   mode: 'development',
   entry: {
     index: './src/index.js',
     print: './src/print.js',
   },
   devtool: 'inline-source-map',
  devServer: {
    static: './dist',
  },
   plugins: [
     new HtmlWebpackPlugin({
       title: 'Development',
     }),
   ],
   output: {
     filename: '[name].bundle.js',
     path: path.resolve(__dirname, 'dist'),
     clean: true,
   },
  optimization: {
    runtimeChunk: 'single',
  },
 };
  • 配置package.json
{
   "name": "webpack-demo",
   "version": "1.0.0",
   "description": "",
   "private": true,
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "watch": "webpack --watch",
    "start": "webpack serve --open",
     "build": "webpack"
   },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
     "html-webpack-plugin": "^4.5.0",
     "webpack": "^5.4.0",
     "webpack-cli": "^4.2.0",
     "webpack-dev-server": "^3.11.0"
   },
   "dependencies": {
     "lodash": "^4.17.20"
   }
 }
  • 运行webpack 使用webpack-dev-server

npm start
运行npm start 后,一旦更改文件,webpack会自动打包编译并自动重写加载代码
无需手动刷新浏览器

4. 使用webpack-dev-middleware

webpack-dev-middleware 是一个包装器,它将把webpack处理的文件发送到服务器。
这在webpack-dev-server内部使用,但是它可以作为单独的包提供,以允许在需要时
进行更多自定义设置。

  • 安装express 和 webpack-dev-middleware

npm install --save-dev express webpack-dev-middleware

  • 设置publicPath
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

 module.exports = {
   mode: 'development',
   entry: {
     index: './src/index.js',
     print: './src/print.js',
   },
   devtool: 'inline-source-map',
   devServer: {
     static: './dist',
   },
   plugins: [
     new HtmlWebpackPlugin({
       title: 'Development',
     }),
   ],
   output: {
     filename: '[name].bundle.js',
     path: path.resolve(__dirname, 'dist'),
     clean: true,
     publicPath: '/',
   },
 };
  • 设置express server

server.js

const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');

const app = express();
const config = require('./webpack.config.js');
const compiler = webpack(config);

// Tell express to use the webpack-dev-middleware and use the webpack.config.js
// configuration file as a base.
app.use(
  webpackDevMiddleware(compiler, {
    publicPath: config.output.publicPath,
  })
);

// Serve the files on port 3000.
app.listen(3000, function () {
  console.log('Example app listening on port 3000!\n');
});
  • 设置npm command (npm run server)

package.json

{
   "name": "webpack-demo",
   "version": "1.0.0",
   "description": "",
   "private": true,
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "watch": "webpack --watch",
     "start": "webpack serve --open",
     "server": "node server.js",
     "build": "webpack"
   },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
     "express": "^4.17.1",
     "html-webpack-plugin": "^4.5.0",
     "webpack": "^5.4.0",
     "webpack-cli": "^4.2.0",
     "webpack-dev-middleware": "^4.0.2",
     "webpack-dev-server": "^3.11.0"
   },
   "dependencies": {
     "lodash": "^4.17.20"
   }
 }

参考:https://webpack.js.org/guides/development/

标签:const,dev,server,webpack,开发,模式,path,js
From: https://www.cnblogs.com/xiaodi-js/p/16915925.html

相关文章

  • VM501振弦读数模块开发测试套件
    开发板(demoboard)是用来进行嵌入式系统开发的电路板,包括中央处理器、存储器、输入设备、输出设备、数据通路/总线和外部资源接口等一系列硬件组件。开发板一般由嵌入式系统......
  • 推特群控营销:自动发帖模式
    说到群控,大家一点也不陌生,今天推特群推王给大家介绍一下twitter群控,现在很多企业都在通过推特做推广,简单来说,Twitter群控营销就是借助第三方群控软件控制多个账号,同时大批量......
  • Android开发 内容提供者ContentProvider
    前言ContentProvider内容提供者属于Android四大组件之一,是用来让App将一些共享数据提供给外部操作的组件。一般情况下是配合数据库使用,将数据库内容外露给外部进行增......
  • 设计模式 - (抽象)工厂模式
    本文以故事方式介绍工厂模式以及抽象工厂模式的一些知识,本文出现的人名为化名周末闲来无事,小可爱给大漂亮说想吃烤红薯,让大漂亮做给小可爱吃,大漂亮有点为难了,一个堂堂七尺......
  • 设计模式之单例设计模式
    1.1基本概念在某些特殊场合中,⼀个类对外提供且只提供⼀个对象时,这样的类叫做单例类,⽽设计单例类的流程和思想叫做单例设计模式。1.2实现流程私有化构造方法,使用pr......
  • android开发Installed Build Tools revision 33.0.0 is corrupted. Remove and instal
    InstalledBuildToolsrevision33.0.0iscorrupted.RemoveandinstallagainusingtheSDKManager.在你的androidsdk安卓目录中找到buildtools目录中的d8.bat,......
  • “如何实现高效的应用交付”鲁班会开发者训练营厦门站进行时
    摘要:2022年11月18日,来自厦门创新中心的40余位开发者,齐聚华为云鲁班会开发者训练营厦门站,与华为云技术大咖共同探讨技术未来,落地应用交付。为了抓住新时代IT技术脉搏,一同探讨......
  • “如何实现高效的应用交付”鲁班会开发者训练营厦门站进行时
    摘要:2022年11月18日,来自厦门创新中心的40余位开发者,齐聚华为云鲁班会开发者训练营厦门站,与华为云技术大咖共同探讨技术未来,落地应用交付。为了抓住新时代IT技术脉搏,一同探......
  • 微服务开发框架(spring cloud /dubbo)
      Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,基于RPC调用,对于目前使用率较高的SpringCloudNetflix来说,它是基于HTTP的,所以效率上没有Dubbo高,但问题在于Du......
  • 大咖说·先临三维|技术入云塑造3D视觉行业新模式
    高精度3D视觉技术主要的工作原理是什么?它的开发难度在哪里?数字化技术对其有何助力?本期大咖说,看先临三维副总裁兼研究院副院长江腾飞如何分享。嘉宾介绍江腾飞:先......