通过 Lando 上的环境变量设置 webpack dev server watch options (poll, aggregate_timeout)
Set webpacker dev server watch options (poll, aggregate_timeout) via environmental variables on Lando
项目正在使用 Ruby Webpacker and Lando 用于本地开发。
我的 config/webpacker.yml
包含这个:
development:
<<: *default
compile: true
dev_server:
public: some.lndo.site
host: '0.0.0.0'
port: 3035
...
watch_options:
poll: 1000
aggregate_timeout: 500
因此,在 webpacker.yml
中为我的本地环境插入 adding/editing 参数,我可以将其中一些选项设置为我的 .lando.yml
文件中的环境变量(即 public
, host
, port
):
services:
appserver:
type: ruby:2.6.3
overrides:
environment:
WEBPACKER_DEV_SERVER_PUBLIC: some.lndo.site
WEBPACKER_DEV_SERVER_HOST: '0.0.0.0'
WEBPACKER_DEV_SERVER_PORT: '3035'
...
这行得通。
问题:但是如何通过环境变量设置watch-options
(即poll
)的环境变量?
我试过这些,但没有成功:
WEBPACKER_DEV_SERVER_WATCHOPTIONS_POLL
WEBPACKER_DEV_SERVER_WATCH_OPTIONS_POLL
WEBPACKER_DEV_SERVER_POLL
目前不支持 dev_server
对象中的嵌套 JSON(从 Webpacker v4.2.0 开始),其中 key
插入 ENV["WEBPACK_DEV_SERVER_#{key.upcase}"]
只会匹配顶级键:https://github.com/rails/webpacker/blob/417542c8ed7d344ecc24b3ae40e303d478ad50d0/lib/webpacker/dev_server.rb#L59-L61
# env_prefix = "WEBPACKER_DEV_SERVER".freeze
def fetch(key)
ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
end
因为我猜你不需要 Rails 服务器来了解监视投票选项,所以没有必要将动态值传递给 config/webpacker.yml
。我会改为在 config/webpack/development.js
中阅读它们,即直接进入 Webpack:
// config/webpack/development.js
const environment = require('./environment.js');
environment.config.merge({
devServer: {
watchOptions: {
poll: process.env.WEBPACK_DEV_SERVER_WATCH_POLL,
aggregateTimeout: process.env.WEBPACK_DEV_SERVER_WATCH_TIMEOUT
}
}
});
module.exports = environment.toWebpackConfig();
项目正在使用 Ruby Webpacker and Lando 用于本地开发。
我的 config/webpacker.yml
包含这个:
development:
<<: *default
compile: true
dev_server:
public: some.lndo.site
host: '0.0.0.0'
port: 3035
...
watch_options:
poll: 1000
aggregate_timeout: 500
因此,在 webpacker.yml
中为我的本地环境插入 adding/editing 参数,我可以将其中一些选项设置为我的 .lando.yml
文件中的环境变量(即 public
, host
, port
):
services:
appserver:
type: ruby:2.6.3
overrides:
environment:
WEBPACKER_DEV_SERVER_PUBLIC: some.lndo.site
WEBPACKER_DEV_SERVER_HOST: '0.0.0.0'
WEBPACKER_DEV_SERVER_PORT: '3035'
...
这行得通。
问题:但是如何通过环境变量设置watch-options
(即poll
)的环境变量?
我试过这些,但没有成功:
WEBPACKER_DEV_SERVER_WATCHOPTIONS_POLL
WEBPACKER_DEV_SERVER_WATCH_OPTIONS_POLL
WEBPACKER_DEV_SERVER_POLL
目前不支持 dev_server
对象中的嵌套 JSON(从 Webpacker v4.2.0 开始),其中 key
插入 ENV["WEBPACK_DEV_SERVER_#{key.upcase}"]
只会匹配顶级键:https://github.com/rails/webpacker/blob/417542c8ed7d344ecc24b3ae40e303d478ad50d0/lib/webpacker/dev_server.rb#L59-L61
# env_prefix = "WEBPACKER_DEV_SERVER".freeze
def fetch(key)
ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
end
因为我猜你不需要 Rails 服务器来了解监视投票选项,所以没有必要将动态值传递给 config/webpacker.yml
。我会改为在 config/webpack/development.js
中阅读它们,即直接进入 Webpack:
// config/webpack/development.js
const environment = require('./environment.js');
environment.config.merge({
devServer: {
watchOptions: {
poll: process.env.WEBPACK_DEV_SERVER_WATCH_POLL,
aggregateTimeout: process.env.WEBPACK_DEV_SERVER_WATCH_TIMEOUT
}
}
});
module.exports = environment.toWebpackConfig();