Ver código fonte

Webpack part2: 实战1

daxia 2 anos atrás
pai
commit
7f5a562993

+ 20 - 1
18_Webpack/day-2/webpack-demo/config/webpack.config.base.js

@@ -1 +1,20 @@
-module.exports = {};
+const path = require('path');
+const HtmlWebpackPlugin = require('html-webpack-plugin');
+
+module.exports = {
+  entry: {
+    index: path.join(__dirname, '../src/main.js'),
+  },
+  output: {
+    filename: '[name].bundle.[chunkhash].js',
+    path: path.resolve(__dirname, '../dist'),
+    clean: true, // 每一次打包前清空dist文件夹
+  },
+  plugins: [
+    new HtmlWebpackPlugin({
+      title: 'Vue App',
+      favicon: path.join(__dirname, '../public/favicon.ico'),
+      template: path.join(__dirname, '../public/index.html'),
+    }),
+  ],
+};

+ 10 - 1
18_Webpack/day-2/webpack-demo/config/webpack.config.dev.js

@@ -1,4 +1,13 @@
 const base = require('./webpack.config.base');
 const { merge } = require('webpack-merge');
 
-module.exports = merge(base, {});
+module.exports = merge(base, {
+  mode: 'development',
+  devtool: 'eval-cheap-module-source-map',
+  devServer: {
+    historyApiFallback: true,
+    port: 4000,
+    hot: true,
+    open: true,
+  },
+});

+ 3 - 1
18_Webpack/day-2/webpack-demo/config/webpack.config.prod.js

@@ -1,4 +1,6 @@
 const base = require('./webpack.config.base');
 const { merge } = require('webpack-merge');
 
-module.exports = merge(base, {});
+module.exports = merge(base, {
+  mode: 'production',
+});

BIN
18_Webpack/day-2/webpack-demo/dist/favicon.ico


+ 32 - 0
18_Webpack/day-2/webpack-demo/dist/index.bundle.b8a560d6d950cfe3be4e.js

@@ -0,0 +1,32 @@
+/*
+ * ATTENTION: An "eval-source-map" devtool has been used.
+ * This devtool is neither made for production nor for readable output files.
+ * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
+ * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
+ * or disable the default devtool with "devtool: false".
+ * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
+ */
+/******/ (() => { // webpackBootstrap
+/******/ 	var __webpack_modules__ = ({
+
+/***/ "./src/main.js":
+/*!*********************!*\
+  !*** ./src/main.js ***!
+  \*********************/
+/***/ (() => {
+
+eval("console.log('hello,webpack.');\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvbWFpbi5qcy5qcyIsIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZXMiOlsid2VicGFjazovL3dlYnBhY2stZGVtby8uL3NyYy9tYWluLmpzPzU2ZDciXSwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coJ2hlbGxvLHdlYnBhY2suJyk7XG4iXSwibmFtZXMiOltdLCJzb3VyY2VSb290IjoiIn0=\n//# sourceURL=webpack-internal:///./src/main.js\n");
+
+/***/ })
+
+/******/ 	});
+/************************************************************************/
+/******/ 	
+/******/ 	// startup
+/******/ 	// Load entry module and return exports
+/******/ 	// This entry module can't be inlined because the eval-source-map devtool is used.
+/******/ 	var __webpack_exports__ = {};
+/******/ 	__webpack_modules__["./src/main.js"]();
+/******/ 	
+/******/ })()
+;

+ 12 - 0
18_Webpack/day-2/webpack-demo/dist/index.html

@@ -0,0 +1,12 @@
+<!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>Vue App</title>
+  <link rel="icon" href="favicon.ico"><script defer src="index.bundle.b8a560d6d950cfe3be4e.js"></script></head>
+  <body>
+    <div id="app"></div>
+  </body>
+</html>

Diferenças do arquivo suprimidas por serem muito extensas
+ 832 - 5
18_Webpack/day-2/webpack-demo/package-lock.json


+ 5 - 1
18_Webpack/day-2/webpack-demo/package.json

@@ -4,7 +4,10 @@
   "description": "",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "dev": "webpack --config ./config/webpack.config.dev.js",
+    "serve": "webpack serve --config ./config/webpack.config.dev.js",
+    "build": "webpack --config ./config/webpack.config.prod.js"
   },
   "keywords": [],
   "author": "",
@@ -14,6 +17,7 @@
     "html-webpack-plugin": "^5.5.0",
     "webpack": "^5.78.0",
     "webpack-cli": "^5.0.1",
+    "webpack-dev-server": "^4.13.2",
     "webpack-merge": "^5.8.0"
   }
 }

+ 0 - 0
18_Webpack/day-2/webpack-demo/src/index.js


+ 3 - 0
18_Webpack/day-2/webpack-demo/src/main.js

@@ -0,0 +1,3 @@
+console.log('hello,webpack.');
+// debugger; // 代码中 手动添加断点
+// alert(1);

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff