-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assets): 加载资源 images、svg、media、fonts
- Loading branch information
luoxue
committed
Dec 14, 2019
1 parent
164b04d
commit 14920e8
Showing
16 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |