购物车
登陆 / 注册
微信扫码登陆

推荐手册

file-loader


Instructs webpack to emit the required object as file and to return its public URL

安装

npm install --save-dev file-loader

用法

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

import img from './file.png'

webpack.config.js

module.exports = {  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}
          }
        ]
      }
    ]
  }
}

生成文件 file.png,输出到输出目录并返回 public URL。

"/public/path/0dcbbaa7013869e351f.png"

选项

名称类型默认值描述
name`{String\Function}`[hash].[ext]为你的文件配置自定义文件名模板
context{String}this.options.context配置自定义文件 context,默认为 webpack.config.js context
publicPath`{String\Function}`__webpack_public_path__为你的文件配置自定义 public 发布目录
outputPath`{String\Function}`'undefined'为你的文件配置自定义 output 输出目录
useRelativePath{Boolean}false如果你希望为每个文件生成一个相对 url 的 context 时,应该将其设置为 true
emitFile{Boolean}true默认情况下会生成文件,可以通过将此项设置为 false 来禁止(例如,使用了服务端的 packages)

#

You can configure a custom filename template for your file using the query parameter name. For instance, to copy a file from your context directory into the output directory retaining the full directory structure, you might use

{String}

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[path][name].[ext]'
  }
}

{Function}

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name (file) {
          if (env === 'development') {
                  return '[path][name].[ext]'
      }      return '[hash].[ext]'
    }
  }
}

placeholders

名称类型默认值描述
[ext]{String}file.extname资源扩展名
[name]{String}file.basename资源的基本名称
[path]{String}file.dirname资源相对于 context的路径
[hash]{String}md5内容的哈希值,下面的 hashes 配置中有更多信息
[N]{Number}``当前文件名按照查询参数 regExp 匹配后获得到第 N 个匹配结果

hashes

[<hashType>:hash:<digestType>:<length>] optionally you can configure

名称类型默认值描述
hashType{String}md5sha1, md5, sha256, sha512
digestType{String}base64hex, base26, base32, base36, base49, base52, base58, base62, base64
length{Number}9999字符的长度

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

context

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[path][name].[ext]',
    context: ''
  }
}

你可以使用 outputPathuseRelativePathpublicPath来指定自定义 output 输出路径和 public 发布目录。

publicPath

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[path][name].[ext]',
    publicPath: 'assets/'
  }
}

outputPath

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[path][name].[ext]',
    outputPath: 'images/'
  }
}

useRelativePath

如果你希望为每个文件生成一个相对 URL 的 context 时,应该将 useRelativePath 设置为 true

{
  loader: 'file-loader',
  options: {
    useRelativePath: process.env.NODE_ENV === "production"
  }
}

emitFile

默认情况下会生成文件,可以通过将此项设置为 false 来禁用(例如使用了服务端的 packages)。

import img from './file.png'
{
  loader: 'file-loader',
  options: {
    emitFile: false
  }
}

  返回 public URL 但不会生成文件

`${publicPath}/0dcbbaa701328e351f.png`

示例

import png from 'image.png'

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: 'dirname/[hash].[ext]'
  }
}
dirname/0dcbbaa701328ae351f.png

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[sha512:hash:base64:7].[ext]'
  }
}
gdyb21L.png
import png from 'path/to/file.png'

webpack.config.js

{
  loader: 'file-loader',
  options: {
    name: '[path][name].[ext]?[hash]'
  }
}
path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
网站导航
标签地图
学习路径
视频教程
开发软件
旗下子站
php中文网
phpstudy
技术文章
文档工具
关于我们
企业合作
人才招聘
联系我们
讲师招募
QQ交流群
QQ官方交流群
微信公众号
微信公众号