2020-05-21 05:26:51 +08:00
|
|
|
This is a very simple example that shows the usage of the asset module type.
|
2019-03-31 22:17:22 +08:00
|
|
|
|
|
|
|
|
Files can be imported like other modules without file-loader.
|
|
|
|
|
|
|
|
|
|
# example.js
|
|
|
|
|
|
2019-07-16 19:16:27 +08:00
|
|
|
```javascript
|
2019-03-31 22:17:22 +08:00
|
|
|
import png from "./images/file.png";
|
|
|
|
|
import jpg from "./images/file.jpg";
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[png, jpg, svg].forEach(src => {
|
|
|
|
|
createImageElement(src.split(".").pop(), src);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-17 23:30:25 +08:00
|
|
|
# webpack.config.js
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
module.exports = {
|
|
|
|
|
output: {
|
|
|
|
|
assetModuleFilename: "images/[hash][ext]"
|
|
|
|
|
},
|
|
|
|
|
module: {
|
|
|
|
|
rules: [
|
|
|
|
|
{
|
|
|
|
|
test: /\.(png|jpg|svg)$/,
|
2019-11-18 00:49:48 +08:00
|
|
|
type: "asset"
|
2019-07-17 23:30:25 +08:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-31 22:17:22 +08:00
|
|
|
# js/output.js
|
|
|
|
|
|
2019-07-16 19:16:27 +08:00
|
|
|
```javascript
|
2019-10-11 05:11:05 +08:00
|
|
|
/******/ (() => { // webpackBootstrap
|
2019-03-31 22:17:22 +08:00
|
|
|
/******/ "use strict";
|
2019-10-11 05:11:05 +08:00
|
|
|
/******/ var __webpack_modules__ = ([
|
2020-05-21 05:26:51 +08:00
|
|
|
/* 0 */,
|
2019-03-31 22:17:22 +08:00
|
|
|
/* 1 */
|
|
|
|
|
/*!*************************!*\
|
|
|
|
|
!*** ./images/file.png ***!
|
|
|
|
|
\*************************/
|
2020-05-21 05:26:51 +08:00
|
|
|
/*! default exports */
|
|
|
|
|
/*! exports [not provided] [maybe used (runtime-defined)] */
|
2019-10-11 05:11:05 +08:00
|
|
|
/*! runtime requirements: module, __webpack_require__.p, __webpack_require__.* */
|
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
2019-03-31 22:17:22 +08:00
|
|
|
|
2020-05-21 05:26:51 +08:00
|
|
|
module.exports = __webpack_require__.p + "images/11744e207f673e90d075.png";
|
2019-03-31 22:17:22 +08:00
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
/* 2 */
|
|
|
|
|
/*!*************************!*\
|
|
|
|
|
!*** ./images/file.jpg ***!
|
|
|
|
|
\*************************/
|
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-03-31 22:17:22 +08:00
|
|
|
|
2019-11-19 21:24:22 +08:00
|
|
|
module.exports = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAA...4CD/9M//Z";
|
2019-03-31 22:17:22 +08:00
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
/* 3 */
|
|
|
|
|
/*!*************************!*\
|
|
|
|
|
!*** ./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-03-31 22:17:22 +08:00
|
|
|
|
2019-11-19 21:24:22 +08:00
|
|
|
module.exports = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=";
|
2019-03-31 22:17:22 +08:00
|
|
|
|
|
|
|
|
/***/ })
|
2019-10-11 05:11:05 +08:00
|
|
|
/******/ ]);
|
2019-11-19 21:10:28 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<details><summary><code>/* webpack runtime code */</code></summary>
|
|
|
|
|
|
2020-09-02 21:07:15 +08:00
|
|
|
```js
|
2019-10-11 05:11:05 +08:00
|
|
|
/************************************************************************/
|
2020-09-02 21:07:15 +08:00
|
|
|
/******/ // 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/publicPath */
|
|
|
|
|
/******/
|
2019-10-11 05:11:05 +08:00
|
|
|
/************************************************************************/
|
2020-09-02 21:07:15 +08:00
|
|
|
/******/ /******/ (() => {
|
|
|
|
|
/******/ __webpack_require__.p = "dist/";
|
|
|
|
|
/******/
|
|
|
|
|
})();
|
|
|
|
|
/******/
|
2019-10-11 05:11:05 +08:00
|
|
|
/************************************************************************/
|
2019-11-19 21:10:28 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
2020-09-02 21:07:15 +08:00
|
|
|
```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_png__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./images/file.png */ 1);
|
|
|
|
|
/* harmony import */ var _images_file_jpg__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./images/file.jpg */ 2);
|
|
|
|
|
/* harmony import */ var _images_file_svg__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./images/file.svg */ 3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_png__WEBPACK_IMPORTED_MODULE_0__, _images_file_jpg__WEBPACK_IMPORTED_MODULE_1__, _images_file_svg__WEBPACK_IMPORTED_MODULE_2__].forEach(src => {
|
|
|
|
|
createImageElement(src.split(".").pop(), src);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
2019-10-11 05:11:05 +08:00
|
|
|
/******/ })()
|
|
|
|
|
;
|
2019-03-31 22:17:22 +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
|
|
|
images/11744e207f673e90d075.png 14.6 KiB [emitted] [immutable] [name: (main)]
|
|
|
|
|
output.js 12.3 KiB [emitted] [name: main]
|
|
|
|
|
Entrypoint main = output.js (images/11744e207f673e90d075.png)
|
|
|
|
|
chunk output.js (main) 9.58 KiB (javascript) 14.6 KiB (asset) 32 bytes (runtime) [entry] [rendered]
|
2019-03-31 22:17:22 +08:00
|
|
|
> ./example.js main
|
2019-10-11 05:11:05 +08:00
|
|
|
./example.js 742 bytes [built]
|
2019-03-31 22:17:22 +08:00
|
|
|
[no exports]
|
2020-05-21 05:26:51 +08:00
|
|
|
[no exports used]
|
2019-03-31 22:17:22 +08:00
|
|
|
entry ./example.js main
|
2019-11-26 20:56:27 +08:00
|
|
|
./images/file.jpg 7.92 KiB [built]
|
2019-11-19 21:48:46 +08:00
|
|
|
[no exports]
|
2019-10-11 05:11:05 +08:00
|
|
|
harmony side effect evaluation ./images/file.jpg ./example.js 2:0-36
|
|
|
|
|
harmony import specifier ./images/file.jpg ./example.js 28:6-9
|
2019-11-18 21:29:19 +08:00
|
|
|
./images/file.png 42 bytes (javascript) 14.6 KiB (asset) [built]
|
2019-11-19 21:48:46 +08:00
|
|
|
[no exports]
|
2019-10-11 05:11:05 +08:00
|
|
|
harmony side effect evaluation ./images/file.png ./example.js 1:0-36
|
|
|
|
|
harmony import specifier ./images/file.png ./example.js 28:1-4
|
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-10-11 05:11:05 +08:00
|
|
|
harmony side effect evaluation ./images/file.svg ./example.js 3:0-36
|
|
|
|
|
harmony import specifier ./images/file.svg ./example.js 28:11-14
|
2020-05-21 05:26:51 +08:00
|
|
|
+ 1 hidden chunk module
|
2019-03-31 22:17:22 +08:00
|
|
|
```
|