A loader for webpack which transforms files into base64 URIs
Installations
npm install url-loader
Releases
Contributors
Developer
webpack-contrib
Developer Guide
Module System
CommonJS
Min. Node Version
>= 10.13.0
Typescript Support
No
Node Version
10.15.2
NPM Version
6.14.8
Statistics
1,405 Stars
133 Commits
158 Forks
23 Watching
4 Branches
53 Contributors
Updated on 02 Nov 2024
Bundle Size
387.33 kB
Minified
87.33 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
2,216,489,728
Last day
-4.4%
1,021,134
Compared to previous day
Last week
2.8%
5,375,292
Compared to previous week
Last month
9.9%
22,553,425
Compared to previous month
Last year
-30.9%
285,765,773
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Peer Dependencies
2
Dev Dependencies
24
url-loader
DEPREACTED for v5: please consider migrating to asset modules
.
A loader for webpack which transforms files into base64 URIs.
Getting Started
To begin, you'll need to install url-loader
:
1$ npm install url-loader --save-dev
url-loader
works like
file-loader
, but can return
a DataURL if the file is smaller than a byte limit.
index.js
1import img from './image.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 limit: 8192, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
And run webpack
via your preferred method.
Options
Name | Type | Default | Description |
---|---|---|---|
limit | {Boolean|Number|String} | true | Specifying the maximum size of a file in bytes. |
mimetype | {Boolean|String} | based from mime-types | Sets the MIME type for the file to be transformed. |
encoding | {Boolean|String} | base64 | Specify the encoding which the file will be inlined with. |
generator | {Function} | () => type/subtype;encoding,base64_data | You can create you own custom implementation for encoding data. |
fallback | {String} | file-loader | Specifies an alternative loader to use when a target file's size exceeds the limit. |
esModule | {Boolean} | true | Use ES modules syntax. |
limit
Type: Boolean|Number|String
Default: true
The limit can be specified via loader options and defaults to no limit.
Boolean
Enable or disable transform files into base64.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 limit: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Number|String
A Number
or String
specifying the maximum size of a file in bytes.
If the file size is equal or greater than the limit file-loader
will be used (by default) and all query parameters are passed to it.
Using an alternative to file-loader
is enabled via the fallback
option.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 limit: 8192, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
mimetype
Type: Boolean|String
Default: based from mime-types
Specify the mimetype
which the file will be inlined with.
If unspecified the mimetype
value will be used from mime-types.
Boolean
The true
value allows to generation the mimetype
part from mime-types.
The false
value removes the mediatype
part from a Data URL (if omitted, defaults to text/plain;charset=US-ASCII
).
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 mimetype: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
String
Sets the MIME type for the file to be transformed.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 mimetype: 'image/png', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
encoding
Type: Boolean|String
Default: base64
Specify the encoding
which the file will be inlined with.
If unspecified the encoding
will be base64
.
Boolean
If 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
.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.svg$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 encoding: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
String
It supports Node.js Buffers and Character Encodings which are ["utf8","utf16le","latin1","base64","hex","ascii","binary","ucs2"]
.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.svg$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 encoding: 'utf8', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
generator
Type: Function
Default: (mimetype, encoding, content, resourcePath) => mimetype;encoding,base64_content
You can create you own custom implementation for encoding data.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|html)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 // The `mimetype` and `encoding` arguments will be obtained from your options 11 // The `resourcePath` argument is path to file. 12 generator: (content, mimetype, encoding, resourcePath) => { 13 if (/\.html$/i.test(resourcePath)) { 14 return `data:${mimetype},${content.toString()}`; 15 } 16 17 return `data:${mimetype}${ 18 encoding ? `;${encoding}` : '' 19 },${content.toString(encoding)}`; 20 }, 21 }, 22 }, 23 ], 24 }, 25 ], 26 }, 27};
fallback
Type: String
Default: 'file-loader'
Specifies an alternative loader to use when a target file's size exceeds the limit set in the limit
option.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 fallback: require.resolve('responsive-loader'), 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
The fallback loader will receive the same configuration options as url-loader.
For example, to set the quality option of a responsive-loader above use:
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 fallback: require.resolve('responsive-loader'), 11 quality: 85, 12 }, 13 }, 14 ], 15 }, 16 ], 17 }, 18};
esModule
Type: Boolean
Default: true
By default, file-loader
generates JS modules that use the ES modules syntax.
There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a CommonJS module syntax using:
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.css$/, 6 use: [ 7 { 8 loader: 'url-loader', 9 options: { 10 esModule: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Examples
SVG
SVG can be compressed into a more compact output, avoiding base64
.
You can read about it more here.
You can do it using mini-svg-data-uri package.
webpack.config.js
1const svgToMiniDataURI = require('mini-svg-data-uri'); 2 3module.exports = { 4 module: { 5 rules: [ 6 { 7 test: /\.svg$/i, 8 use: [ 9 { 10 loader: 'url-loader', 11 options: { 12 generator: (content) => svgToMiniDataURI(content.toString()), 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
Contributing
Please take a moment to read our contributing guidelines if you haven't yet done so.
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 6/30 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:50: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:68: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:71: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:93: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack-contrib/url-loader/nodejs.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:39
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:78
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:88
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 2 out of 5 npmCommand dependencies pinned
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 20 are checked with a SAST tool
Reason
49 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
3.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More