人妻少妇精品久久久久久蜜臀av,久久综合激激的五月天,日韩精品无码专区免费播放,欧美精品999

wx5c(索尼wx5c)

前沿拓展:


1.6.4 加載 images 圖像

假如,現在我們正在下載 CSS,但是像 background 和 icon 這樣的圖像,要如何處理呢?在 webpack 5 中,可以使用內置的 Asset Modules,我們可以輕松地將這些內容混入我們的系統(tǒng)中,這個我們在"資源模塊"一節(jié)中已經介紹了。這里再補充一個知識點,在 css文件里也可以直接引用文件,修改 style.css 和入口 index.js

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.classList.add('block-bg')

07-manage-assets/src/style.css

.hello {
background-color: #f9efd4;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

07-manage-assets/src/index.js

// 導入模塊
//…
import './style.css'

//…
block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.textContent = exampleText
block.classList.add('block-bg')
document.body.appendChild(block)

//…

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們看到,通過樣式把背景圖片加到了頁面中。

1.6.5 加載 fonts 字體

那么,像字體這樣的其他資源如何處理呢?使用 Asset Modules 可以接收并加載任何文件,第二將其輸出到構建目錄。這就是說,我們可以將它們用于任何類型的文件,也包括字體。讓我們更新 webpack.config.js 來處理字體文件:

module: {
rules: [
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},

//…
}

在項目中添加一些字體文件:

wx5c(索尼wx5c)

?

編輯

添加圖片注釋,不超過 140 字(可選)

配置好 loader 并將字體文件放在合適的位置后,你可以通過一個 @font-face 聲明將其混合。本地的 url(…) 指令會被 webpack 獲取處理,就像它處理圖片一樣:

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

07-manage-assets/src/style.css

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.hello {
background-color: #f9efd4;
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg);
}

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

07-manage-assets/src/index.js

// 導入模塊
//…

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們再打包一下,看看輸出的文件:

[felix] 07-manage-assets $ npx webpack

07-manage-assets/dist/styles/4a9cff551c7a105e1554.css

/*!******************************************************************!*
!*** css ../node_modules/css-loader/dist/cjs.js!./src/style.css ***!
******************************************************************/@font-face{font-family:iconfont;src:url(../images/65b194f1f711865371d1.ttf) format("truetype")}.hello{background-color:#f9efd4}.icon{-webkit-font-**oothing:antialiased;-moz-osx-font-**oothing:grayscale;font-family:iconfont!important;font-size:16px;font-style:normal}.block-bg{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg viewBox='0 0 3046.7 875.7' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m387 0 387 218.9v437.9L387 875.7 0 656.8V218.9z' fill='%23fff'/%3E%3Cpath d='M704.9 641.7 399.8 814.3V679.9l190.1-104.6zm20.9-18.9V261.9l-111.6 64.5v232zM67.9 641.7 373 814.3V679.9L182.8 575.3zM47 622.8V261.9l111.6 64.5v232zm13.1-384.3L373 61.5v129.9L172.5 301.7l-1.6.9zm652.6 0-312.9-177v129.9l200.5 110.2 1.6.9z' fill='%238ed6fb'/%3E%3Cpath d='M373 649.3 185.4 546.1V341.8L373 450.1zm26.8 0 187.6-103.1V341.8L399.8 450.1zM198.1 318.2l188.3-103.5 188.3 103.5-188.3 108.7z' fill='%231c78c0'/%3E%3Cpath d='M1164.3 576.3h82.5l84.1-280.2h-80.4l-49.8 198.8-53.1-198.8H1078l-53.6 198.8-49.3-198.8h-80.4l83.6 280.2h82.5l52-179.5zM1335.2 437c0 84.1 57.3 146.3 147.4 146.3 69.7 0 107.2-41.8 117.9-61.6l-48.8-37c-8 11.8-30 34.3-68.1 34.3-41.3 0-71.3-26.8-72.9-64.3H1608c.5-5.4.5-10.7.5-16.1 0-91.6-49.3-149.5-136.1-149.5-79.9 0-137.2 63.2-137.2 147.9zm77.7-30.6c3.2-32.1 25.7-56.8 60.6-56.8 33.8 0 58.4 22.5 60 56.8zm223.5 169.9h69.7v-28.9c7.5 9.1 35.4 35.9 83.1 35.9 80.4 0 137.2-60.5 137.2-146.8 0-86.8-52.5-147.3-132.9-147.3-48.2 0-76.1 26.8-83.1 36.4V188.9h-73.9v387.4zm71.8-139.3c0-52.5 31.1-82.5 71.8-82.5 42.9 0 71.8 33.8 71.8 82.5 0 49.8-30 80.9-71.8 80.9-45 0-71.8-36.5-71.8-80.9zm247 239.5h73.9V547.3c7 9.1 34.8 35.9 83.1 35.9 80.4 0 132.9-60.5 132.9-147.3 0-85.7-56.8-146.8-137.2-146.8-47.7 0-75.6 26.8-83.1 36.4V296h-69.7v380.5zm71.8-241.1c0-44.5 26.8-80.9 71.8-80.9 41.8 0 71.8 31.1 71.8 80.9 0 48.8-28.9 82.5-71.8 82.5-40.7 0-71.8-30-71.8-82.5zm231.5 54.1c0 58.9 48.2 93.8 105 93.8 32.2 0 53.6-9.6 68.1-25.2l4.8 18.2h65.4V398.9c0-62.7-26.8-109.8-116.8-109.8-42.9 0-85.2 16.1-110.4 33.2l27.9 50.4a165.2 165.2 0 0 1 74.5-19.8c32.7 0 50.9 16.6 50.9 41.3v18.2c-10.2-7-32.2-15.5-60.6-15.5-65.4-.1-108.8 37.4-108.8 92.6zm73.9-2.2c0-23 19.8-39.1 48.2-39.1s48.8 14.5 48.8 39.1c0 23.6-20.4 38.6-48.2 38.6s-48.8-15.5-48.8-38.6zm348.9 30.6c-46.6 0-79.8-33.8-79.8-81.4 0-45 29.5-82 77.2-82a95.2 95.2 0 0 1 65.4 26.8l20.9-62.2a142.6 142.6 0 0 0-88.4-30c-85.2 0-149 62.7-149 147.9s62.2 146.3 149.5 146.3a141 141 0 0 0 87.3-30l-19.8-60.5c-12.4 10.1-34.9 25.1-63.3 25.1zm110.9 58.4h73.9V431.6l93.8 144.7h86.8L2940.6 423l98.6-127h-83.1l-90 117.9v-225h-73.9z' fill='%23f5fafa'/%3E%3C/svg%3E")}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3R5bGVzLzRhOWNmZjU1MWM3YTEwNWUxNTU0LmNzcyIsIm1hcHBpbmdzIjoiQUFBQTs7cUVBRXFFLENDRnJFLFdBQ0U**0**QXVCLENBQ3ZCLDhEQUNGLENBRUEsT0FDRSx3QkFDRixDQUVBLE1BSUUsa0NBQW1DLENBQ25DLGlDQUFrQyxDQUpsQyw4QkFBa0MsQ0FDbEMsY0FBZSxDQUNmLGlCQUdGLENBRUEsVUFDRSw0d0VBQ0YiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vNGE5Y2ZmNTUxYzdhMTA1ZTE1NTQuY3NzIiwid2VicGFjazovLy8uL3NyYy9zdHlsZS5jc3MiXSwic291cmNlc0NvbnRlbnQiOlsiLyohKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqISpcXFxuICAhKioqIGNzcyAuLi9ub2RlX21vZHVsZXMvY3NzLWxvYWRlci9kaXN0L2Nqcy5qcyEuL3NyYy9zdHlsZS5jc3MgKioqIVxuICBcXCoqKioqKioqKioqKioqKioqKioqKioqKio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IDI2Ljh**jAuOS02Mi4yYTE0Mi42IDE0Mi42IDAgMCAwIC04OC40LTMwYy04NS4yIDAtMTQ5IDYyLjctMTQ5IDE0Ny45czYyLjIgMTQ2LjMgMTQ5LjUgMTQ2LjNhMTQxIDE0MSAwIDAgMCA4Ny4zLTMwbC0xOS44LTYwLjVjLTEyLjQgMTAuMS0zNC45IDI1LjEtNjMuMyAyNS4xem0xMTAuOSA1OC40aDczLjl2LTE0NC43bDkzLjggMTQ0LjdoODYuOGwtMTA2LjEtMTUzLjMgOTguNi0xMjdoLTgzLjFsLTkwIDExNy45di0yMjVoLTczLjl6JyBmaWxsPSclMjNmNWZhZmEnLyUzZSUzYy9zdmclM2VcIikgO1xufVxuIiwiQGZvbnQtZmFjZ**7XG4gIGZvbnQtZmFtaWx5OiAnaWNvbmZvbnQnO1xuICBzcmM6IHVybCgnLi9hc3NldHMvaWNvbmZvbnQudHRmJykgZm9ybWF0KCd0cnVldHlwZScpO1xufVxuXG4uaGV**G8ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiAjZjllZmQ0O1xufVxuXG4uaWNvbiB7XG4gIGZvbnQtZmFtaWx5OiBcIml**25mb250XCIgIWltcG9ydGFudDtcbiAgZm9udC1zaXplOiAxNnB4O1xuICBmb250LXN0eWxlOiBub3JtYWw7XG4gIC13ZWJraXQtZm9udC1zbW9vdGhpbmc6IGFudGlhbGlhc2VkO1xuICAtbW96LW9zeC1mb250LXNtb290aGluZzogZ3JheXNjYWxlO1xufVxuXG4uYmxvY2stYmcge1xuICBiYWNrZ3JvdW5kLWltYWdlOiB1cmwoLi9hc3NldHMvd2VicGFjay1**2**LnN2ZykgO1xufSJdLCJuYW1lcyI6W10sInNvdXJjZVJvb3QiOiIifQ==*/

由于在前面我們應用了如下配置,使生產環(huán)境css 文件也進行了壓縮處理。我們可以注釋它:

optimization: {
// minimize: true,
}1.6.6 加載數據

此外,可以加載的有用資源還有數據,如 JSON 文件,CSV、TSV 和 XML。類似于 NodeJS,JSON 支持實際上是內置的,也就是說 import Data from './data.json' 默認將正常運行。要導入 CSV、TSV 和 XML,你可以使用 csv-loader 和 xml-loader。讓我們處理加載這三類文件:

[felix] webpack5 $ npm install –save-dev csv-loader xml-loader

添加配置:

module: {
rules: [
{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
],
},

//…
}

現在,你可以 import 這四種類型的數據(JSON, CSV, TSV, XML)中的任何一種,所導入的 Data 變量,將包含可直接使用的已解析 JSON:

創(chuàng)建兩個文件:

07-manage-assets/src/assets/data.xml

<?xml version="1.0" encoding="UTF-8"?>

<note>
<to>Mary</to>
<from>John</from>
<heading>Reminder</heading>
<body>Call Cindy on Tuesday</body>
</note>

07-manage-assets/src/assets/data.csv

to,from,heading,body
Mary,John,Reminder,Call Cindy on Tuesday
Zoe,Bill,Reminder,Buy orange juice
Autumn,Lindsey,Letter,I miss you

在入口文件里加載數據模塊,并在控制臺上打印導入內容:

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

console.log(Data)
console.log(Notes)

07-manage-assets/src/index.js

// 導入模塊
//…

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

//…

console.log(Data)
console.log(Notes)

查看開發(fā)者工具中的控制臺,你應該能夠看到導入的數據會被打印出來!

[felix] 07-manage-assets $ npx webpack servewx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

由此可見,data.xml文件轉化為一個JS對象,data.cvs轉化為一個數組。

1.6.7 自定義 JSON 模塊 parser

通過使用 自定義 parser 替代特定的 webpack loader,可以將任何 toml、yamljson5 文件作為 JSON 模塊導入。

假設你在 src 文件夾下有一個 data.toml、一個 data.yaml 以及一個 data.json5 文件:

07-manage-assets/src/assets/json/data.toml

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEOnLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z

07-manage-assets/src/assets/json/data.yaml

title: YAML Exampleowner:name: Tom Preston-Wernerorganization: GitHubbio: |-GitHub Cofounder & CEOLikes tater tots and beer.dob: 1979-05-27T07:32:00.000Z

07-manage-assets/src/assets/json/data.json5

{
// comment
title: 'JSON5 Example',
owner: {
name: 'Tom Preston-Werner',
organization: 'GitHub',
bio: 'GitHub Cofounder & CEOn
Likes tater tots and beer.',
dob: '1979-05-27T07:32:00.000Z',
},
}

第一安裝 toml,yamljsjson5 的 packages:

[felix] webpack5 $ npm install toml yamljs json5 –save-dev

并在你的 webpack 中配置它們:

const toml = require('toml');
const yaml = require('yamljs');
const json5 = require('json5');

module.exports = {
module: {
rules: [
{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
]
}
}

07-manage-assets/webpack.config.js

//…

const toml = require('toml')
const yaml = require('yamljs')
const json5 = require('json5')

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
],
},

//…
}

在主文件中引入模塊,并打印內容:

import toml from './data.toml';
import yaml from './data.yaml';
import json from './data.json5';

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

07-manage-assets/src/index.js

// 導入模塊
//…
import toml from './assets/json/data.toml'
import yaml from './assets/json/data.yaml'
import json from './assets/json/data.json5'

//…

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

啟動服務,打開瀏覽器:

[felix] 07-manage-assets $ npx webpack serve

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

現在,toml、yamljson5幾個類型的文件都正常輸出了結果。

拓展知識:

wx5c

WX5C
1400萬
24的廣角
全景
3D全景
虛化模式
新加入的美膚功能
這款感光度也提高了,拍出的相片很漂亮

IXUS130
除了超薄機身,快門速度快,沒其他功能

wx5c

WX5C,背照式CMOS感光元件是最大優(yōu)勢,ISO800照片仍可用,130在ISO800下已比較模糊了,第三就是大光圈,適合拍人像
而且3D拍攝也非常吸引人

wx5c

本人
推薦
佳能130

佳能
是專業(yè)的

索尼
大多數只是外觀好看

性價比
不如
佳能好

wx5c

當然是佳能IXUS130I好,索尼的產品只是注重外觀,性能與佳能還是有一定差距。

wx5c

佳能比索尼好

索尼的外觀設計好,但拍照性能未必能勝過佳能

要外觀好看,就選索尼,要拍出來的照片好看,就選佳能

前沿拓展:


1.6.4 加載 images 圖像

假如,現在我們正在下載 CSS,但是像 background 和 icon 這樣的圖像,要如何處理呢?在 webpack 5 中,可以使用內置的 Asset Modules,我們可以輕松地將這些內容混入我們的系統(tǒng)中,這個我們在"資源模塊"一節(jié)中已經介紹了。這里再補充一個知識點,在 css文件里也可以直接引用文件,修改 style.css 和入口 index.js

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.classList.add('block-bg')

07-manage-assets/src/style.css

.hello {
background-color: #f9efd4;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

07-manage-assets/src/index.js

// 導入模塊
//…
import './style.css'

//…
block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.textContent = exampleText
block.classList.add('block-bg')
document.body.appendChild(block)

//…

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們看到,通過樣式把背景圖片加到了頁面中。

1.6.5 加載 fonts 字體

那么,像字體這樣的其他資源如何處理呢?使用 Asset Modules 可以接收并加載任何文件,第二將其輸出到構建目錄。這就是說,我們可以將它們用于任何類型的文件,也包括字體。讓我們更新 webpack.config.js 來處理字體文件:

module: {
rules: [
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},

//…
}

在項目中添加一些字體文件:

wx5c(索尼wx5c)

?

編輯

添加圖片注釋,不超過 140 字(可選)

配置好 loader 并將字體文件放在合適的位置后,你可以通過一個 @font-face 聲明將其混合。本地的 url(…) 指令會被 webpack 獲取處理,就像它處理圖片一樣:

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

07-manage-assets/src/style.css

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.hello {
background-color: #f9efd4;
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg);
}

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

07-manage-assets/src/index.js

// 導入模塊
//…

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們再打包一下,看看輸出的文件:

[felix] 07-manage-assets $ npx webpack

07-manage-assets/dist/styles/4a9cff551c7a105e1554.css

/*!******************************************************************!*
!*** css ../node_modules/css-loader/dist/cjs.js!./src/style.css ***!
******************************************************************/@font-face{font-family:iconfont;src:url(../images/65b194f1f711865371d1.ttf) format("truetype")}.hello{background-color:#f9efd4}.icon{-webkit-font-**oothing:antialiased;-moz-osx-font-**oothing:grayscale;font-family:iconfont!important;font-size:16px;font-style:normal}.block-bg{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg viewBox='0 0 3046.7 875.7' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m387 0 387 218.9v437.9L387 875.7 0 656.8V218.9z' fill='%23fff'/%3E%3Cpath d='M704.9 641.7 399.8 814.3V679.9l190.1-104.6zm20.9-18.9V261.9l-111.6 64.5v232zM67.9 641.7 373 814.3V679.9L182.8 575.3zM47 622.8V261.9l111.6 64.5v232zm13.1-384.3L373 61.5v129.9L172.5 301.7l-1.6.9zm652.6 0-312.9-177v129.9l200.5 110.2 1.6.9z' fill='%238ed6fb'/%3E%3Cpath d='M373 649.3 185.4 546.1V341.8L373 450.1zm26.8 0 187.6-103.1V341.8L399.8 450.1zM198.1 318.2l188.3-103.5 188.3 103.5-188.3 108.7z' fill='%231c78c0'/%3E%3Cpath d='M1164.3 576.3h82.5l84.1-280.2h-80.4l-49.8 198.8-53.1-198.8H1078l-53.6 198.8-49.3-198.8h-80.4l83.6 280.2h82.5l52-179.5zM1335.2 437c0 84.1 57.3 146.3 147.4 146.3 69.7 0 107.2-41.8 117.9-61.6l-48.8-37c-8 11.8-30 34.3-68.1 34.3-41.3 0-71.3-26.8-72.9-64.3H1608c.5-5.4.5-10.7.5-16.1 0-91.6-49.3-149.5-136.1-149.5-79.9 0-137.2 63.2-137.2 147.9zm77.7-30.6c3.2-32.1 25.7-56.8 60.6-56.8 33.8 0 58.4 22.5 60 56.8zm223.5 169.9h69.7v-28.9c7.5 9.1 35.4 35.9 83.1 35.9 80.4 0 137.2-60.5 137.2-146.8 0-86.8-52.5-147.3-132.9-147.3-48.2 0-76.1 26.8-83.1 36.4V188.9h-73.9v387.4zm71.8-139.3c0-52.5 31.1-82.5 71.8-82.5 42.9 0 71.8 33.8 71.8 82.5 0 49.8-30 80.9-71.8 80.9-45 0-71.8-36.5-71.8-80.9zm247 239.5h73.9V547.3c7 9.1 34.8 35.9 83.1 35.9 80.4 0 132.9-60.5 132.9-147.3 0-85.7-56.8-146.8-137.2-146.8-47.7 0-75.6 26.8-83.1 36.4V296h-69.7v380.5zm71.8-241.1c0-44.5 26.8-80.9 71.8-80.9 41.8 0 71.8 31.1 71.8 80.9 0 48.8-28.9 82.5-71.8 82.5-40.7 0-71.8-30-71.8-82.5zm231.5 54.1c0 58.9 48.2 93.8 105 93.8 32.2 0 53.6-9.6 68.1-25.2l4.8 18.2h65.4V398.9c0-62.7-26.8-109.8-116.8-109.8-42.9 0-85.2 16.1-110.4 33.2l27.9 50.4a165.2 165.2 0 0 1 74.5-19.8c32.7 0 50.9 16.6 50.9 41.3v18.2c-10.2-7-32.2-15.5-60.6-15.5-65.4-.1-108.8 37.4-108.8 92.6zm73.9-2.2c0-23 19.8-39.1 48.2-39.1s48.8 14.5 48.8 39.1c0 23.6-20.4 38.6-48.2 38.6s-48.8-15.5-48.8-38.6zm348.9 30.6c-46.6 0-79.8-33.8-79.8-81.4 0-45 29.5-82 77.2-82a95.2 95.2 0 0 1 65.4 26.8l20.9-62.2a142.6 142.6 0 0 0-88.4-30c-85.2 0-149 62.7-149 147.9s62.2 146.3 149.5 146.3a141 141 0 0 0 87.3-30l-19.8-60.5c-12.4 10.1-34.9 25.1-63.3 25.1zm110.9 58.4h73.9V431.6l93.8 144.7h86.8L2940.6 423l98.6-127h-83.1l-90 117.9v-225h-73.9z' fill='%23f5fafa'/%3E%3C/svg%3E")}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaW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3NldHMvaWNvbmZvbnQudHRmJykgZm9ybWF0KCd0cnVldHlwZScpO1xufVxuXG4uaGV**G8ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiAjZjllZmQ0O1xufVxuXG4uaWNvbiB7XG4gIGZvbnQtZmFtaWx5OiBcIml**25mb250XCIgIWltcG9ydGFudDtcbiAgZm9udC1zaXplOiAxNnB4O1xuICBmb250LXN0eWxlOiBub3JtYWw7XG4gIC13ZWJraXQtZm9udC1zbW9vdGhpbmc6IGFudGlhbGlhc2VkO1xuICAtbW96LW9zeC1mb250LXNtb290aGluZzogZ3JheXNjYWxlO1xufVxuXG4uYmxvY2stYmcge1xuICBiYWNrZ3JvdW5kLWltYWdlOiB1cmwoLi9hc3NldHMvd2VicGFjay1**2**LnN2ZykgO1xufSJdLCJuYW1lcyI6W10sInNvdXJjZVJvb3QiOiIifQ==*/

由于在前面我們應用了如下配置,使生產環(huán)境css 文件也進行了壓縮處理。我們可以注釋它:

optimization: {
// minimize: true,
}1.6.6 加載數據

此外,可以加載的有用資源還有數據,如 JSON 文件,CSV、TSV 和 XML。類似于 NodeJS,JSON 支持實際上是內置的,也就是說 import Data from './data.json' 默認將正常運行。要導入 CSV、TSV 和 XML,你可以使用 csv-loader 和 xml-loader。讓我們處理加載這三類文件:

[felix] webpack5 $ npm install –save-dev csv-loader xml-loader

添加配置:

module: {
rules: [
{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
],
},

//…
}

現在,你可以 import 這四種類型的數據(JSON, CSV, TSV, XML)中的任何一種,所導入的 Data 變量,將包含可直接使用的已解析 JSON:

創(chuàng)建兩個文件:

07-manage-assets/src/assets/data.xml

<?xml version="1.0" encoding="UTF-8"?>

<note>
<to>Mary</to>
<from>John</from>
<heading>Reminder</heading>
<body>Call Cindy on Tuesday</body>
</note>

07-manage-assets/src/assets/data.csv

to,from,heading,body
Mary,John,Reminder,Call Cindy on Tuesday
Zoe,Bill,Reminder,Buy orange juice
Autumn,Lindsey,Letter,I miss you

在入口文件里加載數據模塊,并在控制臺上打印導入內容:

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

console.log(Data)
console.log(Notes)

07-manage-assets/src/index.js

// 導入模塊
//…

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

//…

console.log(Data)
console.log(Notes)

查看開發(fā)者工具中的控制臺,你應該能夠看到導入的數據會被打印出來!

[felix] 07-manage-assets $ npx webpack servewx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

由此可見,data.xml文件轉化為一個JS對象,data.cvs轉化為一個數組。

1.6.7 自定義 JSON 模塊 parser

通過使用 自定義 parser 替代特定的 webpack loader,可以將任何 tomlyamljson5 文件作為 JSON 模塊導入。

假設你在 src 文件夾下有一個 data.toml、一個 data.yaml 以及一個 data.json5 文件:

07-manage-assets/src/assets/json/data.toml

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEOnLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z

07-manage-assets/src/assets/json/data.yaml

title: YAML Exampleowner:name: Tom Preston-Wernerorganization: GitHubbio: |-GitHub Cofounder & CEOLikes tater tots and beer.dob: 1979-05-27T07:32:00.000Z

07-manage-assets/src/assets/json/data.json5

{
// comment
title: 'JSON5 Example',
owner: {
name: 'Tom Preston-Werner',
organization: 'GitHub',
bio: 'GitHub Cofounder & CEOn
Likes tater tots and beer.',
dob: '1979-05-27T07:32:00.000Z',
},
}

第一安裝 tomlyamljsjson5 的 packages:

[felix] webpack5 $ npm install toml yamljs json5 –save-dev

并在你的 webpack 中配置它們:

const toml = require('toml');
const yaml = require('yamljs');
const json5 = require('json5');

module.exports = {
module: {
rules: [
{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
]
}
}

07-manage-assets/webpack.config.js

//…

const toml = require('toml')
const yaml = require('yamljs')
const json5 = require('json5')

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
],
},

//…
}

在主文件中引入模塊,并打印內容:

import toml from './data.toml';
import yaml from './data.yaml';
import json from './data.json5';

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

07-manage-assets/src/index.js

// 導入模塊
//…
import toml from './assets/json/data.toml'
import yaml from './assets/json/data.yaml'
import json from './assets/json/data.json5'

//…

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

啟動服務,打開瀏覽器:

[felix] 07-manage-assets $ npx webpack serve

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

現在,toml、yamljson5幾個類型的文件都正常輸出了結果。

拓展知識:

wx5c

WX5C
1400萬
24的廣角
全景
3D全景
虛化模式
新加入的美膚功能
這款感光度也提高了,拍出的相片很漂亮

IXUS130
除了超薄機身,快門速度快,沒其他功能

wx5c

WX5C,背照式CMOS感光元件是最大優(yōu)勢,ISO800照片仍可用,130在ISO800下已比較模糊了,第三就是大光圈,適合拍人像
而且3D拍攝也非常吸引人

wx5c

本人
推薦
佳能130

佳能
是專業(yè)的

索尼
大多數只是外觀好看

性價比
不如
佳能好

wx5c

當然是佳能IXUS130I好,索尼的產品只是注重外觀,性能與佳能還是有一定差距。

wx5c

佳能比索尼好

索尼的外觀設計好,但拍照性能未必能勝過佳能

要外觀好看,就選索尼,要拍出來的照片好看,就選佳能

前沿拓展:


1.6.4 加載 images 圖像

假如,現在我們正在下載 CSS,但是像 background 和 icon 這樣的圖像,要如何處理呢?在 webpack 5 中,可以使用內置的 Asset Modules,我們可以輕松地將這些內容混入我們的系統(tǒng)中,這個我們在"資源模塊"一節(jié)中已經介紹了。這里再補充一個知識點,在 css文件里也可以直接引用文件,修改 style.css 和入口 index.js

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.classList.add('block-bg')

07-manage-assets/src/style.css

.hello {
background-color: #f9efd4;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

07-manage-assets/src/index.js

// 導入模塊
//…
import './style.css'

//…
block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.textContent = exampleText
block.classList.add('block-bg')
document.body.appendChild(block)

//…

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們看到,通過樣式把背景圖片加到了頁面中。

1.6.5 加載 fonts 字體

那么,像字體這樣的其他資源如何處理呢?使用 Asset Modules 可以接收并加載任何文件,第二將其輸出到構建目錄。這就是說,我們可以將它們用于任何類型的文件,也包括字體。讓我們更新 webpack.config.js 來處理字體文件:

module: {
rules: [
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},

//…
}

在項目中添加一些字體文件:

wx5c(索尼wx5c)

?

編輯

添加圖片注釋,不超過 140 字(可選)

配置好 loader 并將字體文件放在合適的位置后,你可以通過一個 @font-face 聲明將其混合。本地的 url(…) 指令會被 webpack 獲取處理,就像它處理圖片一樣:

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

07-manage-assets/src/style.css

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.hello {
background-color: #f9efd4;
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg);
}

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

07-manage-assets/src/index.js

// 導入模塊
//…

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們再打包一下,看看輸出的文件:

[felix] 07-manage-assets $ npx webpack

07-manage-assets/dist/styles/4a9cff551c7a105e1554.css

/*!******************************************************************!*
!*** css ../node_modules/css-loader/dist/cjs.js!./src/style.css ***!
******************************************************************/@font-face{font-family:iconfont;src:url(../images/65b194f1f711865371d1.ttf) format("truetype")}.hello{background-color:#f9efd4}.icon{-webkit-font-**oothing:antialiased;-moz-osx-font-**oothing:grayscale;font-family:iconfont!important;font-size:16px;font-style:normal}.block-bg{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg viewBox='0 0 3046.7 875.7' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m387 0 387 218.9v437.9L387 875.7 0 656.8V218.9z' fill='%23fff'/%3E%3Cpath d='M704.9 641.7 399.8 814.3V679.9l190.1-104.6zm20.9-18.9V261.9l-111.6 64.5v232zM67.9 641.7 373 814.3V679.9L182.8 575.3zM47 622.8V261.9l111.6 64.5v232zm13.1-384.3L373 61.5v129.9L172.5 301.7l-1.6.9zm652.6 0-312.9-177v129.9l200.5 110.2 1.6.9z' fill='%238ed6fb'/%3E%3Cpath d='M373 649.3 185.4 546.1V341.8L373 450.1zm26.8 0 187.6-103.1V341.8L399.8 450.1zM198.1 318.2l188.3-103.5 188.3 103.5-188.3 108.7z' fill='%231c78c0'/%3E%3Cpath d='M1164.3 576.3h82.5l84.1-280.2h-80.4l-49.8 198.8-53.1-198.8H1078l-53.6 198.8-49.3-198.8h-80.4l83.6 280.2h82.5l52-179.5zM1335.2 437c0 84.1 57.3 146.3 147.4 146.3 69.7 0 107.2-41.8 117.9-61.6l-48.8-37c-8 11.8-30 34.3-68.1 34.3-41.3 0-71.3-26.8-72.9-64.3H1608c.5-5.4.5-10.7.5-16.1 0-91.6-49.3-149.5-136.1-149.5-79.9 0-137.2 63.2-137.2 147.9zm77.7-30.6c3.2-32.1 25.7-56.8 60.6-56.8 33.8 0 58.4 22.5 60 56.8zm223.5 169.9h69.7v-28.9c7.5 9.1 35.4 35.9 83.1 35.9 80.4 0 137.2-60.5 137.2-146.8 0-86.8-52.5-147.3-132.9-147.3-48.2 0-76.1 26.8-83.1 36.4V188.9h-73.9v387.4zm71.8-139.3c0-52.5 31.1-82.5 71.8-82.5 42.9 0 71.8 33.8 71.8 82.5 0 49.8-30 80.9-71.8 80.9-45 0-71.8-36.5-71.8-80.9zm247 239.5h73.9V547.3c7 9.1 34.8 35.9 83.1 35.9 80.4 0 132.9-60.5 132.9-147.3 0-85.7-56.8-146.8-137.2-146.8-47.7 0-75.6 26.8-83.1 36.4V296h-69.7v380.5zm71.8-241.1c0-44.5 26.8-80.9 71.8-80.9 41.8 0 71.8 31.1 71.8 80.9 0 48.8-28.9 82.5-71.8 82.5-40.7 0-71.8-30-71.8-82.5zm231.5 54.1c0 58.9 48.2 93.8 105 93.8 32.2 0 53.6-9.6 68.1-25.2l4.8 18.2h65.4V398.9c0-62.7-26.8-109.8-116.8-109.8-42.9 0-85.2 16.1-110.4 33.2l27.9 50.4a165.2 165.2 0 0 1 74.5-19.8c32.7 0 50.9 16.6 50.9 41.3v18.2c-10.2-7-32.2-15.5-60.6-15.5-65.4-.1-108.8 37.4-108.8 92.6zm73.9-2.2c0-23 19.8-39.1 48.2-39.1s48.8 14.5 48.8 39.1c0 23.6-20.4 38.6-48.2 38.6s-48.8-15.5-48.8-38.6zm348.9 30.6c-46.6 0-79.8-33.8-79.8-81.4 0-45 29.5-82 77.2-82a95.2 95.2 0 0 1 65.4 26.8l20.9-62.2a142.6 142.6 0 0 0-88.4-30c-85.2 0-149 62.7-149 147.9s62.2 146.3 149.5 146.3a141 141 0 0 0 87.3-30l-19.8-60.5c-12.4 10.1-34.9 25.1-63.3 25.1zm110.9 58.4h73.9V431.6l93.8 144.7h86.8L2940.6 423l98.6-127h-83.1l-90 117.9v-225h-73.9z' fill='%23f5fafa'/%3E%3C/svg%3E")}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3R5bGVzLzRhOWNmZjU1MWM3YTEwNWUxNTU0LmNzcyIsIm1hcHBpbmdzIjoiQUFBQTs7cUVBRXFFLENDRnJFLFdBQ0U**0**QXVCLENBQ3ZCLDhEQUNGLENBRUEsT0FDRSx3QkFDRixDQUVBLE1BSUUsa0NBQW1DLENBQ25DLGlDQUFrQyxDQUpsQyw4QkFBa0MsQ0FDbEMsY0FBZSxDQUNmLGlCQUdGLENBRUEsVUFDRSw0d0VBQ0YiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vNGE5Y2ZmNTUxYzdhMTA1ZTE1NTQuY3NzIiwid2VicGFjazovLy8uL3NyYy9zdHlsZS5jc3MiXSwic291cmNlc0NvbnRlbnQiOlsiLyohKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqIS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

由于在前面我們應用了如下配置,使生產環(huán)境css 文件也進行了壓縮處理。我們可以注釋它:

optimization: {
// minimize: true,
}1.6.6 加載數據

此外,可以加載的有用資源還有數據,如 JSON 文件,CSV、TSV 和 XML。類似于 NodeJS,JSON 支持實際上是內置的,也就是說 import Data from './data.json' 默認將正常運行。要導入 CSV、TSV 和 XML,你可以使用 csv-loader 和 xml-loader。讓我們處理加載這三類文件:

[felix] webpack5 $ npm install –save-dev csv-loader xml-loader

添加配置:

module: {
rules: [
{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
],
},

//…
}

現在,你可以 import 這四種類型的數據(JSON, CSV, TSV, XML)中的任何一種,所導入的 Data 變量,將包含可直接使用的已解析 JSON:

創(chuàng)建兩個文件:

07-manage-assets/src/assets/data.xml

<?xml version="1.0" encoding="UTF-8"?>

<note>
<to>Mary</to>
<from>John</from>
<heading>Reminder</heading>
<body>Call Cindy on Tuesday</body>
</note>

07-manage-assets/src/assets/data.csv

to,from,heading,body
Mary,John,Reminder,Call Cindy on Tuesday
Zoe,Bill,Reminder,Buy orange juice
Autumn,Lindsey,Letter,I miss you

在入口文件里加載數據模塊,并在控制臺上打印導入內容:

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

console.log(Data)
console.log(Notes)

07-manage-assets/src/index.js

// 導入模塊
//…

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

//…

console.log(Data)
console.log(Notes)

查看開發(fā)者工具中的控制臺,你應該能夠看到導入的數據會被打印出來!

[felix] 07-manage-assets $ npx webpack servewx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

由此可見,data.xml文件轉化為一個JS對象,data.cvs轉化為一個數組。

1.6.7 自定義 JSON 模塊 parser

通過使用 自定義 parser 替代特定的 webpack loader,可以將任何 toml、yamljson5 文件作為 JSON 模塊導入。

假設你在 src 文件夾下有一個 data.toml、一個 data.yaml 以及一個 data.json5 文件:

07-manage-assets/src/assets/json/data.toml

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEOnLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z

07-manage-assets/src/assets/json/data.yaml

title: YAML Exampleowner:name: Tom Preston-Wernerorganization: GitHubbio: |-GitHub Cofounder & CEOLikes tater tots and beer.dob: 1979-05-27T07:32:00.000Z

07-manage-assets/src/assets/json/data.json5

{
// comment
title: 'JSON5 Example',
owner: {
name: 'Tom Preston-Werner',
organization: 'GitHub',
bio: 'GitHub Cofounder & CEOn
Likes tater tots and beer.',
dob: '1979-05-27T07:32:00.000Z',
},
}

第一安裝 tomlyamljsjson5 的 packages:

[felix] webpack5 $ npm install toml yamljs json5 –save-dev

并在你的 webpack 中配置它們:

const toml = require('toml');
const yaml = require('yamljs');
const json5 = require('json5');

module.exports = {
module: {
rules: [
{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
]
}
}

07-manage-assets/webpack.config.js

//…

const toml = require('toml')
const yaml = require('yamljs')
const json5 = require('json5')

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
],
},

//…
}

在主文件中引入模塊,并打印內容:

import toml from './data.toml';
import yaml from './data.yaml';
import json from './data.json5';

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

07-manage-assets/src/index.js

// 導入模塊
//…
import toml from './assets/json/data.toml'
import yaml from './assets/json/data.yaml'
import json from './assets/json/data.json5'

//…

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

啟動服務,打開瀏覽器:

[felix] 07-manage-assets $ npx webpack serve

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

現在,toml、yamljson5幾個類型的文件都正常輸出了結果。

拓展知識:

wx5c

WX5C
1400萬
24的廣角
全景
3D全景
虛化模式
新加入的美膚功能
這款感光度也提高了,拍出的相片很漂亮

IXUS130
除了超薄機身,快門速度快,沒其他功能

wx5c

WX5C,背照式CMOS感光元件是最大優(yōu)勢,ISO800照片仍可用,130在ISO800下已比較模糊了,第三就是大光圈,適合拍人像
而且3D拍攝也非常吸引人

wx5c

本人
推薦
佳能130

佳能
是專業(yè)的

索尼
大多數只是外觀好看

性價比
不如
佳能好

wx5c

當然是佳能IXUS130I好,索尼的產品只是注重外觀,性能與佳能還是有一定差距。

wx5c

佳能比索尼好

索尼的外觀設計好,但拍照性能未必能勝過佳能

要外觀好看,就選索尼,要拍出來的照片好看,就選佳能

前沿拓展:


1.6.4 加載 images 圖像

假如,現在我們正在下載 CSS,但是像 background 和 icon 這樣的圖像,要如何處理呢?在 webpack 5 中,可以使用內置的 Asset Modules,我們可以輕松地將這些內容混入我們的系統(tǒng)中,這個我們在"資源模塊"一節(jié)中已經介紹了。這里再補充一個知識點,在 css文件里也可以直接引用文件,修改 style.css 和入口 index.js

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.classList.add('block-bg')

07-manage-assets/src/style.css

.hello {
background-color: #f9efd4;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg) ;
}

07-manage-assets/src/index.js

// 導入模塊
//…
import './style.css'

//…
block.style.cssText = `width: 200px; height: 200px; background-color: #2b3a42`
block.textContent = exampleText
block.classList.add('block-bg')
document.body.appendChild(block)

//…

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們看到,通過樣式把背景圖片加到了頁面中。

1.6.5 加載 fonts 字體

那么,像字體這樣的其他資源如何處理呢?使用 Asset Modules 可以接收并加載任何文件,第二將其輸出到構建目錄。這就是說,我們可以將它們用于任何類型的文件,也包括字體。讓我們更新 webpack.config.js 來處理字體文件:

module: {
rules: [
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},

//…
}

在項目中添加一些字體文件:

wx5c(索尼wx5c)

?

編輯

添加圖片注釋,不超過 140 字(可選)

配置好 loader 并將字體文件放在合適的位置后,你可以通過一個 @font-face 聲明將其混合。本地的 url(…) 指令會被 webpack 獲取處理,就像它處理圖片一樣:

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

07-manage-assets/src/style.css

@font-face {
font-family: 'iconfont';
src: url('./assets/iconfont.ttf') format('truetype');
}

.hello {
background-color: #f9efd4;
}

.icon {
font-family: "iconfont" !important;
font-size: 30px;
font-style: normal;
-webkit-font-**oothing: antialiased;
-moz-osx-font-**oothing: grayscale;
}

.block-bg {
background-image: url(./assets/webpack-logo.svg);
}

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

07-manage-assets/src/index.js

// 導入模塊
//…

const span = document.createElement('span')
span.classList.add('icon')
span.innerHTML = ''
document.body.appendChild(span)

啟動服務,打開瀏覽器:

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

我們再打包一下,看看輸出的文件:

[felix] 07-manage-assets $ npx webpack

07-manage-assets/dist/styles/4a9cff551c7a105e1554.css

/*!******************************************************************!*
!*** css ../node_modules/css-loader/dist/cjs.js!./src/style.css ***!
******************************************************************/@font-face{font-family:iconfont;src:url(../images/65b194f1f711865371d1.ttf) format("truetype")}.hello{background-color:#f9efd4}.icon{-webkit-font-**oothing:antialiased;-moz-osx-font-**oothing:grayscale;font-family:iconfont!important;font-size:16px;font-style:normal}.block-bg{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg viewBox='0 0 3046.7 875.7' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m387 0 387 218.9v437.9L387 875.7 0 656.8V218.9z' fill='%23fff'/%3E%3Cpath d='M704.9 641.7 399.8 814.3V679.9l190.1-104.6zm20.9-18.9V261.9l-111.6 64.5v232zM67.9 641.7 373 814.3V679.9L182.8 575.3zM47 622.8V261.9l111.6 64.5v232zm13.1-384.3L373 61.5v129.9L172.5 301.7l-1.6.9zm652.6 0-312.9-177v129.9l200.5 110.2 1.6.9z' fill='%238ed6fb'/%3E%3Cpath d='M373 649.3 185.4 546.1V341.8L373 450.1zm26.8 0 187.6-103.1V341.8L399.8 450.1zM198.1 318.2l188.3-103.5 188.3 103.5-188.3 108.7z' fill='%231c78c0'/%3E%3Cpath d='M1164.3 576.3h82.5l84.1-280.2h-80.4l-49.8 198.8-53.1-198.8H1078l-53.6 198.8-49.3-198.8h-80.4l83.6 280.2h82.5l52-179.5zM1335.2 437c0 84.1 57.3 146.3 147.4 146.3 69.7 0 107.2-41.8 117.9-61.6l-48.8-37c-8 11.8-30 34.3-68.1 34.3-41.3 0-71.3-26.8-72.9-64.3H1608c.5-5.4.5-10.7.5-16.1 0-91.6-49.3-149.5-136.1-149.5-79.9 0-137.2 63.2-137.2 147.9zm77.7-30.6c3.2-32.1 25.7-56.8 60.6-56.8 33.8 0 58.4 22.5 60 56.8zm223.5 169.9h69.7v-28.9c7.5 9.1 35.4 35.9 83.1 35.9 80.4 0 137.2-60.5 137.2-146.8 0-86.8-52.5-147.3-132.9-147.3-48.2 0-76.1 26.8-83.1 36.4V188.9h-73.9v387.4zm71.8-139.3c0-52.5 31.1-82.5 71.8-82.5 42.9 0 71.8 33.8 71.8 82.5 0 49.8-30 80.9-71.8 80.9-45 0-71.8-36.5-71.8-80.9zm247 239.5h73.9V547.3c7 9.1 34.8 35.9 83.1 35.9 80.4 0 132.9-60.5 132.9-147.3 0-85.7-56.8-146.8-137.2-146.8-47.7 0-75.6 26.8-83.1 36.4V296h-69.7v380.5zm71.8-241.1c0-44.5 26.8-80.9 71.8-80.9 41.8 0 71.8 31.1 71.8 80.9 0 48.8-28.9 82.5-71.8 82.5-40.7 0-71.8-30-71.8-82.5zm231.5 54.1c0 58.9 48.2 93.8 105 93.8 32.2 0 53.6-9.6 68.1-25.2l4.8 18.2h65.4V398.9c0-62.7-26.8-109.8-116.8-109.8-42.9 0-85.2 16.1-110.4 33.2l27.9 50.4a165.2 165.2 0 0 1 74.5-19.8c32.7 0 50.9 16.6 50.9 41.3v18.2c-10.2-7-32.2-15.5-60.6-15.5-65.4-.1-108.8 37.4-108.8 92.6zm73.9-2.2c0-23 19.8-39.1 48.2-39.1s48.8 14.5 48.8 39.1c0 23.6-20.4 38.6-48.2 38.6s-48.8-15.5-48.8-38.6zm348.9 30.6c-46.6 0-79.8-33.8-79.8-81.4 0-45 29.5-82 77.2-82a95.2 95.2 0 0 1 65.4 26.8l20.9-62.2a142.6 142.6 0 0 0-88.4-30c-85.2 0-149 62.7-149 147.9s62.2 146.3 149.5 146.3a141 141 0 0 0 87.3-30l-19.8-60.5c-12.4 10.1-34.9 25.1-63.3 25.1zm110.9 58.4h73.9V431.6l93.8 144.7h86.8L2940.6 423l98.6-127h-83.1l-90 117.9v-225h-73.9z' fill='%23f5fafa'/%3E%3C/svg%3E")}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3R5bGVzLzRhOWNmZjU1MWM3YTEwNWUxNTU0LmNzcyIsIm1hcHBpbmdzIjoiQUFBQTs7cUVBRXFFLENDRnJFLFdBQ0U**0**QXVCLENBQ3ZCLDhEQUNGLENBRUEsT0FDRSx3QkFDRixDQUVBLE1BSUUsa0NBQW1DLENBQ25DLGlDQUFrQyxDQUpsQyw4QkFBa0MsQ0FDbEMsY0FBZSxDQUNmLGlCQUdGLENBRUEsVUFDRSw0d0VBQ0YiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vNGE5Y2ZmNTUxYzdhMTA1ZTE1NTQuY3NzIiwid2VicGFjazovLy8uL3NyYy9zdHlsZS5jc3MiXSwic291cmNlc0NvbnRlbnQiOlsiLyohKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqISpcXFxuICAhKioqIGNzcyAuLi9ub2RlX21vZHVsZXMvY3NzLWxvYWRlci9kaXN0L2Nqcy5qcyEuL3NyYy9zdHlsZS5jc3MgKioqIVxuICBcXCoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi9cbkBmb250LWZhY2Uge1xuICBmb250LWZhbWlseTogJ2l**25mb250JztcbiAgc3JjOiB1cmwoLi4vaW1hZ2VzLzY1YjE5NGYxZjcxMTg2NTM3MWQxLnR0ZikgZm9ybWF0KCd0cnVldHlwZScpO1xufVxuXG4uaGV**G8ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiAjZjllZmQ0O1xufVxuXG4uaWNvbiB7XG4gIGZvbnQtZmFtaWx5OiBcIml**25mb250XCIgIWltcG9ydGFudDtcbiAgZm9udC1zaXplOiAx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maWxsPSclMjNmNWZhZmEnLyUzZSUzYy9zdmclM2VcIikgO1xufVxuIiwiQGZvbnQtZmFjZ**7XG4gIGZvbnQtZmFtaWx5OiAnaWNvbmZvbnQnO1xuICBzcmM6IHVybCgnLi9hc3NldHMvaWNvbmZvbnQudHRmJykgZm9ybWF0KCd0cnVldHlwZScpO1xufVxuXG4uaGV**G8ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiAjZjllZmQ0O1xufVxuXG4uaWNvbiB7XG4gIGZvbnQtZmFtaWx5OiBcIml**25mb250XCIgIWltcG9ydGFudDtcbiAgZm9udC1zaXplOiAxNnB4O1xuICBmb250LXN0eWxlOiBub3JtYWw7XG4gIC13ZWJraXQtZm9udC1zbW9vdGhpbmc6IGFudGlhbGlhc2VkO1xuICAtbW96LW9zeC1mb250LXNtb290aGluZzogZ3JheXNjYWxlO1xufVxuXG4uYmxvY2stYmcge1xuICBiYWNrZ3JvdW5kLWltYWdlOiB1cmwoLi9hc3NldHMvd2VicGFjay1**2**LnN2ZykgO1xufSJdLCJuYW1lcyI6W10sInNvdXJjZVJvb3QiOiIifQ==*/

由于在前面我們應用了如下配置,使生產環(huán)境css 文件也進行了壓縮處理。我們可以注釋它:

optimization: {
// minimize: true,
}1.6.6 加載數據

此外,可以加載的有用資源還有數據,如 JSON 文件,CSV、TSV 和 XML。類似于 NodeJS,JSON 支持實際上是內置的,也就是說 import Data from './data.json' 默認將正常運行。要導入 CSV、TSV 和 XML,你可以使用 csv-loader 和 xml-loader。讓我們處理加載這三類文件:

[felix] webpack5 $ npm install –save-dev csv-loader xml-loader

添加配置:

module: {
rules: [
{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
]
}

07-manage-assets/webpack.config.js

//…

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /.xml$/i,
use: ['xml-loader'],
},
],
},

//…
}

現在,你可以 import 這四種類型的數據(JSON, CSV, TSV, XML)中的任何一種,所導入的 Data 變量,將包含可直接使用的已解析 JSON:

創(chuàng)建兩個文件:

07-manage-assets/src/assets/data.xml

<?xml version="1.0" encoding="UTF-8"?>

<note>
<to>Mary</to>
<from>John</from>
<heading>Reminder</heading>
<body>Call Cindy on Tuesday</body>
</note>

07-manage-assets/src/assets/data.csv

to,from,heading,body
Mary,John,Reminder,Call Cindy on Tuesday
Zoe,Bill,Reminder,Buy orange juice
Autumn,Lindsey,Letter,I miss you

在入口文件里加載數據模塊,并在控制臺上打印導入內容:

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

console.log(Data)
console.log(Notes)

07-manage-assets/src/index.js

// 導入模塊
//…

import Data from './assets/data.xml'
import Notes from './assets/data.csv'

//…

console.log(Data)
console.log(Notes)

查看開發(fā)者工具中的控制臺,你應該能夠看到導入的數據會被打印出來!

[felix] 07-manage-assets $ npx webpack servewx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

由此可見,data.xml文件轉化為一個JS對象,data.cvs轉化為一個數組。

1.6.7 自定義 JSON 模塊 parser

通過使用 自定義 parser 替代特定的 webpack loader,可以將任何 toml、yamljson5 文件作為 JSON 模塊導入。

假設你在 src 文件夾下有一個 data.toml、一個 data.yaml 以及一個 data.json5 文件:

07-manage-assets/src/assets/json/data.toml

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEOnLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z

07-manage-assets/src/assets/json/data.yaml

title: YAML Exampleowner:name: Tom Preston-Wernerorganization: GitHubbio: |-GitHub Cofounder & CEOLikes tater tots and beer.dob: 1979-05-27T07:32:00.000Z

07-manage-assets/src/assets/json/data.json5

{
// comment
title: 'JSON5 Example',
owner: {
name: 'Tom Preston-Werner',
organization: 'GitHub',
bio: 'GitHub Cofounder & CEOn
Likes tater tots and beer.',
dob: '1979-05-27T07:32:00.000Z',
},
}

第一安裝 toml,yamljsjson5 的 packages:

[felix] webpack5 $ npm install toml yamljs json5 –save-dev

并在你的 webpack 中配置它們:

const toml = require('toml');
const yaml = require('yamljs');
const json5 = require('json5');

module.exports = {
module: {
rules: [
{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
]
}
}

07-manage-assets/webpack.config.js

//…

const toml = require('toml')
const yaml = require('yamljs')
const json5 = require('json5')

module.exports = {
//…

// 配置資源文件
module: {
rules: [
//…

{
test: /.toml$/i,
type: 'json',
parser: {
parse: toml.parse,
},
},
{
test: /.yaml$/i,
type: 'json',
parser: {
parse: yaml.parse,
},
},
{
test: /.json5$/i,
type: 'json',
parser: {
parse: json5.parse,
},
},
],
},

//…
}

在主文件中引入模塊,并打印內容:

import toml from './data.toml';
import yaml from './data.yaml';
import json from './data.json5';

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

07-manage-assets/src/index.js

// 導入模塊
//…
import toml from './assets/json/data.toml'
import yaml from './assets/json/data.yaml'
import json from './assets/json/data.json5'

//…

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

啟動服務,打開瀏覽器:

[felix] 07-manage-assets $ npx webpack serve

wx5c(索尼wx5c)

?

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

現在,toml、yamljson5幾個類型的文件都正常輸出了結果。

拓展知識:

wx5c

WX5C
1400萬
24的廣角
全景
3D全景
虛化模式
新加入的美膚功能
這款感光度也提高了,拍出的相片很漂亮

IXUS130
除了超薄機身,快門速度快,沒其他功能

wx5c

WX5C,背照式CMOS感光元件是最大優(yōu)勢,ISO800照片仍可用,130在ISO800下已比較模糊了,第三就是大光圈,適合拍人像
而且3D拍攝也非常吸引人

wx5c

本人
推薦
佳能130

佳能
是專業(yè)的

索尼
大多數只是外觀好看

性價比
不如
佳能好

wx5c

當然是佳能IXUS130I好,索尼的產品只是注重外觀,性能與佳能還是有一定差距。

wx5c

佳能比索尼好

索尼的外觀設計好,但拍照性能未必能勝過佳能

要外觀好看,就選索尼,要拍出來的照片好看,就選佳能

原創(chuàng)文章,作者:九賢生活小編,如若轉載,請注明出處:http://m.xiesong.cn/89328.html

欧亚午夜成人av| 国产精品麻豆欧美日韩WW| 大香伊人在| 免费在线网站污| 亚洲欧美日韩中文综合| 被绑到房间用各种道具调教| 在线播放免费av| 免费的成人在线视频| 国产91在线|欧美| 彰化县| 国产熟女91熟女| 大香蕉视频久久久| 伊久色综人| 西门庆在线影院| 美女黄色网站在线观看| 少妇99在线观看| 在线 国产 欧美| 道本道网站| 免费一区二区三区无码| 亚洲日韩欧美一区二区三区在线 | 开心色丁香| 大香蕉综| 无码不卡中文字幕av| 凹凸视频一区二区| 亚洲天堂一二三区| 久精在线视频| 国内精品久久久久久久影视麻豆| 伦理偷拍麻豆久久久| 变态另类片影院| 白浆无码久久久久| 无码人妻丰满熟妇区毛片| 久久娱乐精品视频| 超碰97人人做| 精品国产99区| 鹤庆县| 国模无码视频一区二区三区| 欧美熟妇在线资源| 狠狠久久东京热| 123区 日韩精品| 两女女百合互慰AV赤裸无遮挡| 五月婷婷激情综合网|