1、准备

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. hahaha
  11. </body>
  12. </html>
  1. function add(x, y) {
  2. return x + y;
  3. }
  4. console.log(add(1, 2));
  5. console.log(add(2, 2));

2、webpack.config.js

  1. const { resolve } = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. module.exports = {
  4. entry: './src/index.js',
  5. output: {
  6. filename: 'built.js',
  7. path: resolve(__dirname, 'build')
  8. },
  9. module: {
  10. },
  11. plugins: [
  12. new HtmlWebpackPlugin({
  13. template: './src/index.html'
  14. })
  15. ],
  16. mode: 'development',
  17. // 用来自动化(自动编译,自动打开浏览器,自动刷新浏览器~~)
  18. // 特点:只会在内存中编译打包,不会有任何输出
  19. // 启动devServer指令为:npx webpack-dev-server
  20. devServer: {
  21. // 启动gzip压缩
  22. compress: true,
  23. // 端口号
  24. port: 3000,
  25. // 自动打开浏览器
  26. open: true,
  27. //自动刷新
  28. liveReload:true,
  29. // HMR true开启 HMR 功能 开启模块热替换功能后将在不刷新整个页面的情况下通过用新模块替换老模块来做到实时预览
  30. hot:false
  31. }
  32. };

3、结果

image.png
image.png