`webpack` 函数有什么作用?

What does `webpack` function do?

我用了很长时间的Webpack,总是纠结于Webpack的配置。最近,我尝试使用 Webpack 使 VSCode 显示智能感知,我发现 webpack 是一个函数。

太好了,因为现在我可以启用智能感知而无需阅读复杂无聊的文档。但是当它 运行s 时,它并不像我所尊重的那样美妙。类型检查警告错误。我尝试在控制台中 运行 webpack({}) 并且它 return 类似于 Compiler 模式或默认配置。虽然我找到了另一种使用 /** @type {webpack.Configuration} */ 启用智能感知的方法,但仍然想知道; webpack() 函数的真正作用是什么?我们需要关心它吗?

衷心感谢!

webpack 函数允许以编程方式使用 Webpack。

Webpack Node.js API documentation所述,

The imported webpack function is fed a webpack Configuration Object and runs the webpack compiler if a callback function is provided

<...>

If you don’t pass the webpack runner function a callback, it will return a webpack Compiler instance. This instance can be used to manually trigger the webpack runner or have it build and watch for changes, much like the CLI.

在 webpack.config.js 中不需要它,因为它已经由 Webpack CLI 处理。