webpack/examples/asset-advanced
Tobias Koppers 0111405198 enable asset experiment 2020-09-03 14:19:09 +02:00
..
images initial improvement for asset modules 2019-11-26 15:41:48 +01:00
README.md enable asset experiment 2020-09-03 14:19:09 +02:00
build.js initial improvement for asset modules 2019-11-26 15:41:48 +01:00
example.js initial improvement for asset modules 2019-11-26 15:41:48 +01:00
index.html initial improvement for asset modules 2019-11-26 15:41:48 +01:00
template.md Grammatical fix 2020-02-11 19:17:54 +05:30
webpack.config.js enable asset experiment 2020-09-03 14:19:09 +02:00

README.md

This example shows the usage of the asset module type with asset generator options customization.

Files can be imported similar to other modules without file-loader or url-loader.

example.js

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

const svgToMiniDataURI = require("mini-svg-data-uri");

module.exports = {
	output: {
		assetModuleFilename: "images/[hash][ext]"
	},
	module: {
		rules: [
			{
				test: /\.(png|jpg)$/,
				type: "asset"
			},
			{
				test: /\.svg$/,
				type: "asset",
				generator: {
					dataUrl: content => {
						if (typeof content !== "string") {
							content = content.toString();
						}

						return svgToMiniDataURI(content);
					}
				}
			}
		]
	}
};

js/output.js

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*************************!*\
  !*** ./images/file.svg ***!
  \*************************/
/*! default exports */
/*! exports [not provided] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {

module.exports = "data:image/svg+xml,%3csvg xmlns='http://www.w3.or...3c/svg%3e";

/***/ })
/******/ 	]);
/* webpack runtime code */
/************************************************************************/
/******/ // 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;
	/******/
}
/******/
/************************************************************************/
(() => {
/*!********************!*\
  !*** ./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);
});

})();

/******/ })()
;

Info

webpack output

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.16
    Asset      Size
output.js  3.02 KiB  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 1.54 KiB [entry] [rendered]
    > ./example.js main
 ./example.js 658 bytes [built]
     [no exports]
     [no exports used]
     entry ./example.js main
 ./images/file.svg 915 bytes [built]
     [no exports]
     harmony side effect evaluation ./images/file.svg ./example.js 1:0-36
     harmony import specifier ./images/file.svg ./example.js 26:1-4