在 Webpack Encore 中配置 "writeToDisk"

Configuring "writeToDisk" in Webpack Encore

直觉上,我认为 writeToDisk 选项可以在 configureDevServerOptions 下配置,但尝试这样做会产生此错误:

Running webpack-dev-server ...

 WARNING  Webpack is already provided by Webpack Encore, also adding it to your package.json file may cause issues.
 WARNING  Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues. Your assets will load from the publicPath (http://localhost:9091/build/) instead of from the dev server URL.
[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'writeToDisk'. These properties are valid:
   object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, setupExitSignals?, static?, watchFiles?, webSocketServer? }
error Command failed with exit code 2.

也曾尝试通过 getWebpackConfig() 对象设置该值,但生成了相同的错误。

由于 this writeToDisk option,下一个攻击方法可能是通过 devMiddleware。这会产生此错误:

Running webpack-dev-server ...

 WARNING  Webpack is already provided by Webpack Encore, also adding it to your package.json file may cause issues.
 WARNING  Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues. Your assets will load from the publicPath (http://localhost:9091/build/) instead of from the dev server URL.
  TypeError: Cannot set property 'writeToDisk' of undefined

  - webpack.config.js:99 Object.<anonymous>
    C:/path/to/project/webpack.config.js:99:41

  - apply-options-callback.js:13 module.exports
    [project]/[@symfony]/webpack-encore/lib/utils/apply-options-callback.js:13:36

  - config-generator.js:616 ConfigGenerator.buildDevServerConfig
    [project]/[@symfony]/webpack-encore/lib/config-generator.js:616:16

  - config-generator.js:60 ConfigGenerator.getWebpackConfig
    [project]/[@symfony]/webpack-encore/lib/config-generator.js:60:74

  - config-generator.js:631 module.exports
    [project]/[@symfony]/webpack-encore/lib/config-generator.js:631:22

  - index.js:1634 Encore.getWebpackConfig
    [project]/[@symfony]/webpack-encore/index.js:1634:16

  - EncoreProxy.js:51 Proxy.minDistance
    [project]/[@symfony]/webpack-encore/lib/EncoreProxy.js:51:53

  - webpack.config.js:126 Object.<anonymous>
    C:/path/to/project/webpack.config.js:126:25

  - v8-compile-cache.js:192 Module._compile
    [project]/[v8-compile-cache]/v8-compile-cache.js:192:30

  - loader:1153 Object.Module._extensions..js
    node:internal/modules/cjs/loader:1153:10


error Command failed with exit code 1.

由于 publicPath 不在此开发服务器“模式”中,我不得不将 public 通过 setPublicPath 的路径设置为警告中的值,导致该警告生成。

第一个警告来自将我的 package.json Webpack 定义设置为 webpack@^5.35,因此不会生成其他依赖项警告。

如何正确配置未在开发服务器架构中定义的选项?

这可以使用 copyFiles 函数来完成,只需确保在 dev-server 环境中复制整个构建目录即可。