| 
									
										
										
										
											2020-02-09 15:00:39 +08:00
										 |  |  | This example shows the usage of the asset module type with asset generator options customization. | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 15:00:39 +08:00
										 |  |  | Files can be imported similar to other modules without file-loader or url-loader. | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # example.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | import svg from "./images/file.svg"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const container = document.createElement("div"); | 
					
						
							|  |  |  | Object.assign(container.style, { | 
					
						
							|  |  |  | 	display: "flex", | 
					
						
							|  |  |  | 	justifyContent: "center" | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | document.body.appendChild(container); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function createImageElement(title, src) { | 
					
						
							|  |  |  | 	const div = document.createElement("div"); | 
					
						
							|  |  |  | 	div.style.textAlign = "center"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const h2 = document.createElement("h2"); | 
					
						
							|  |  |  | 	h2.textContent = title; | 
					
						
							|  |  |  | 	div.appendChild(h2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const img = document.createElement("img"); | 
					
						
							|  |  |  | 	img.setAttribute("src", src); | 
					
						
							|  |  |  | 	img.setAttribute("width", "150"); | 
					
						
							|  |  |  | 	div.appendChild(img); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	container.appendChild(div); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [svg].forEach(src => { | 
					
						
							|  |  |  | 	createImageElement(src.split(".").pop(), src); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # webpack.config.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2019-11-18 21:53:16 +08:00
										 |  |  | const svgToMiniDataURI = require("mini-svg-data-uri"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  | 	output: { | 
					
						
							|  |  |  | 		assetModuleFilename: "images/[hash][ext]" | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	module: { | 
					
						
							|  |  |  | 		rules: [ | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-11-18 22:04:49 +08:00
										 |  |  | 				test: /\.(png|jpg)$/, | 
					
						
							|  |  |  | 				type: "asset" | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				test: /\.svg$/, | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 				type: "asset", | 
					
						
							|  |  |  | 				generator: { | 
					
						
							|  |  |  | 					dataUrl: content => { | 
					
						
							| 
									
										
										
										
											2019-11-18 00:49:48 +08:00
										 |  |  | 						if (typeof content !== "string") { | 
					
						
							|  |  |  | 							content = content.toString(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 21:53:16 +08:00
										 |  |  | 						return svgToMiniDataURI(content); | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		] | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	experiments: { | 
					
						
							|  |  |  | 		asset: true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # js/output.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | /******/ (() => { // webpackBootstrap | 
					
						
							|  |  |  | /******/ 	"use strict"; | 
					
						
							|  |  |  | /******/ 	var __webpack_modules__ = ([ | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | /* 0 */, | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | /* 1 */ | 
					
						
							|  |  |  | /*!*************************!*\ | 
					
						
							|  |  |  |   !*** ./images/file.svg ***! | 
					
						
							|  |  |  |   \*************************/ | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | /*! default exports */ | 
					
						
							|  |  |  | /*! exports [not provided] [maybe used (runtime-defined)] */ | 
					
						
							| 
									
										
										
										
											2019-11-18 00:49:48 +08:00
										 |  |  | /*! runtime requirements: module */ | 
					
						
							|  |  |  | /***/ ((module) => { | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 21:24:22 +08:00
										 |  |  | module.exports = "data:image/svg+xml,%3csvg xmlns='http://www.w3.or...3c/svg%3e"; | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /***/ }) | 
					
						
							|  |  |  | /******/ 	]); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <details><summary><code>/* webpack runtime code */</code></summary> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` 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] = { | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | /******/ 			// no module.id needed | 
					
						
							|  |  |  | /******/ 			// no module.loaded needed | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | /******/ 			exports: {} | 
					
						
							|  |  |  | /******/ 		}; | 
					
						
							|  |  |  | /******/ 	 | 
					
						
							|  |  |  | /******/ 		// Execute the module function | 
					
						
							|  |  |  | /******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__); | 
					
						
							|  |  |  | /******/ 	 | 
					
						
							|  |  |  | /******/ 		// Return the exports of the module | 
					
						
							|  |  |  | /******/ 		return module.exports; | 
					
						
							|  |  |  | /******/ 	} | 
					
						
							|  |  |  | /******/ 	 | 
					
						
							|  |  |  | /************************************************************************/ | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </details> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` js | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | (() => { | 
					
						
							|  |  |  | /*!********************!*\ | 
					
						
							|  |  |  |   !*** ./example.js ***! | 
					
						
							|  |  |  |   \********************/ | 
					
						
							|  |  |  | /*! namespace exports */ | 
					
						
							|  |  |  | /*! exports [not provided] [unused] */ | 
					
						
							|  |  |  | /*! runtime requirements: __webpack_require__ */ | 
					
						
							|  |  |  | /* harmony import */ var _images_file_svg__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./images/file.svg */ 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const container = document.createElement("div"); | 
					
						
							|  |  |  | Object.assign(container.style, { | 
					
						
							|  |  |  | 	display: "flex", | 
					
						
							|  |  |  | 	justifyContent: "center" | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | document.body.appendChild(container); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function createImageElement(title, src) { | 
					
						
							|  |  |  | 	const div = document.createElement("div"); | 
					
						
							|  |  |  | 	div.style.textAlign = "center"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const h2 = document.createElement("h2"); | 
					
						
							|  |  |  | 	h2.textContent = title; | 
					
						
							|  |  |  | 	div.appendChild(h2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const img = document.createElement("img"); | 
					
						
							|  |  |  | 	img.setAttribute("src", src); | 
					
						
							|  |  |  | 	img.setAttribute("width", "150"); | 
					
						
							|  |  |  | 	div.appendChild(img); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	container.appendChild(div); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [_images_file_svg__WEBPACK_IMPORTED_MODULE_0__].forEach(src => { | 
					
						
							|  |  |  | 	createImageElement(src.split(".").pop(), src); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | })(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | /******/ })() | 
					
						
							|  |  |  | ; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Info
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## webpack output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Hash: 0a1b2c3d4e5f6a7b8c9d | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | Version: webpack 5.0.0-beta.16 | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  |     Asset      Size | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | output.js  3.02 KiB  [emitted]  [name: main] | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | Entrypoint main = output.js | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  | chunk output.js (main) 1.54 KiB [entry] [rendered] | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  |     > ./example.js main
 | 
					
						
							|  |  |  |  ./example.js 658 bytes [built] | 
					
						
							|  |  |  |      [no exports] | 
					
						
							| 
									
										
										
										
											2020-05-21 05:26:51 +08:00
										 |  |  |      [no exports used] | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  |      entry ./example.js main | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  |  ./images/file.svg 915 bytes [built] | 
					
						
							| 
									
										
										
										
											2019-11-19 21:48:46 +08:00
										 |  |  |      [no exports] | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  |      harmony side effect evaluation ./images/file.svg ./example.js 1:0-36 | 
					
						
							|  |  |  |      harmony import specifier ./images/file.svg ./example.js 26:1-4 | 
					
						
							|  |  |  | ``` |