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.
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:
module.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif)$/i, loader: 'file-loader', options: { publicPath: (url, resourcePath, context) => { // `resourcePath` is original absolute path to asset // `context` is directory where stored asset (`rootContext`) or `context` option // To get relative path you can use // const relativePath = path.relative(context, resourcePath); if (/my-custom-image\.png/.test(resourcePath)) { return `other_public_path/${url}`; } if (/images/.test(context)) { return `image_output_path/${url}`; } return `public_path/${url}`; }, }, }, ], },};
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.
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.
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.
ℹ️ 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.
Dynamic public path depending on environment variable at run time
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
const assetPrefixForNamespace = (namespace) => { switch (namespace) { case 'prod': return 'https://cache.myserver.net/web'; case 'uat': return 'https://cache-uat.myserver.net/web'; case 'st': return 'https://cache-st.myserver.net/web'; case 'dev': return 'https://cache-dev.myserver.net/web'; default: return ''; }};const namespace = process.env.NAMESPACE;__webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;