Layer is a powerful feature provided by Rspack that allows you to categorize and manage modules. By assigning different layers to modules, you can perform differentiated processing for different types of modules, such as:
When a module is assigned a layer, all its dependent modules will also be assigned to that layer. For example, if you specify the layer as client for the root module, all modules starting from the root module and their dependencies will be assigned to the client layer.
You can specify layers for modules in the following ways:
Directly specify layers for different entry points in the entry configuration using the layer option:
You can use the rule.layer option to specify a layer for all modules that match the rule:
The layer assigned by rule.layer will override the module's own layer. For example, if a module has been assigned a layer through Entry configuration, the layer assigned through Rule will override the layer specified by Entry.
To better understand how Layer works, we categorize modules into two types:
1. Downward Inheritance
All dependent modules of a Layer module will inherit the same layer. For example, when you specify the layer as 'client' for an entry module, all modules in the entire dependency tree starting from that entry will be assigned to the 'client' layer.
2. Mid-path Override
If a module in the dependency chain is reassigned to a different layer (such as 'server') through Loader rules, then all subsequent dependencies starting from that module will follow the new layer.
When a regular module is depended upon by multiple modules with different layers, that module will generate multiple copies in the build output, with each copy corresponding to a layer.
Example: If both 'client' layer and 'server' layer depend on the same lib.js module, then:
lib.js: one belonging to the 'client' layer and another to the 'server' layerYou can use issuerLayer to filter modules from specific layers and apply different processing rules to them. The following example shows how to use different compilation targets for different layers:
During the build optimization phase, you can assign modules to different chunks based on different layers:
Through the above configuration, we successfully output server and client code to different directories respectively, achieving layer-based code separation.