webpack/examples/asset-advanced/README.md

239 lines
7.2 KiB
Markdown
Raw Normal View History

2019-11-18 00:49:48 +08:00
This example shows usage of the asset module type with asset generator options customization.
2019-11-16 00:27:36 +08:00
2019-11-18 00:49:48 +08:00
Files can be imported like 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
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();
}
return svgToMiniDataURI(content);
2019-11-16 00:27:36 +08:00
}
}
}
]
},
experiments: {
asset: true
}
};
```
# js/output.js
```javascript
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ([
/* 0 */
/*!********************!*\
!*** ./example.js ***!
\********************/
/*! exports [not provided] [no usage info] */
/*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.r, __webpack_exports__, __webpack_require__.* */
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _images_file_svg__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./images/file.svg */ 1);
/* harmony import */ var _images_file_svg__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_images_file_svg__WEBPACK_IMPORTED_MODULE_0__);
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___default())].forEach(src => {
createImageElement(src.split(".").pop(), src);
});
/***/ }),
/* 1 */
/*!*************************!*\
!*** ./images/file.svg ***!
\*************************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
2019-11-18 00:49:48 +08:00
/*! runtime requirements: module */
/***/ ((module) => {
2019-11-16 00:27:36 +08:00
module.exports = "data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 600'%3e%3ctitle%3eicon-square-small%3c/title%3e%3cpath fill='white' d='M300 .1L565 150v299.9L300 599.8 35 449.9V150z'/%3e%3cpath fill='%238ED6FB' d='M517.7 439.5L308.8 557.8v-92L439 394.1l78.7 45.4zm14.3-12.9V179.4l-76.4 44.1v159l76.4 44.1zM81.5 439.5l208.9 118.2v-92l-130.2-71.6-78.7 45.4zm-14.3-12.9V179.4l76.4 44.1v159l-76.4 44.1zm8.9-263.2L290.4 42.2v89l-137.3 75.5-1.1.6-75.9-43.9zm446.9 0L308.8 42.2v89L446 206.8l1.1.6 75.9-44z'/%3e%3cpath fill='%231C78C0' d='M290.4 444.8L162 374.1V234.2l128.4 74.1v136.5zm18.4 0l128.4-70.6v-140l-128.4 74.1v136.5zM299.6 303zm-129-85l129-70.9L428.5 218l-128.9 74.4-129-74.4z'/%3e%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] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ !function() {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => module['default'] :
/******/ () => module;
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ var hasOwnProperty = Object.prototype.hasOwnProperty;
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(hasOwnProperty.call(definition, key) && !hasOwnProperty.call(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ !function() {
/******/ // 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 });
/******/ };
/******/ }();
/******/
/************************************************************************/
```
</details>
``` js
/******/ // startup
/******/ // Load entry module
/******/ __webpack_require__(0);
/******/ // This entry module used 'exports' so it can't be inlined
/******/ })()
;
```
# Info
## webpack output
```
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.7
Asset Size
output.js 5.08 KiB [emitted] [name: main]
Entrypoint main = output.js
chunk output.js (main) 1.6 KiB (javascript) 895 bytes (runtime) [entry] [rendered]
2019-11-16 00:27:36 +08:00
> ./example.js main
./example.js 658 bytes [built]
[no exports]
[used exports unknown]
entry ./example.js main
./images/file.svg 984 bytes [built]
2019-11-16 00:27:36 +08:00
[used exports unknown]
harmony side effect evaluation ./images/file.svg ./example.js 1:0-36
harmony import specifier ./images/file.svg ./example.js 26:1-4
+ 3 hidden chunk modules
```