file-loader

[![npm][npm]][npm-url] [![node][node]][node-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![chat][chat]][chat-url] [![size][size]][size-url]

The file-loader resolves import/require() on a file into a url and emits the file into the output directory.

起步

你需要预先安装 file-loader

  1. $ npm install file-loader --save-dev

在一个 bundle 文件中 import(或 require)目标文件:

file.js

  1. import img from './file.png';

然后,在 webpack 配置中添加 loader。例如:

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {},
  10. },
  11. ],
  12. },
  13. ],
  14. },
  15. };

然后,通过你偏爱的方式去运行 webpack。将 file.png 作为一个文件,生成到输出目录, (如果指定了选项,则使用指定的命名约定) 并返回文件的 public URI。

ℹ️ 默认情况下,生成文件的文件名,是文件内容的 MD5 哈希值,并会保留所引用资源的原始扩展名。

选项

name

类型:String|Function 默认:'[hash].[ext]'

Specifies a custom filename template for the target file(s) using the query parameter name. For example, to emit a file from your context directory into the output directory retaining the full directory structure, you might use:

String

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. name: '[path][name].[ext]',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

Function

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. name(file) {
  11. if (process.env.NODE_ENV === 'development') {
  12. return '[path][name].[ext]';
  13. }
  14. return '[hash].[ext]';
  15. },
  16. },
  17. },
  18. ],
  19. },
  20. ],
  21. },
  22. };

ℹ️ 默认情况下,文件会按照你指定的路径和名称输出同一目录中,且会使用相同的 URI 路径来访问文件。

outputPath

类型:String|Function 默认:undefined

Specify a filesystem path where the target file(s) will be placed.

String

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. outputPath: 'images',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

Function

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. outputPath: (url, resourcePath, context) => {
  11. // `resourcePath` is original absolute path to asset
  12. // `context` is directory where stored asset (`rootContext`) or `context` option
  13. // To get relative path you can use
  14. // const relativePath = path.relative(context, resourcePath);
  15. if (/my-custom-image\.png/.test(resourcePath)) {
  16. return `other_output_path/${url}`;
  17. }
  18. if (/images/.test(context)) {
  19. return `image_output_path/${url}`;
  20. }
  21. return `output_path/${url}`;
  22. },
  23. },
  24. },
  25. ],
  26. },
  27. ],
  28. },
  29. };

publicPath

类型:String|Function 默认:__webpack_public_path__

Specifies a custom public path for the target file(s).

String

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. publicPath: 'assets',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

Function

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. publicPath: (url, resourcePath, context) => {
  11. // `resourcePath` is original absolute path to asset
  12. // `context` is directory where stored asset (`rootContext`) or `context` option
  13. // To get relative path you can use
  14. // const relativePath = path.relative(context, resourcePath);
  15. if (/my-custom-image\.png/.test(resourcePath)) {
  16. return `other_public_path/${url}`;
  17. }
  18. if (/images/.test(context)) {
  19. return `image_output_path/${url}`;
  20. }
  21. return `public_path/${url}`;
  22. },
  23. },
  24. },
  25. ],
  26. },
  27. ],
  28. },
  29. };

context

类型:String 默认:context

Specifies a custom file context.

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. context: 'project',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

emitFile

类型:Boolean 默认:true

如果是 true,生成一个文件(向文件系统写入一个文件)。 如果是 false,loader 会返回 public URI,但不会生成文件。 对于服务器端 package,禁用此选项通常很有用。

file.js

  1. // bundle file
  2. import img from './file.png';

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.css$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. emitFile: false,
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

regExp

类型:RegExp 默认:undefined

Specifies a Regular Expression to one or many parts of the target file path. The capture groups can be reused in the name property using [N] placeholder.

file.js

  1. import img from './customer01/file.png';

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/,
  11. name: '[1]-[name].[ext]',
  12. },
  13. },
  14. ],
  15. },
  16. ],
  17. },
  18. };

ℹ️ If [0] is used, it will be replaced by the entire tested string, whereas [1] will contain the first capturing parenthesis of your regex and so on…

placeholders

Full information about placeholders you can find here.

[ext]

类型:String 默认:file.extname

目标文件/资源的文件扩展名。

[name]

类型:String 默认:file.basename

文件/资源的基本名称。

[path]

类型:String 默认:file.directory

The path of the resource relative to the webpack/config context.

[folder]

类型:String 默认:file.folder

The folder of the resource is in.

[emoji]

类型:String 默认:undefined

A random emoji representation of content.

[emoji:<length>]

类型:String 默认:undefined

Same as above, but with a customizable number of emojis

[hash]

类型:String 默认:md5

指定生成文件内容哈希值的哈希方法。

[<hashType>:hash:<digestType>:<length>]

类型:String

The hash of options.content (Buffer) (by default it’s the hex digest of the hash).

digestType

类型:String 默认:'hex'

The digest that the hash function should use. Valid values include: base26, base32, base36, base49, base52, base58, base62, base64, and hex.

hashType

类型:String 默认:'md5'

The type of hash that the has function should use. Valid values include: md5, sha1, sha256, and sha512.

length

类型:Number 默认:undefined

Users may also specify a length for the computed hash.

[N]

类型:String 默认:undefined

The n-th match obtained from matching the current file name against the regExp.

示例

The following examples show how one might use file-loader and what the result would be.

file.js

  1. import png from './image.png';

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. name: 'dirname/[hash].[ext]',
  11. },
  12. }
  13. ],
  14. },
  15. ],
  16. },
  17. };

结果:

  1. # result
  2. dirname/0dcbbaa701328ae351f.png

file.js

  1. import png from './image.png';

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. name: '[sha512:hash:base64:7].[ext]',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

结果:

  1. # result
  2. gdyb21L.png

file.js

  1. import png from './path/to/file.png';

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.(png|jpg|gif)$/,
  6. use: [
  7. {
  8. loader: 'file-loader',
  9. options: {
  10. name: '[path][name].[ext]?[hash]',
  11. },
  12. },
  13. ],
  14. },
  15. ],
  16. },
  17. };

结果:

  1. # result
  2. path/to/file.png?e43b20c069c4a01867c31e98cbce33c9

贡献

如果你从未阅读过我们的贡献指南,请在上面花点时间。

贡献指南

License

MIT