From 3e2621b0b9f2720c468597fe22b9d58adfe07a32 Mon Sep 17 00:00:00 2001 From: Ryunosuke Sato Date: Wed, 14 Mar 2018 15:02:48 +0900 Subject: [PATCH] Allow custom env for Webpacker The Rails defined custom env is allowed by https://github.com/rails/webpacker/pull/1304. However Webpacker custom env is not allowed for now. ``` /Users/tricknotes/dummy-app/node_modules/@rails/webpacker/package/config.js:16 delete defaults.extensions ^ TypeError: Cannot convert undefined or null to object ``` This commit allows using user defined env for Webpacker. --- package/config.js | 2 +- test/env_test.rb | 14 ++++++++++++++ test/test_app/config/webpacker.yml | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/package/config.js b/package/config.js index 24390690c..879c15e61 100644 --- a/package/config.js +++ b/package/config.js @@ -9,7 +9,7 @@ const defaultConfigPath = require.resolve('../lib/install/config/webpacker.yml') const configPath = resolve('config', 'webpacker.yml') const getConfig = () => { - const defaults = safeLoad(readFileSync(defaultConfigPath), 'utf8')[env] + const defaults = safeLoad(readFileSync(defaultConfigPath), 'utf8')[env] || {} const app = safeLoad(readFileSync(configPath), 'utf8')[env] if (isArray(app.extensions) && app.extensions.length) { diff --git a/test/env_test.rb b/test/env_test.rb index 1fc743011..63633dc77 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -13,6 +13,20 @@ def test_custom end end + def test_custom_with_user_defined + with_node_env("cucumber") do + reloaded_config + assert_equal Webpacker.env, "cucumber" + end + end + + def test_fallback + with_node_env("not-defined") do + reloaded_config + assert_equal Webpacker.env, "production" + end + end + def test_default assert_equal Webpacker::Env::DEFAULT, "production" end diff --git a/test/test_app/config/webpacker.yml b/test/test_app/config/webpacker.yml index beaf1831c..2fe50b351 100644 --- a/test/test_app/config/webpacker.yml +++ b/test/test_app/config/webpacker.yml @@ -61,3 +61,7 @@ production: # Cache manifest.json for performance cache_manifest: true + +cucumber: + <<: *default + compile: true