webpack一:專案初始化、安裝webpack/webpack-cli、第一次打包、webpack本身只能處理js/json檔案、webpack作用總結

深藍冰河發表於2020-12-19

一、專案初始化、安裝webpack/webpack-cli、第一次打包

1)專案初始化

npm init
輸入專案名,其它預設即可

命令完成後會在專案下生成一個package.json:
package name: (webpack) webpack1
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to F:\Demo\webpack\package.json:
內容如下

{
  "name": "webpack1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

2)安裝webpack和webpack-cli:全域性工具及專案開發依賴

2.1全域性安裝

cnpm i webpack webpack-cli -g

2.2專案開發依賴安裝

cnpm i webpack webpack-cli -D
-D相當於:
--save-dev

命令完成後package.json增加了如下:
“devDependencies”: {
“webpack”: “^5.11.0”,
“webpack-cli”: “^4.2.0”
}

3)編寫入口檔案src/index.js

/*
入口檔案:打包時以此為入口
*/

function add(a,b){
    return a+b
}

console.log(add(1,2))

4)開發環境打包development和生產環境打包production

  1. 開發環境打包:
    webpack ./src/index.js -o ./build --mode=development
    含義:以index.js為入口檔案,打包輸出到./build/main.js,以開發模式
  2. 生產環境打包:
    webpack ./src/index.js -o ./builde/build.js --mode=production
    含義:以index.js為入口檔案,打包輸出到build.js,以生產模式
開發環境打包:
webpack ./src/index.js -o ./build/dev --mode=development

生產環境打包:
webpack ./src/index.js -o ./build/pro --mode=production

執行效果:

F:\Demo\webpack\webpack1>webpack ./src/index.js -o ./build/production --mode=pro
duction
[webpack-cli] Compilation finished
asset main.js 15 bytes [emitted] [minimized] (name: main)
./src/index.js 422 bytes [built] [code generated]
webpack 5.11.0 compiled successfully in 327 ms

F:\Demo\webpack\webpack1>webpack ./src/index.js -o ./build/dev --mode=developmen
t
[webpack-cli] Compilation finished
asset main.js 1.18 KiB [emitted] (name: main)
./src/index.js 422 bytes [built] [code generated]
webpack 5.11.0 compiled successfully in 173 ms

開發打包程式碼:build/dev/main.js

/*
 * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
 * This devtool is not neither made for production nor for readable output files.
 * It uses "eval()" calls to create a separate source file 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
/*!**********************!*\
  !*** ./src/index.js ***!
  \**********************/
eval("/*1.開發環境打包:\r\n    webpack ./src/index.js -o ./build --mode=development\r\n    含義:以index.js為入口檔案,打包輸出到./build/main.js,以開發模式\r\n2.生產環境打包:\r\n    webpack ./src/index.js -o ./builde/build.js --mode=production\r\n    含義:以index.js為入口檔案,打包輸出到build.js,以生產模式\r\n*/\r\n\r\nfunction add(a,b){\r\n    return a+b\r\n}\r\n\r\nconsole.log(add(1,2))\n\n//# sourceURL=webpack://webpack1/./src/index.js?");
/******/ })()
;

生產打包如下build/pro/main.js:

console.log(3)

二、webpack本身只能處理js/json檔案,其它如css/image都不能處理

1)建一個檔案data.json

{
    "name":"小明",
    "age":18,
    "sex":"女"
}

2)引用json:index.js

import data from './data.json'
console.log(data)

3)再次打包

開發模式打包直接入到build根目錄:
webpack ./src/index.js -o ./build --mode=development

生產模式打包:
webpack ./src/index.js -o ./build/pro --mode=production

開發模式程式碼build/main.js

/*
 * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
 * This devtool is not neither made for production nor for readable output files.
 * It uses "eval()" calls to create a separate source file 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
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ "./src/data.json":
/*!***********************!*\
  !*** ./src/data.json ***!
  \***********************/
/***/ ((module) => {

eval("module.exports = JSON.parse(\"{\\\"name\\\":\\\"小明\\\",\\\"age\\\":18,\\\"sex\\\":\\\"女\\\"}\");\n\n//# sourceURL=webpack://webpack1/./src/data.json?");

/***/ }),

/***/ "./src/index.js":
/*!**********************!*\
  !*** ./src/index.js ***!
  \**********************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _data_json__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./data.json */ \"./src/data.json\");\n/*入口檔案:打包從此處開始\r\n\r\n1.開發環境打包:\r\n    webpack ./src/index.js -o ./build --mode=development\r\n    含義:以index.js為入口檔案,打包輸出到./build/main.js,以開發模式\r\n2.生產環境打包:\r\n    webpack ./src/index.js -o ./builde/build.js --mode=production\r\n    含義:以index.js為入口檔案,打包輸出到build.js,以生產模式\r\n*/\r\n\r\nfunction add(a,b){\r\n    return a+b\r\n}\r\n\r\nconsole.log(add(1,2))\r\n\r\nconsole.log(_data_json__WEBPACK_IMPORTED_MODULE_0__)\n\n//# sourceURL=webpack://webpack1/./src/index.js?");

/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		if(__webpack_module_cache__[moduleId]) {
/******/ 			return __webpack_module_cache__[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
/******/ 	// startup
/******/ 	// Load entry module
/******/ 	__webpack_require__("./src/index.js");
/******/ 	// This entry module used 'exports' so it can't be inlined
/******/ })()
;

生產模式程式碼build/pro/main.js

(()=>{"use strict";const e=JSON.parse('{"name":"小明","age":18,"sex":"女"}');console.log(3),console.log(e)})();

如果引入了css檔案再打包就會報錯

總結:wp能做什麼

  1. webpack能處理js/json資源,不能處理css/img等其他資源
  2. 生產環境和開發環境將ES6模組化編譯成瀏覽器能識別的模組化~
  3. 生產環境比開發環境多一個壓縮js程式碼。

相關文章