In modular programming, developers decompose the program into discrete function blocks (discrete chunks of functionality) and are called modules .
Each module has a smaller contact surface than the complete program, making calibration, commissioning and testing easy. The well-written modules provide a reliable abstraction and encapsulation boundaries, allowing each module in the application to have a clear design and clear purpose.
What is the Webpack module
In contrast to the node. JS module, the Webpack module can express their dependencies in a variety of ways, with several examples:
- ES2015
import statement;
- CommonJS
require() statement;
- AMD
define and require statements;
- The statements in the Css/sass/less file
@import ;
url(...) image link (image URL) in the style () or HTML file ()
Supported module Types
Webpack can support a variety of languages and pre-processor authoring modules through loader . Loader Describes how Webpack handles non-JavaScript (non-javascript) modules and introduces these dependencies in bundles . The Webpack community has built loaderfor a variety of popular language and language processors, including:
- Coffeescript
- TypeScript
- Esnext (Babel)
- Sass
- Less
- Stylus
Overall, Webpack provides a customizable, powerful, and rich API that allows any technology stack to use Webpack, maintaining non-intrusive (non-opinionated) in your development, testing, and build processes.
Module parsing (modules Resolution)
Resolver is a library that helps find the absolute path to a module. A module can be used as a dependency module for another module and then referenced by the latter, as follows:
The modules you rely on can be libraries from application code or third parties (a third party library). Resolver Help Webpack find the module code that needs to be introduced in the bundle, which is contained in each require / import statement. webpackuse Enhanced-resolve to parse file paths when packaging a module
parsing rules in Webpack
Using enhanced-resolve , Webpack can parse three file paths:
Absolute Path
Since we have obtained the absolute path to the file, we do not need to parse it further.
relative Path
In this case, the import require directory where the resource files (resource file) are used or are considered to be contextual directories (context directory). In the import/require relative path given in, this context path is added to produce the absolute path of the module (absolute path).
Module Path
The module will resolve.modules search within all directories specified in. You can replace the initial module path, which resolve.alias creates an alias by using the configuration option.
once the path is parsed according to the above rules, the parser (resolver) checks to see if the path points to a file or directory. if the path points to a file :
- If the path has a file name extension, the file is packaged directly.
- Otherwise, the [] option will be used
resolve.extensions as the file extension to resolve, which tells the parser which extensions (for example,) can be accepted in the resolution .js .jsx .
if the path points to a folder , take the following steps to find the correct file with the correct extension:
- If the folder contains
package.json files, the fields specified in the configuration options are found in order resolve.mainFields . And package.json the first of these fields determines the file path.
- If the
package.json file does not exist or if package.json the main field in the file does not return a valid path, the file name specified in the configuration option is searched sequentially to resolve.mainFiles see if it can match to an existing file name in the Import/require directory.
- The file extension
resolve.extensions is parsed using the option in a similar way.
Webpack provides a reasonable default configuration for these options based on the build target.
Cache
each file system access is cached so that multiple concurrent or serial requests to the same file are triggered more quickly. In observation mode, only modified files are picked out from the cache. If you turn off observation mode, the cache is cleaned up before each compilation.
Webpack Learning-Module (Modules)