我只希望 Webpack 查看名为“.stories”的目录中的文件,但它们可以位于 src/
目录结构中的任意位置。
例如,我天真的尝试如下所示:
/*
src/
some.story.js <-- don't get this
|_components/
|_Button/
|_ .stories/ <--
|_ index.js <-- ignore this
|_ utils.js <-- ignore this
|_ main.story.js <-- get this
|_Forms/
|_ TextField
|_ .stories/ <-- same kind of thing
*/
// require.context API
// require.context(directory to search, shouldSearchSubDirs?, fileNameRegex)
require.context('src/**/.stories', true, /\.story\.js$/)
到目前为止,API 似乎希望我对文件名进行所有模式匹配。但是仅文件名不足以排除其他目录中的 .story.js
文件。我想允许 .stories/
目录之外的 .story.js
文件,并让 Webpack 忽略它们。
这可能吗?我错过了什么?
阅读 official documentation 时, 这意味着第一个参数指定了一个特定的目录:
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 是一个复杂的工具,很容易误解它的功能。
最佳答案
我知道已经很久了。但我会为其他人修正答案。
require.context('../src/components/', true, /\.stories\/[a-zA-Z0-9]+\.story.js$/);
关于javascript - 是否可以在 webpack 的 require.context 调用中使用通配符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174974/