{"id":13517196,"url":"https://github.com/webpack-contrib/url-loader","last_synced_at":"2025-12-15T04:48:29.834Z","repository":{"id":3277612,"uuid":"4317511","full_name":"webpack-contrib/url-loader","owner":"webpack-contrib","description":"A loader for webpack which transforms files into base64 URIs","archived":true,"fork":false,"pushed_at":"2021-03-15T14:54:21.000Z","size":1628,"stargazers_count":1404,"open_issues_count":4,"forks_count":146,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-09-26T00:43:31.534Z","etag":null,"topics":["webpack-loader"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ntucllab/libact","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"open_collective":"webpack"}},"created_at":"2012-05-13T20:54:53.000Z","updated_at":"2025-09-11T11:42:09.000Z","dependencies_parsed_at":"2022-08-06T13:16:42.267Z","dependency_job_id":null,"html_url":"https://github.com/webpack-contrib/url-loader","commit_stats":null,"previous_names":["webpack/url-loader"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/webpack-contrib/url-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Furl-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Furl-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Furl-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Furl-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-contrib","download_url":"https://codeload.github.com/webpack-contrib/url-loader/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Furl-loader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277450939,"owners_count":25819971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-28T02:00:08.834Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["webpack-loader"],"created_at":"2024-08-01T05:01:30.943Z","updated_at":"2025-09-29T00:32:44.404Z","avatar_url":"https://github.com/webpack-contrib.png","language":"JavaScript","readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/webpack/webpack\"\u003e\n    \u003cimg width=\"200\" height=\"200\" src=\"https://webpack.js.org/assets/icon-square-big.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n[![chat][chat]][chat-url]\n[![size][size]][size-url]\n\n# url-loader\n\n**DEPREACTED for v5**: please consider migrating to [`asset modules`](https://webpack.js.org/guides/asset-modules/).\n\nA loader for webpack which transforms files into base64 URIs.\n\n## Getting Started\n\nTo begin, you'll need to install `url-loader`:\n\n```console\n$ npm install url-loader --save-dev\n```\n\n`url-loader` works like\n[`file-loader`](https://github.com/webpack-contrib/file-loader), but can return\na DataURL if the file is smaller than a byte limit.\n\n**index.js**\n\n```js\nimport img from './image.png';\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              limit: 8192,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\nAnd run `webpack` via your preferred method.\n\n## Options\n\n|             Name              |            Type             |                            Default                            | Description                                                                         |\n| :---------------------------: | :-------------------------: | :-----------------------------------------------------------: | :---------------------------------------------------------------------------------- |\n|     **[`limit`](#limit)**     | `{Boolean\\|Number\\|String}` |                            `true`                             | Specifying the maximum size of a file in bytes.                                     |\n|  **[`mimetype`](#mimetype)**  |     `{Boolean\\|String}`     | based from [mime-types](https://github.com/jshttp/mime-types) | Sets the MIME type for the file to be transformed.                                  |\n|  **[`encoding`](#encoding)**  |     `{Boolean\\|String}`     |                           `base64`                            | Specify the encoding which the file will be inlined with.                           |\n| **[`generator`](#generator)** |        `{Function}`         |           `() =\u003e type/subtype;encoding,base64_data`           | You can create you own custom implementation for encoding data.                     |\n|  **[`fallback`](#fallback)**  |         `{String}`          |                         `file-loader`                         | Specifies an alternative loader to use when a target file's size exceeds the limit. |\n|  **[`esModule`](#esmodule)**  |         `{Boolean}`         |                            `true`                             | Use ES modules syntax.                                                              |\n\n### `limit`\n\nType: `Boolean|Number|String`\nDefault: `true`\n\nThe limit can be specified via loader options and defaults to no limit.\n\n#### `Boolean`\n\nEnable or disable transform files into base64.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              limit: false,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `Number|String`\n\nA `Number` or `String` specifying the maximum size of a file in bytes.\nIf the file size is **equal** or **greater** than the limit [`file-loader`](https://github.com/webpack-contrib/file-loader) will be used (by default) and all query parameters are passed to it.\n\nUsing an alternative to `file-loader` is enabled via the `fallback` option.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              limit: 8192,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `mimetype`\n\nType: `Boolean|String`\nDefault: based from [mime-types](https://github.com/jshttp/mime-types)\n\nSpecify the `mimetype` which the file will be inlined with.\nIf unspecified the `mimetype` value will be used from [mime-types](https://github.com/jshttp/mime-types).\n\n#### `Boolean`\n\nThe `true` value allows to generation the `mimetype` part from [mime-types](https://github.com/jshttp/mime-types).\nThe `false` value removes the `mediatype` part from a Data URL (if omitted, defaults to `text/plain;charset=US-ASCII`).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              mimetype: false,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `String`\n\nSets the MIME type for the file to be transformed.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              mimetype: 'image/png',\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `encoding`\n\nType: `Boolean|String`\nDefault: `base64`\n\nSpecify the `encoding` which the file will be inlined with.\nIf unspecified the `encoding` will be `base64`.\n\n#### `Boolean`\n\nIf you don't want to use any encoding you can set `encoding` to `false` however if you set it to `true` it will use the default encoding `base64`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.svg$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              encoding: false,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `String`\n\nIt supports [Node.js Buffers and Character Encodings](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) which are `[\"utf8\",\"utf16le\",\"latin1\",\"base64\",\"hex\",\"ascii\",\"binary\",\"ucs2\"]`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.svg$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              encoding: 'utf8',\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `generator`\n\nType: `Function`\nDefault: `(mimetype, encoding, content, resourcePath) =\u003e mimetype;encoding,base64_content`\n\nYou can create you own custom implementation for encoding data.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|html)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              // The `mimetype` and `encoding` arguments will be obtained from your options\n              // The `resourcePath` argument is path to file.\n              generator: (content, mimetype, encoding, resourcePath) =\u003e {\n                if (/\\.html$/i.test(resourcePath)) {\n                  return `data:${mimetype},${content.toString()}`;\n                }\n\n                return `data:${mimetype}${\n                  encoding ? `;${encoding}` : ''\n                },${content.toString(encoding)}`;\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `fallback`\n\nType: `String`\nDefault: `'file-loader'`\n\nSpecifies an alternative loader to use when a target file's size exceeds the limit set in the `limit` option.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              fallback: require.resolve('responsive-loader'),\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\nThe fallback loader will receive the same configuration options as url-loader.\n\nFor example, to set the quality option of a responsive-loader above use:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(png|jpg|gif)$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              fallback: require.resolve('responsive-loader'),\n              quality: 85,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `esModule`\n\nType: `Boolean`\nDefault: `true`\n\nBy default, `file-loader` generates JS modules that use the ES modules syntax.\nThere are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).\n\nYou can enable a CommonJS module syntax using:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              esModule: false,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n## Examples\n\n### SVG\n\nSVG can be compressed into a more compact output, avoiding `base64`.\nYou can read about it more [here](https://css-tricks.com/probably-dont-base64-svg/).\nYou can do it using [mini-svg-data-uri](https://github.com/tigt/mini-svg-data-uri) package.\n\n**webpack.config.js**\n\n```js\nconst svgToMiniDataURI = require('mini-svg-data-uri');\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.svg$/i,\n        use: [\n          {\n            loader: 'url-loader',\n            options: {\n              generator: (content) =\u003e svgToMiniDataURI(content.toString()),\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n## Contributing\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/url-loader.svg\n[npm-url]: https://npmjs.com/package/url-loader\n[node]: https://img.shields.io/node/v/url-loader.svg\n[node-url]: https://nodejs.org\n[deps]: https://david-dm.org/webpack-contrib/url-loader.svg\n[deps-url]: https://david-dm.org/webpack-contrib/url-loader\n[tests]: https://github.com/webpack-contrib/url-loader/workflows/url-loader/badge.svg\n[tests-url]: https://github.com/webpack-contrib/url-loader/actions\n[cover]: https://codecov.io/gh/webpack-contrib/url-loader/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack-contrib/url-loader\n[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg\n[chat-url]: https://gitter.im/webpack/webpack\n[size]: https://packagephobia.now.sh/badge?p=url-loader\n[size-url]: https://packagephobia.now.sh/result?p=url-loader\n\n```\n\n```\n","funding_links":["https://opencollective.com/webpack"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Furl-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-contrib%2Furl-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Furl-loader/lists"}