网页 综合 更新时间 2026-11-14 09:18:03

Dependency Management | webpack

自动蜘蛛 webpack.js.org 正文约 2086 字
Dependency Management | webpack
A context is created if your request contains expressions, so the exact module is not known on compile time.
Example, given we have the following folder structure including .ejs files:
When following import() or require() call is evaluated:
Webpack parses the import() or require() call and extracts some information:
A context module is generated. It contains references to all modules in that directory that can be required with a request matching the regular expression. The context module contains a map which translates requests to module ids.
The context module also contains some runtime logic to access the map.
This means dynamic calls are supported but will cause all matching modules to be included in the bundle.
The ESM equivalent of require.context is import.meta.webpackContext.
The arguments passed to import.meta.webpackContext must be literals!
You can create your own context with the require.context() function.
It allows you to pass in a directory to search, a flag indicating whether subdirectories should be searched too, and a regular expression to match files against.
Webpack parses for require.context() in the code while building.
The syntax is as follows:
The arguments passed to require.context must be literals!
A context module exports a (require) function that takes one argument: the request.
The exported function has 3 properties: resolve, keys, id.
This can be useful if you want to require all files in a directory or matching a pattern, Example:
Copyright OpenJS Foundation and webpack contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
The OpenJS Foundation | Terms of Use | Privacy Policy | Bylaws | Code of Conduct | Trademark Policy | Trademark List | Cookie Policy
访问原文 返回搜索 https://webpack.js.org/guides/dependency-management/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。