From e61618465f872694775d1a39c10cf55d9df939ea Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Tue, 20 Mar 2018 16:10:49 +0000 Subject: [PATCH] Detangle NODE_ENV and RAILS_ENV (#1359) --- lib/install/bin/webpack | 2 +- lib/install/bin/webpack-dev-server | 2 +- lib/install/config/webpack/development.js | 2 ++ lib/install/config/webpack/production.js | 2 ++ lib/install/config/webpack/test.js | 2 ++ lib/tasks/webpacker/compile.rake | 2 ++ lib/webpacker/compiler.rb | 3 +- lib/webpacker/dev_server_runner.rb | 4 +-- lib/webpacker/env.rb | 4 +-- package/__tests__/dev_server.js | 6 ++-- package/__tests__/development.js | 30 ++++++++++++++++++++ package/__tests__/env.js | 34 +++++++++++++++++------ package/__tests__/index.js | 31 --------------------- package/__tests__/production.js | 27 ++++++++++++++++++ package/__tests__/staging.js | 27 ++++++++++++++++++ package/__tests__/test.js | 23 +++++++++++++++ package/config.js | 26 ++++++++--------- package/dev_server.js | 21 ++++++-------- package/env.js | 24 +++++++--------- package/environments/__tests__/base.js | 2 -- package/index.js | 4 +-- package/utils/get_style_rule.js | 3 +- test/configuration_test.rb | 24 +++++++--------- test/dev_server_test.rb | 14 +++------- test/env_test.rb | 16 +++++++---- test/test_app/config/webpacker.yml | 12 ++++++++ test/test_helper.rb | 12 ++++---- 27 files changed, 229 insertions(+), 130 deletions(-) create mode 100644 package/__tests__/development.js delete mode 100644 package/__tests__/index.js create mode 100644 package/__tests__/production.js create mode 100644 package/__tests__/staging.js create mode 100644 package/__tests__/test.js diff --git a/lib/install/bin/webpack b/lib/install/bin/webpack index 5f9bd1f88..0869ad277 100755 --- a/lib/install/bin/webpack +++ b/lib/install/bin/webpack @@ -1,7 +1,7 @@ #!/usr/bin/env ruby ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development" -ENV["NODE_ENV"] ||= ENV["RAILS_ENV"] +ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development" require "pathname" ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", diff --git a/lib/install/bin/webpack-dev-server b/lib/install/bin/webpack-dev-server index 173b32714..251f65e8e 100755 --- a/lib/install/bin/webpack-dev-server +++ b/lib/install/bin/webpack-dev-server @@ -1,7 +1,7 @@ #!/usr/bin/env ruby ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development" -ENV["NODE_ENV"] ||= ENV["RAILS_ENV"] +ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development" require "pathname" ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", diff --git a/lib/install/config/webpack/development.js b/lib/install/config/webpack/development.js index 81269f651..c5edff94a 100644 --- a/lib/install/config/webpack/development.js +++ b/lib/install/config/webpack/development.js @@ -1,3 +1,5 @@ +process.env.NODE_ENV = process.env.NODE_ENV || 'development' + const environment = require('./environment') module.exports = environment.toWebpackConfig() diff --git a/lib/install/config/webpack/production.js b/lib/install/config/webpack/production.js index 81269f651..be0f53aac 100644 --- a/lib/install/config/webpack/production.js +++ b/lib/install/config/webpack/production.js @@ -1,3 +1,5 @@ +process.env.NODE_ENV = process.env.NODE_ENV || 'production' + const environment = require('./environment') module.exports = environment.toWebpackConfig() diff --git a/lib/install/config/webpack/test.js b/lib/install/config/webpack/test.js index 81269f651..c5edff94a 100644 --- a/lib/install/config/webpack/test.js +++ b/lib/install/config/webpack/test.js @@ -1,3 +1,5 @@ +process.env.NODE_ENV = process.env.NODE_ENV || 'development' + const environment = require('./environment') module.exports = environment.toWebpackConfig() diff --git a/lib/tasks/webpacker/compile.rake b/lib/tasks/webpacker/compile.rake index c8ea225e6..4a9056422 100644 --- a/lib/tasks/webpacker/compile.rake +++ b/lib/tasks/webpacker/compile.rake @@ -1,3 +1,5 @@ +ENV["NODE_ENV"] ||= "production" + $stdout.sync = true def ensure_log_goes_to_stdout diff --git a/lib/webpacker/compiler.rb b/lib/webpacker/compiler.rb index 1ebcf68f6..14be5ac73 100644 --- a/lib/webpacker/compiler.rb +++ b/lib/webpacker/compiler.rb @@ -78,8 +78,7 @@ def compilation_digest_path end def webpack_env - env.merge("NODE_ENV" => @webpacker.env, - "WEBPACKER_ASSET_HOST" => ActionController::Base.helpers.compute_asset_host, + env.merge("WEBPACKER_ASSET_HOST" => ActionController::Base.helpers.compute_asset_host, "WEBPACKER_RELATIVE_URL_ROOT" => ActionController::Base.relative_url_root) end end diff --git a/lib/webpacker/dev_server_runner.rb b/lib/webpacker/dev_server_runner.rb index 8a4b93242..c21db0e9c 100644 --- a/lib/webpacker/dev_server_runner.rb +++ b/lib/webpacker/dev_server_runner.rb @@ -14,14 +14,14 @@ def run private def load_config @config_file = File.join(@app_path, "config/webpacker.yml") - dev_server = YAML.load_file(@config_file)[ENV["NODE_ENV"]]["dev_server"] + dev_server = YAML.load_file(@config_file)[ENV["RAILS_ENV"]]["dev_server"] @hostname = dev_server["host"] @port = dev_server["port"] @pretty = dev_server.fetch("pretty", true) rescue Errno::ENOENT, NoMethodError - $stdout.puts "webpack dev_server configuration not found in #{@config_file}." + $stdout.puts "webpack dev_server configuration not found in #{@config_file}[#{ENV["RAILS_ENV"]}]." $stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker" exit! end diff --git a/lib/webpacker/env.rb b/lib/webpacker/env.rb index f5b1e5c9e..4539456bb 100644 --- a/lib/webpacker/env.rb +++ b/lib/webpacker/env.rb @@ -18,11 +18,11 @@ def inquire private def current - (ENV["NODE_ENV"] || Rails.env).presence_in(available_environments) + Rails.env.presence_in(available_environments) end def fallback_env_warning - logger.info "NODE_ENV=#{ENV["NODE_ENV"]} and RAILS_ENV=#{Rails.env} environment is not defined in config/webpacker.yml, falling back to #{DEFAULT} environment" + logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/webpacker.yml, falling back to #{DEFAULT} environment" end def available_environments diff --git a/package/__tests__/dev_server.js b/package/__tests__/dev_server.js index 6e0b31f4b..1a5d6fb20 100644 --- a/package/__tests__/dev_server.js +++ b/package/__tests__/dev_server.js @@ -8,8 +8,9 @@ describe('DevServer', () => { beforeEach(() => jest.resetModules()) afterAll(chdirCwd) - test('with NODE_ENV set to development', () => { + test('with NODE_ENV and RAILS_ENV set to development', () => { process.env.NODE_ENV = 'development' + process.env.RAILS_ENV = 'development' process.env.WEBPACKER_DEV_SERVER_HOST = '0.0.0.0' process.env.WEBPACKER_DEV_SERVER_PORT = 5000 @@ -19,7 +20,8 @@ describe('DevServer', () => { expect(devServer.port).toEqual('5000') }) - test('with NODE_ENV set to production', () => { + test('with NODE_ENV and RAILS_ENV set to production', () => { + process.env.RAILS_ENV = 'production' process.env.NODE_ENV = 'production' expect(require('../dev_server')).toEqual({}) }) diff --git a/package/__tests__/development.js b/package/__tests__/development.js new file mode 100644 index 000000000..bd1f1e8e1 --- /dev/null +++ b/package/__tests__/development.js @@ -0,0 +1,30 @@ +/* test expect, describe, afterAll, beforeEach */ + +const { resolve } = require('path') +const { chdirTestApp, chdirCwd } = require('../utils/helpers') + +chdirTestApp() + +describe('Development environment', () => { + afterAll(chdirCwd) + + describe('toWebpackConfig', () => { + beforeEach(() => jest.resetModules()) + + test('should use development config and environment', () => { + process.env.RAILS_ENV = 'development' + process.env.NODE_ENV = 'development' + const { environment } = require('../index') + + const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs')) + expect(config.output.publicPath).toEqual('/packs/') + expect(config).toMatchObject({ + devServer: { + host: 'localhost', + port: 3035 + } + }) + }) + }) +}) diff --git a/package/__tests__/env.js b/package/__tests__/env.js index 707c9e205..14823bf94 100644 --- a/package/__tests__/env.js +++ b/package/__tests__/env.js @@ -8,21 +8,39 @@ describe('Env', () => { beforeEach(() => jest.resetModules()) afterAll(chdirCwd) - test('with NODE_ENV set to development', () => { + test('with NODE_ENV and RAILS_ENV set to development', () => { + process.env.RAILS_ENV = 'development' process.env.NODE_ENV = 'development' - expect(require('../env')).toEqual('development') + expect(require('../env')).toEqual({ + railsEnv: 'development', + nodeEnv: 'development' + }) }) test('with undefined NODE_ENV and RAILS_ENV set to development', () => { - delete process.env.NODE_ENV process.env.RAILS_ENV = 'development' - expect(require('../env')).toEqual('development') + delete process.env.NODE_ENV + expect(require('../env')).toEqual({ + railsEnv: 'development', + nodeEnv: 'production' + }) }) - test('with a non-standard environment', () => { - process.env.NODE_ENV = 'foo' - process.env.RAILS_ENV = 'foo' - expect(require('../env')).toEqual('production') + test('with undefined NODE_ENV and RAILS_ENV', () => { + delete process.env.NODE_ENV delete process.env.RAILS_ENV + expect(require('../env')).toEqual({ + railsEnv: 'production', + nodeEnv: 'production' + }) + }) + + test('with a non-standard environment', () => { + process.env.RAILS_ENV = 'staging' + process.env.NODE_ENV = 'staging' + expect(require('../env')).toEqual({ + railsEnv: 'staging', + nodeEnv: 'production' + }) }) }) diff --git a/package/__tests__/index.js b/package/__tests__/index.js deleted file mode 100644 index 9e19e72b9..000000000 --- a/package/__tests__/index.js +++ /dev/null @@ -1,31 +0,0 @@ -/* global test expect, describe */ - -const { chdirTestApp, chdirCwd } = require('../utils/helpers') - -chdirTestApp() - -describe('Webpacker', () => { - beforeEach(() => jest.resetModules()) - afterAll(chdirCwd) - - test('with NODE_ENV set to development', () => { - process.env.NODE_ENV = 'development' - const { environment } = require('../index') - expect(environment.toWebpackConfig()).toMatchObject({ - devServer: { - host: 'localhost', - port: 3035 - } - }) - }) - - test('with a non-standard env', () => { - process.env.NODE_ENV = 'staging' - process.env.RAILS_ENV = 'staging' - const { environment } = require('../index') - expect(environment.toWebpackConfig()).toMatchObject({ - devtool: 'nosources-source-map', - stats: 'normal' - }) - }) -}) diff --git a/package/__tests__/production.js b/package/__tests__/production.js new file mode 100644 index 000000000..96f212817 --- /dev/null +++ b/package/__tests__/production.js @@ -0,0 +1,27 @@ +/* test expect, describe, afterAll, beforeEach */ + +const { resolve } = require('path') +const { chdirTestApp, chdirCwd } = require('../utils/helpers') + +chdirTestApp() + +describe('Production environment', () => { + afterAll(chdirCwd) + + describe('toWebpackConfig', () => { + beforeEach(() => jest.resetModules()) + + test('should use production config and environment', () => { + process.env.RAILS_ENV = 'production' + const { environment } = require('../index') + + const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs')) + expect(config.output.publicPath).toEqual('/packs/') + expect(config).toMatchObject({ + devtool: 'nosources-source-map', + stats: 'normal' + }) + }) + }) +}) diff --git a/package/__tests__/staging.js b/package/__tests__/staging.js new file mode 100644 index 000000000..794e61553 --- /dev/null +++ b/package/__tests__/staging.js @@ -0,0 +1,27 @@ +/* test expect, describe, afterAll, beforeEach */ + +const { resolve } = require('path') +const { chdirTestApp, chdirCwd } = require('../utils/helpers') + +chdirTestApp() + +describe('Custom environment', () => { + afterAll(chdirCwd) + + describe('toWebpackConfig', () => { + beforeEach(() => jest.resetModules()) + + test('should use staging config and production environment', () => { + process.env.RAILS_ENV = 'staging' + const { environment } = require('../index') + + const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs-staging')) + expect(config.output.publicPath).toEqual('/packs-staging/') + expect(config).toMatchObject({ + devtool: 'nosources-source-map', + stats: 'normal' + }) + }) + }) +}) diff --git a/package/__tests__/test.js b/package/__tests__/test.js new file mode 100644 index 000000000..4fa6cbff0 --- /dev/null +++ b/package/__tests__/test.js @@ -0,0 +1,23 @@ +/* test expect, describe, afterAll, beforeEach */ + +const { resolve } = require('path') +const { chdirTestApp, chdirCwd } = require('../utils/helpers') + +chdirTestApp() + +describe('Test environment', () => { + afterAll(chdirCwd) + + describe('toWebpackConfig', () => { + beforeEach(() => jest.resetModules()) + + test('should use test config and production environment', () => { + process.env.RAILS_ENV = 'test' + const { environment } = require('../index') + + const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs-test')) + expect(config.output.publicPath).toEqual('/packs-test/') + }) + }) +}) diff --git a/package/config.js b/package/config.js index 24390690c..48ae7778a 100644 --- a/package/config.js +++ b/package/config.js @@ -3,25 +3,23 @@ const { safeLoad } = require('js-yaml') const { readFileSync } = require('fs') const deepMerge = require('./utils/deep_merge') const { isArray } = require('./utils/helpers') -const env = require('./env') +const { railsEnv } = require('./env') 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 app = safeLoad(readFileSync(configPath), 'utf8')[env] - - if (isArray(app.extensions) && app.extensions.length) { - delete defaults.extensions - } +const getDefaultConfig = () => { + const defaultConfig = safeLoad(readFileSync(defaultConfigPath), 'utf8') + return defaultConfig[railsEnv] || defaultConfig.production +} - const config = deepMerge(defaults, app) +const defaults = getDefaultConfig() +const app = safeLoad(readFileSync(configPath), 'utf8')[railsEnv] - config.outputPath = resolve('public', config.public_output_path) - config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1') +if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions - return config -} +const config = deepMerge(defaults, app) +config.outputPath = resolve('public', config.public_output_path) +config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1') -module.exports = getConfig() +module.exports = config diff --git a/package/dev_server.js b/package/dev_server.js index b00a54c7e..49510c3bc 100644 --- a/package/dev_server.js +++ b/package/dev_server.js @@ -1,4 +1,4 @@ -const { isBoolean, isEmpty } = require('./utils/helpers') +const { isBoolean } = require('./utils/helpers') const config = require('./config') const fetch = (key) => { @@ -6,18 +6,13 @@ const fetch = (key) => { return isBoolean(value) ? JSON.parse(value) : value } -const devServer = () => { - const devServerConfig = config.dev_server +const devServerConfig = config.dev_server - if (devServerConfig) { - Object.keys(devServerConfig).forEach((key) => { - const envValue = fetch(`WEBPACKER_DEV_SERVER_${key.toUpperCase().replace(/_/g, '')}`) - if (isEmpty(envValue)) return devServerConfig[key] - devServerConfig[key] = envValue - }) - } - - return devServerConfig || {} +if (devServerConfig) { + Object.keys(devServerConfig).forEach((key) => { + const envValue = fetch(`WEBPACKER_DEV_SERVER_${key.toUpperCase().replace(/_/g, '')}`) + if (envValue) devServerConfig[key] = envValue + }) } -module.exports = devServer() +module.exports = devServerConfig || {} diff --git a/package/env.js b/package/env.js index 90263b267..9263294b6 100644 --- a/package/env.js +++ b/package/env.js @@ -2,22 +2,18 @@ const { resolve } = require('path') const { safeLoad } = require('js-yaml') const { readFileSync } = require('fs') +const NODE_ENVIRONMENTS = ['development', 'production'] +const DEFAULT = 'production' const configPath = resolve('config', 'webpacker.yml') -const DEFAULT_ENV = 'production' -const env = () => { - const nodeEnv = process.env.NODE_ENV - const railsEnv = process.env.RAILS_ENV - const config = safeLoad(readFileSync(configPath), 'utf8') - const availableEnvironments = Object.keys(config).join('|') - const regex = new RegExp(availableEnvironments, 'g') +const railsEnv = process.env.RAILS_ENV +const nodeEnv = process.env.NODE_ENV - if (nodeEnv && nodeEnv.match(regex)) return nodeEnv - if (railsEnv && railsEnv.match(regex)) return railsEnv +const config = safeLoad(readFileSync(configPath), 'utf8') +const availableEnvironments = Object.keys(config).join('|') +const regex = new RegExp(availableEnvironments, 'g') - /* eslint no-console: 0 */ - console.warn(`NODE_ENV=${nodeEnv} and RAILS_ENV=${railsEnv} environment is not defined in config/webpacker.yml, falling back to ${DEFAULT_ENV}`) - return DEFAULT_ENV +module.exports = { + railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT, + nodeEnv: nodeEnv && NODE_ENVIRONMENTS.includes(nodeEnv) ? nodeEnv : DEFAULT } - -module.exports = env() diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index d16a6f196..720d974c1 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -31,8 +31,6 @@ describe('Environment', () => { const config = environment.toWebpackConfig() expect(config.output.filename).toEqual('[name]-[chunkhash].js') expect(config.output.chunkFilename).toEqual('[name]-[chunkhash].chunk.js') - expect(config.output.path).toEqual(resolve('public', 'packs-test')) - expect(config.output.publicPath).toEqual('/packs-test/') }) test('should return default loader rules for each file in config/loaders', () => { diff --git a/package/index.js b/package/index.js index 02a74b6bb..8f61d1b7d 100644 --- a/package/index.js +++ b/package/index.js @@ -5,12 +5,12 @@ const { resolve } = require('path') const { existsSync } = require('fs') const Environment = require('./environments/base') const loaders = require('./rules') -const env = require('./env') const config = require('./config') const devServer = require('./dev_server') +const { nodeEnv } = require('./env') const createEnvironment = () => { - const path = resolve(__dirname, 'environments', `${env}.js`) + const path = resolve(__dirname, 'environments', `${nodeEnv}.js`) const constructor = existsSync(path) ? require(path) : Environment return new constructor() } diff --git a/package/utils/get_style_rule.js b/package/utils/get_style_rule.js index d5a0e266b..c144b7f63 100644 --- a/package/utils/get_style_rule.js +++ b/package/utils/get_style_rule.js @@ -1,9 +1,10 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin') const path = require('path') const devServer = require('../dev_server') +const { nodeEnv } = require('../env') const postcssConfigPath = path.resolve(process.cwd(), '.postcssrc.yml') -const isProduction = process.env.NODE_ENV === 'production' +const isProduction = nodeEnv === 'production' const inDevServer = process.argv.find(v => v.includes('webpack-dev-server')) const isHMR = inDevServer && (devServer && devServer.hmr) const extractCSS = !isHMR || isProduction diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 5fe00ab92..1dc05c4d5 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -41,30 +41,26 @@ def test_extensions end def test_cache_manifest? - with_node_env("development") do - refute reloaded_config.cache_manifest? - end + assert Webpacker.config.cache_manifest? - with_node_env("test") do - refute reloaded_config.cache_manifest? + with_rails_env("development") do + refute Webpacker.config.cache_manifest? end - with_node_env("production") do - assert reloaded_config.cache_manifest? + with_rails_env("test") do + refute Webpacker.config.cache_manifest? end end def test_compile? - with_node_env("development") do - assert reloaded_config.compile? - end + refute Webpacker.config.compile? - with_node_env("test") do - assert reloaded_config.compile? + with_rails_env("development") do + assert Webpacker.config.compile? end - with_node_env("production") do - refute reloaded_config.compile? + with_rails_env("test") do + assert Webpacker.config.compile? end end end diff --git a/test/dev_server_test.rb b/test/dev_server_test.rb index 4c6bf1ea8..93d68a236 100644 --- a/test/dev_server_test.rb +++ b/test/dev_server_test.rb @@ -2,29 +2,23 @@ class DevServerTest < Webpacker::Test def test_running? - with_node_env("production") do - reloaded_config - refute Webpacker.dev_server.running? - end + refute Webpacker.dev_server.running? end def test_host - with_node_env("development") do - reloaded_config + with_rails_env("development") do assert_equal Webpacker.dev_server.host, "localhost" end end def test_port - with_node_env("development") do - reloaded_config + with_rails_env("development") do assert_equal Webpacker.dev_server.port, 3035 end end def test_https? - with_node_env("development") do - reloaded_config + with_rails_env("development") do assert_equal Webpacker.dev_server.https?, false end end diff --git a/test/env_test.rb b/test/env_test.rb index 1fc743011..efb6a3c18 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -2,14 +2,18 @@ class EnvTest < Webpacker::Test def test_current - reloaded_config - assert_equal Webpacker.env, "production" + assert_equal Webpacker.env, Rails.env end - def test_custom - with_node_env("default") do - reloaded_config - assert_equal Webpacker.env, "default" + def test_custom_without_config + with_rails_env("foo") do + assert_equal Webpacker.env, "production" + end + end + + def test_custom_with_config + with_rails_env("staging") do + assert_equal Webpacker.env, "staging" end end diff --git a/test/test_app/config/webpacker.yml b/test/test_app/config/webpacker.yml index beaf1831c..cd7cd329e 100644 --- a/test/test_app/config/webpacker.yml +++ b/test/test_app/config/webpacker.yml @@ -61,3 +61,15 @@ production: # Cache manifest.json for performance cache_manifest: true + +staging: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + + # Compile staging packs to a separate directory + public_output_path: packs-staging diff --git a/test/test_helper.rb b/test/test_helper.rb index 54b7d30eb..030b77e68 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,7 +6,7 @@ require_relative "test_app/config/environment" -ENV["NODE_ENV"] = "production" +Rails.env = "production" Webpacker.instance = Webpacker::Instance.new \ root_path: Pathname.new(File.expand_path("test_app", __dir__)), @@ -21,11 +21,13 @@ def reloaded_config Webpacker.config end - def with_node_env(env) - original = ENV["NODE_ENV"] - ENV["NODE_ENV"] = env + def with_rails_env(env) + original = Rails.env + Rails.env = ActiveSupport::StringInquirer.new(env) + reloaded_config yield ensure - ENV["NODE_ENV"] = original + Rails.env = ActiveSupport::StringInquirer.new(original) + reloaded_config end end