Load resource files before Webpack5 using file-loader, url-loader, raw-loader, etc. After Webpackage 5, you can directly use asset Module type to replace the above loader, which will first use the loading mode before WebPackage 5 and the new loading mode added after WebPackage 5
-
The preparatory work
Prepare two different image resources, mine is one
56KB
A picture and11KB
Add the two images to the project, and then display the two images on the page and then package them to see the output
The code is as follows:Packing results are as follows:Packaging will report an error message requiring a suitableloader
To load such files -
file-loader
file-loader
Is to help us process itimport/require()
The file resource is imported and will be placed in the folder we output. As mentioned above, an error will be reported after importing the image resource, so we are importing it nowfile-loader
After the configuration is complete then see the results in the package-
The installation
yarn add file-loader -D
-
configuration
Only need to
webpack
Add arule
justPacking results:Found in thebuild
There are two more files in the folder, these two files are the two images used, and then run to find that the images can load normally -
File name rule and configuration file name
As you can see from the packing result above, the resource file will be added to the specified packing folder, and the name is changed to a long string
MD4
Hash function processing generated byhash
Value), some companies will require the format of the file name after packaging, such as the need to retain the original file name, add the company prefix, retain the extension, this time can be usedPlaceHolders
To finish, aboutPlaceHolders
The official documentation explains it in detail:Here are some common onesPlaceHolders
ext
: Handles file extensionsname
: Process file namehash
Use:MD4
Hash function processing generated byhash
Value (by default, it is the hexadecimal digest of the hash)contentHash
: Specifies the hash method that generates the hash value of the file content. (the default md4)path
: relative to webpack/configcontext
The resource path of.hash:<length>
: Indicates the length of the screenshot hash. The default value is 32 characters
Configuration mode:
Packing results:
-
Example Set the file path
- The path is spelled before the name
- Set the outputPath
-
-
url-loader
url-loader
Usage and function basically andfile-loader
It’s the same thing, buturl-loader
You can convert the file tobase64
theURI
.- The installation
yarn add url-loader -D
- configurationThe discovery is just that
file-loader
replaceurl-loader
Can pack properly and can display properly, butbuild
There is no corresponding resource file in the folderurl-loader
By default, the image resource is converted tobase64
The encoding format is as follows: url-loader
thelimit
The above mentionedurl-loader
Image resources are converted by default tobase64
Encoding format, this has disadvantages, if there are too many images or image resources when the whole page load speed will be very slow, so this can be usedlimit
To do the limit, is the picture resource is greater than how much time do not proceedbase64
The specific configuration of encoding format conversion is as follows:Running results:
Found that addlimit
Large images are no longer converted
- The installation
-
asset module type
The four types of resource modules (quoted here from the official documentation)
asset/resource
Send a separate file and export the URL. Previously by usingfile-loader
The implementation.asset/inline
Export the data URI of a resource. Previously by usingurl-loader
The implementation.asset/source
Export the source code of the resource. Previously by usingraw-loader
The implementation.asset
Automatically choose between exporting a data URI and sending a separate file. Previously by usingurl-loader
And configure the resource volume limitation implementation.
configurationSame addition
rule
- Loading picturesConfig file name and specify path and above
loader
The following figure specifies the file name and corresponding pathAnother way to change the file path is through Settingsoutput.assetModuleFilename
To modify the figureSo you have to be careful if you set itfilename
Preferentially usefilename
The configuration of the - The limit effect of url-loader must be set to the resource type
asset
And then in the Settingsparser.dataUrlCondition.maxSize
Parameters are shown as follows:Running results: