Gathering detailed insights and metrics for file-loader
Gathering detailed insights and metrics for file-loader
Gathering detailed insights and metrics for file-loader
Gathering detailed insights and metrics for file-loader
npm install file-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,864 Stars
204 Commits
257 Forks
33 Watching
1 Branches
70 Contributors
Updated on 26 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.9%
1,992,021
Compared to previous day
Last week
1.6%
10,506,937
Compared to previous week
Last month
11.5%
44,324,313
Compared to previous month
Last year
-20.6%
521,170,538
Compared to previous year
2
1
23
DEPRECATED for v5: please consider migrating to asset modules
.
The file-loader
resolves import
/require()
on a file into a url and emits the file into the output directory.
To begin, you'll need to install file-loader
:
1$ npm install file-loader --save-dev
Import (or require
) the target file(s) in one of the bundle's files:
file.js
1import img from './file.png';
Then add the loader to your webpack
config. For example:
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 }, 10 ], 11 }, 12 ], 13 }, 14};
And run webpack
via your preferred method. This will emit file.png
as a file
in the output directory (with the specified naming convention, if options are
specified to do so) and returns the public URI of the file.
ℹ️ By default the filename of the resulting file is the hash of the file's contents with the original extension of the required resource.
name
Type: String|Function
Default: '[contenthash].[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
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 name: '[path][name].[ext]', 9 }, 10 }, 11 ], 12 }, 13};
Function
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 name(resourcePath, resourceQuery) { 9 // `resourcePath` - `/absolute/path/to/file.js` 10 // `resourceQuery` - `?foo=bar` 11 12 if (process.env.NODE_ENV === 'development') { 13 return '[path][name].[ext]'; 14 } 15 16 return '[contenthash].[ext]'; 17 }, 18 }, 19 }, 20 ], 21 }, 22};
ℹ️ By default the path and name you specify will output the file in that same directory, and will also use the same URI path to access the file.
outputPath
Type: String|Function
Default: undefined
Specify a filesystem path where the target file(s) will be placed.
String
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 outputPath: 'images', 9 }, 10 }, 11 ], 12 }, 13};
Function
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 outputPath: (url, resourcePath, context) => { 9 // `resourcePath` is original absolute path to asset 10 // `context` is directory where stored asset (`rootContext`) or `context` option 11 12 // To get relative path you can use 13 // const relativePath = path.relative(context, resourcePath); 14 15 if (/my-custom-image\.png/.test(resourcePath)) { 16 return `other_output_path/${url}`; 17 } 18 19 if (/images/.test(context)) { 20 return `image_output_path/${url}`; 21 } 22 23 return `output_path/${url}`; 24 }, 25 }, 26 }, 27 ], 28 }, 29};
publicPath
Type: String|Function
Default: __webpack_public_path__
+outputPath
Specifies a custom public path for the target file(s).
String
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 publicPath: 'assets', 9 }, 10 }, 11 ], 12 }, 13};
Function
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 publicPath: (url, resourcePath, context) => { 9 // `resourcePath` is original absolute path to asset 10 // `context` is directory where stored asset (`rootContext`) or `context` option 11 12 // To get relative path you can use 13 // const relativePath = path.relative(context, resourcePath); 14 15 if (/my-custom-image\.png/.test(resourcePath)) { 16 return `other_public_path/${url}`; 17 } 18 19 if (/images/.test(context)) { 20 return `image_output_path/${url}`; 21 } 22 23 return `public_path/${url}`; 24 }, 25 }, 26 }, 27 ], 28 }, 29};
postTransformPublicPath
Type: Function
Default: undefined
Specifies a custom function to post-process the generated public path. This can be used to prepend or append dynamic global variables that are only available at runtime, like __webpack_public_path__
. This would not be possible with just publicPath
, since it stringifies the values.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 publicPath: '/some/path/', 9 postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, 10 }, 11 }, 12 ], 13 }, 14};
context
Type: String
Default: context
Specifies a custom file context.
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 context: 'project', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
emitFile
Type: Boolean
Default: true
If true, emits a file (writes a file to the filesystem). If false, the loader will return a public URI but will not emit the file. It is often useful to disable this option for server-side packages.
file.js
1// bundle file 2import img from './file.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.css$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 emitFile: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
regExp
Type: RegExp
Default: 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
1import img from './customer01/file.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/i, 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...
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: 'file-loader', 9 options: { 10 esModule: false, 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Full information about placeholders you can find here.
[ext]
Type: String
Default: file.extname
The file extension of the target file/resource.
[name]
Type: String
Default: file.basename
The basename of the file/resource.
[path]
Type: String
Default: file.directory
The path of the resource relative to the webpack/config context
.
[folder]
Type: String
Default: file.folder
The folder of the resource is in.
[query]
Type: String
Default: file.query
The query of the resource, i.e. ?foo=bar
.
[emoji]
Type: String
Default: undefined
A random emoji representation of content
.
[emoji:<length>]
Type: String
Default: undefined
Same as above, but with a customizable number of emojis
[hash]
Type: String
Default: md4
Specifies the hash method to use for hashing the file content.
[contenthash]
Type: String
Default: md4
Specifies the hash method to use for hashing the file content.
[<hashType>:hash:<digestType>:<length>]
Type: String
The hash of options.content (Buffer) (by default it's the hex digest of the hash).
digestType
Type: String
Default: 'hex'
The digest that the hash function should use. Valid values include: base26, base32, base36, base49, base52, base58, base62, base64, and hex.
hashType
Type: String
Default: 'md4'
The type of hash that the hash function should use. Valid values include: md4
, md5
, sha1
, sha256
, and sha512
.
length
Type: Number
Default: undefined
Users may also specify a length for the computed hash.
[N]
Type: String
Default: 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
1import png from './image.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 name: 'dirname/[contenthash].[ext]', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Result:
1# result 2dirname/0dcbbaa701328ae351f.png
file.js
1import png from './image.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 name: '[sha512:hash:base64:7].[ext]', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Result:
1# result 2gdyb21L.png
file.js
1import png from './path/to/file.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpe?g|gif)$/i, 6 use: [ 7 { 8 loader: 'file-loader', 9 options: { 10 name: '[path][name].[ext]?[contenthash]', 11 }, 12 }, 13 ], 14 }, 15 ], 16 }, 17};
Result:
1# result 2path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
The following examples show how to use file-loader
for CDN uses query params.
file.js
1import png from './directory/image.png?width=300&height=300';
webpack.config.js
1module.exports = { 2 output: { 3 publicPath: 'https://cdn.example.com/', 4 }, 5 module: { 6 rules: [ 7 { 8 test: /\.(png|jpe?g|gif)$/i, 9 use: [ 10 { 11 loader: 'file-loader', 12 options: { 13 name: '[path][name].[ext][query]', 14 }, 15 }, 16 ], 17 }, 18 ], 19 }, 20};
Result:
1# result 2https://cdn.example.com/directory/image.png?width=300&height=300
An application might want to configure different CDN hosts depending on an environment variable that is only available when running the application. This can be an advantage, as only one build of the application is necessary, which behaves differently depending on environment variables of the deployment environment. Since file-loader is applied when compiling the application, and not when running it, the environment variable cannot be used in the file-loader configuration. A way around this is setting the __webpack_public_path__
to the desired CDN host depending on the environment variable at the entrypoint of the application. The option postTransformPublicPath
can be used to configure a custom path depending on a variable like __webpack_public_path__
.
main.js
1const assetPrefixForNamespace = (namespace) => { 2 switch (namespace) { 3 case 'prod': 4 return 'https://cache.myserver.net/web'; 5 case 'uat': 6 return 'https://cache-uat.myserver.net/web'; 7 case 'st': 8 return 'https://cache-st.myserver.net/web'; 9 case 'dev': 10 return 'https://cache-dev.myserver.net/web'; 11 default: 12 return ''; 13 } 14}; 15const namespace = process.env.NAMESPACE; 16 17__webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;
file.js
1import png from './image.png';
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.(png|jpg|gif)$/i, 6 loader: 'file-loader', 7 options: { 8 name: '[name].[contenthash].[ext]', 9 outputPath: 'static/assets/', 10 publicPath: 'static/assets/', 11 postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, 12 }, 13 }, 14 ], 15 }, 16};
Result when run with NAMESPACE=prod
env variable:
1# result 2https://cache.myserver.net/web/static/assets/image.somehash.png
Result when run with NAMESPACE=dev
env variable:
1# result 2https://cache-dev.myserver.net/web/static/assets/image.somehash.png
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
project is archived
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
49 existing vulnerabilities detected
Details
Score
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@types/file-loader
TypeScript definitions for file-loader
@smithy/shared-ini-file-loader
[![NPM version](https://img.shields.io/npm/v/@smithy/shared-ini-file-loader/latest.svg)](https://www.npmjs.com/package/@smithy/shared-ini-file-loader) [![NPM downloads](https://img.shields.io/npm/dm/@smithy/shared-ini-file-loader.svg)](https://www.npmjs.c
@rushstack/heft-config-file
Configuration file loader for @rushstack/heft
jest-file-loader
A jest transformer similar to webpack's file-loader