如何解决Webpack插件:如何在构建时读取插件使用者的文件?
是否可以读取安装了Webpack插件的回购的内容?
本质上,我编写了一个特定的JS library,它要求调用者在其应用程序代码中使用require.context,以便在构建时读取一些文件并将它们传递给库:
import Library from 'library';
new Library({
requires: require.context('./foo',false,/\w+\.js$/),});
我想减轻用户的负担,只需在库中进行require.context
调用即可将API简化为此:
import Library from 'library';
new Library();
但是,它不起作用,因为该库不知道何时运行应用程序代码的Webpack构建。
我尝试在库中编写一个Webpack插件:
const path = require('path');
class TestPlugin {
apply(compiler) {
compiler.hooks.beforeRun.tap('TestPlugin',() => {
console.log(
`Path where webpack was executed: ${compiler.options.context}`
);
});
}
}
module.exports = {
...
plugins: [new TestPlugin()],};
但是,只有在构建库本身时才调用此函数,而在构建应用程序时才调用
。如何将require.context
调用移到库中(这是NPM依赖项),仍然让它读取调用者的文件?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。