Skip to content

Commit

Permalink
feat(assets): 加载资源 images、svg、media、fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxue committed Dec 14, 2019
1 parent 164b04d commit 14920e8
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
asdasdasdas | 你爱哈的 | [ea12f9c](https://github.com/luoxue-victor/learn_webpack/commit/ea12f9c)
auto readme | 自动生成 readme 文件 | [1e0a526](https://github.com/luoxue-victor/learn_webpack/commit/1e0a526)
autoprefixer | css 自动添加前缀 | [4e88451](https://github.com/luoxue-victor/learn_webpack/commit/4e88451)
case-sensitive=paths | 严格区分大小写 | [164b04d](https://github.com/luoxue-victor/learn_webpack/commit/164b04d)
config | add config option | [83c9f1e](https://github.com/luoxue-victor/learn_webpack/commit/83c9f1e)
create readme | 创建readme | [571bfaa](https://github.com/luoxue-victor/learn_webpack/commit/571bfaa)
daasd | asdasd | [0b6ee85](https://github.com/luoxue-victor/learn_webpack/commit/0b6ee85)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [课时 14:升级 webpack5](./docs/课时-14.md)
- [课时 15:定义通用变量](./docs/课时-15.md)
- [课时 16:严格区分路径大小写](./docs/课时-16.md)
- [课时 17:加载资源 images、svg、media、fonts](./docs/课时-17.md)

</details>

Expand Down Expand Up @@ -98,6 +99,7 @@ webpack-box upgrade 5 # 可以切换到 webpack5/4
- [html-webpack-plugin 生成html](./config/HtmlWebpackPlugin.js)
- [mini-css-extract-plugin 配置](./config/MiniCssExtractPlugin.js)
- [别名配置](./config/alias.js)
- [加载资源 images、svg、media、fonts](./config/assets.js)
- [babel-loader 配置](./config/babelLoader.js)
- [基础配置](./config/base.js)
- [cache-loader 配置(webpack 5 弃用)](./config/cacheLoader.js)
Expand Down Expand Up @@ -143,6 +145,7 @@ module.exports = function (config) {
* @param {object} stylelint stylelint 配置
* @param {object} eslint eslint 配置
* @param {object} alias 配置别名
* @param {Boolean} filenameHashing 文件名是否使用 hash
*/
return {
entry: 'src/main.js',
Expand All @@ -156,6 +159,7 @@ module.exports = function (config) {
'@': resolve('src'),
'@src': resolve('src')
},
filenameHashing: true,
eslint: {
lintOnSave: true, // 开启运行时检测
extensions: ['js', 'jsx', 'vue'] // 默认 ['js', 'jsx']
Expand Down
2 changes: 2 additions & 0 deletions box.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function (config) {
* @param {object} stylelint stylelint 配置
* @param {object} eslint eslint 配置
* @param {object} alias 配置别名
* @param {Boolean} filenameHashing 文件名是否使用 hash
*/
return {
entry: 'src/main.js',
Expand All @@ -30,6 +31,7 @@ module.exports = function (config) {
'@': resolve('src'),
'@src': resolve('src')
},
filenameHashing: true,
eslint: {
lintOnSave: true, // 开启运行时检测
extensions: ['js', 'jsx', 'vue'] // 默认 ['js', 'jsx']
Expand Down
59 changes: 59 additions & 0 deletions config/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// [加载资源 images、svg、media、fonts]
module.exports = ({ config, webpackVersion, resolve, options }) => {
return () => {
// const resolveLocal = require('../util/resolveLocal')
const getAssetPath = require('../util/getAssetPath')
const inlineLimit = 4096

const genAssetSubPath = dir => {
return getAssetPath(
options,
`${dir}/[name]${options.filenameHashing ? '.[hash:8]' : ''}.[ext]`
)
}

const genUrlLoaderOptions = dir => {
return {
limit: inlineLimit,
fallback: {
loader: 'file-loader',
options: {
name: genAssetSubPath(dir)
}
}
}
}

config.module
.rule('images')
.test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options(genUrlLoaderOptions('img'))

// do not base64-inline SVGs.
// https://github.com/facebookincubator/create-react-app/pull/1180
config.module
.rule('svg')
.test(/\.(svg)(\?.*)?$/)
.use('file-loader')
.loader(require.resolve('file-loader'))
.options({
name: genAssetSubPath('img')
})

config.module
.rule('media')
.test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options(genUrlLoaderOptions('media'))

config.module
.rule('fonts')
.test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options(genUrlLoaderOptions('fonts'))
}
}
Binary file added dist/index/img/my-pub.2ca11318.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions dist/index/index.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/index/index.bundle.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/index/index.bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index/src_style_index_postcss.bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions docs/课时-17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## 课时 17:加载资源 images、svg、media、fonts

这章就直接上代码吧,是之前基础篇的补充

```js
module.exports = ({ config, webpackVersion, resolve, options }) => {
return () => {
const getAssetPath = require("../util/getAssetPath");
const inlineLimit = 4096;

const genAssetSubPath = dir => {
return getAssetPath(
options,
`${dir}/[name]${options.filenameHashing ? ".[hash:8]" : ""}.[ext]`
);
};

const genUrlLoaderOptions = dir => {
return {
limit: inlineLimit,
fallback: {
loader: "file-loader",
options: {
name: genAssetSubPath(dir)
}
}
};
};

config.module
.rule("images")
.test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
.use("url-loader")
.loader(require.resolve("url-loader"))
.options(genUrlLoaderOptions("img"));

config.module
.rule("svg")
.test(/\.(svg)(\?.*)?$/)
.use("file-loader")
.loader(require.resolve("file-loader"))
.options({
name: genAssetSubPath("img")
});

config.module
.rule("media")
.test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
.use("url-loader")
.loader(require.resolve("url-loader"))
.options(genUrlLoaderOptions("media"));

config.module
.rule("fonts")
.test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
.use("url-loader")
.loader(require.resolve("url-loader"))
.options(genUrlLoaderOptions("fonts"));
};
};
```
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"eslint-plugin-vue": "^6.0.1",
"express": "^4.17.1",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^5.0.2",
"fork-ts-checker-notifier-webpack-plugin": "^1.0.2",
"fork-ts-checker-webpack-plugin": "^3.1.0",
"friendly-errors-webpack-plugin": "^1.7.0",
Expand Down Expand Up @@ -89,6 +90,7 @@
"thread-loader": "^2.1.3",
"tslint": "^5.20.1",
"typescript": "^3.7.2",
"url-loader": "^3.0.0",
"vue-cli-plugin-commitlint": "^1.0.10",
"vue-template-compiler": "^2.6.10",
"webpack": "^5.0.0-beta.9",
Expand Down
Binary file added src/assets/my-pub.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ require('./style/index.less')
require('./style/index.scss')
import('./style/index.postcss')
require('react')
const myPubPic = require('./assets/my-pub.jpeg')

if (process.env.NODE_ENV === 'production') {
console.log('Welcome to production')
}
console.log(cube(2))
console.log(myPubPic)

const h2 = document.createElement('h2')
h2.className = 'test'
Expand Down
7 changes: 7 additions & 0 deletions util/getAssetPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require('path')

module.exports = function getAssetPath (options, filePath) {
return options.assetsDir
? path.posix.join(options.assetsDir, filePath)
: filePath
}
9 changes: 9 additions & 0 deletions util/getPadLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function getPadLength (obj) {
let longest = 10
for (const name in obj) {
if (name.length + 1 > longest) {
longest = name.length + 1
}
}
return longest
}
4 changes: 4 additions & 0 deletions util/isAbsoluteUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function isAbsoluteUrl (url) {
// A URL is considered absolute if it begins with "<scheme>://" or "//"
return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url)
}

0 comments on commit 14920e8

Please sign in to comment.