Skip to content

Commit 01c6bc0

Browse files
committed
Detangle NODE_ENV and RAILS_ENV
1 parent 3825371 commit 01c6bc0

28 files changed

+236
-130
lines changed

lib/install/bin/webpack

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4-
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
4+
ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development"
55

66
require "pathname"
77
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",

lib/install/bin/webpack-dev-server

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4-
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
4+
ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development"
55

66
require "pathname"
77
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
2+
13
const environment = require('./environment')
24

35
module.exports = environment.toWebpackConfig()
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
2+
13
const environment = require('./environment')
24

35
module.exports = environment.toWebpackConfig()

lib/install/config/webpack/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
2+
13
const environment = require('./environment')
24

35
module.exports = environment.toWebpackConfig()

lib/tasks/webpacker/compile.rake

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ENV["NODE_ENV"] ||= "production"
2+
13
$stdout.sync = true
24

35
def ensure_log_goes_to_stdout

lib/webpacker/compiler.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def compilation_digest_path
7878
end
7979

8080
def webpack_env
81-
env.merge("NODE_ENV" => @webpacker.env,
82-
"WEBPACKER_ASSET_HOST" => ActionController::Base.helpers.compute_asset_host,
81+
env.merge("WEBPACKER_ASSET_HOST" => ActionController::Base.helpers.compute_asset_host,
8382
"WEBPACKER_RELATIVE_URL_ROOT" => ActionController::Base.relative_url_root)
8483
end
8584
end

lib/webpacker/dev_server_runner.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def run
1414
private
1515
def load_config
1616
@config_file = File.join(@app_path, "config/webpacker.yml")
17-
dev_server = YAML.load_file(@config_file)[ENV["NODE_ENV"]]["dev_server"]
17+
dev_server = YAML.load_file(@config_file)[ENV["RAILS_ENV"]]["dev_server"]
1818

1919
@hostname = dev_server["host"]
2020
@port = dev_server["port"]
2121
@pretty = dev_server.fetch("pretty", true)
2222

2323
rescue Errno::ENOENT, NoMethodError
24-
$stdout.puts "webpack dev_server configuration not found in #{@config_file}."
24+
$stdout.puts "webpack dev_server configuration not found in #{@config_file}[#{ENV["RAILS_ENV"]}]."
2525
$stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker"
2626
exit!
2727
end

lib/webpacker/env.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ def inquire
1818

1919
private
2020
def current
21-
(ENV["NODE_ENV"] || Rails.env).presence_in(available_environments)
21+
Rails.env.presence_in(available_environments)
2222
end
2323

2424
def fallback_env_warning
25-
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"
25+
logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/webpacker.yml, falling back to #{DEFAULT} environment"
2626
end
2727

2828
def available_environments

package/__tests__/dev_server.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ describe('DevServer', () => {
88
beforeEach(() => jest.resetModules())
99
afterAll(chdirCwd)
1010

11-
test('with NODE_ENV set to development', () => {
11+
test('with NODE_ENV and RAILS_ENV set to development', () => {
1212
process.env.NODE_ENV = 'development'
13+
process.env.RAILS_ENV = 'development'
1314
process.env.WEBPACKER_DEV_SERVER_HOST = '0.0.0.0'
1415
process.env.WEBPACKER_DEV_SERVER_PORT = 5000
1516

@@ -19,7 +20,8 @@ describe('DevServer', () => {
1920
expect(devServer.port).toEqual('5000')
2021
})
2122

22-
test('with NODE_ENV set to production', () => {
23+
test('with NODE_ENV and RAILS_ENV set to production', () => {
24+
process.env.RAILS_ENV = 'production'
2325
process.env.NODE_ENV = 'production'
2426
expect(require('../dev_server')).toEqual({})
2527
})

package/__tests__/development.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* test expect, describe, afterAll, beforeEach */
2+
3+
const { resolve } = require('path')
4+
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5+
6+
chdirTestApp()
7+
8+
describe('Development environment', () => {
9+
afterAll(chdirCwd)
10+
11+
describe('toWebpackConfig', () => {
12+
beforeEach(() => jest.resetModules())
13+
14+
test('should use development config and environment', () => {
15+
process.env.RAILS_ENV = 'development'
16+
process.env.NODE_ENV = 'development'
17+
const { environment } = require('../index')
18+
19+
const config = environment.toWebpackConfig()
20+
expect(config.output.path).toEqual(resolve('public', 'packs'))
21+
expect(config.output.publicPath).toEqual('/packs/')
22+
expect(config).toMatchObject({
23+
devServer: {
24+
host: 'localhost',
25+
port: 3035
26+
}
27+
})
28+
})
29+
})
30+
})

package/__tests__/env.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,39 @@ describe('Env', () => {
88
beforeEach(() => jest.resetModules())
99
afterAll(chdirCwd)
1010

11-
test('with NODE_ENV set to development', () => {
11+
test('with NODE_ENV and RAILS_ENV set to development', () => {
12+
process.env.RAILS_ENV = 'development'
1213
process.env.NODE_ENV = 'development'
13-
expect(require('../env')).toEqual('development')
14+
expect(require('../env')).toEqual({
15+
railsEnv: 'development',
16+
nodeEnv: 'development'
17+
})
1418
})
1519

1620
test('with undefined NODE_ENV and RAILS_ENV set to development', () => {
17-
delete process.env.NODE_ENV
1821
process.env.RAILS_ENV = 'development'
19-
expect(require('../env')).toEqual('development')
22+
delete process.env.NODE_ENV
23+
expect(require('../env')).toEqual({
24+
railsEnv: 'development',
25+
nodeEnv: 'production'
26+
})
2027
})
2128

22-
test('with a non-standard environment', () => {
23-
process.env.NODE_ENV = 'foo'
24-
process.env.RAILS_ENV = 'foo'
25-
expect(require('../env')).toEqual('production')
29+
test('with undefined NODE_ENV and RAILS_ENV', () => {
30+
delete process.env.NODE_ENV
2631
delete process.env.RAILS_ENV
32+
expect(require('../env')).toEqual({
33+
railsEnv: 'production',
34+
nodeEnv: 'production'
35+
})
36+
})
37+
38+
test('with a non-standard environment', () => {
39+
process.env.RAILS_ENV = 'staging'
40+
process.env.NODE_ENV = 'staging'
41+
expect(require('../env')).toEqual({
42+
railsEnv: 'staging',
43+
nodeEnv: 'production'
44+
})
2745
})
2846
})

package/__tests__/index.js

-31
This file was deleted.

package/__tests__/production.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* test expect, describe, afterAll, beforeEach */
2+
3+
const { resolve } = require('path')
4+
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5+
6+
chdirTestApp()
7+
8+
describe('Production environment', () => {
9+
afterAll(chdirCwd)
10+
11+
describe('toWebpackConfig', () => {
12+
beforeEach(() => jest.resetModules())
13+
14+
test('should use production config and environment', () => {
15+
process.env.RAILS_ENV = 'production'
16+
const { environment } = require('../index')
17+
18+
const config = environment.toWebpackConfig()
19+
expect(config.output.path).toEqual(resolve('public', 'packs'))
20+
expect(config.output.publicPath).toEqual('/packs/')
21+
expect(config).toMatchObject({
22+
devtool: 'nosources-source-map',
23+
stats: 'normal'
24+
})
25+
})
26+
})
27+
})

package/__tests__/staging.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* test expect, describe, afterAll, beforeEach */
2+
3+
const { resolve } = require('path')
4+
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5+
6+
chdirTestApp()
7+
8+
describe('Custom environment', () => {
9+
afterAll(chdirCwd)
10+
11+
describe('toWebpackConfig', () => {
12+
beforeEach(() => jest.resetModules())
13+
14+
test('should use staging config and production environment', () => {
15+
process.env.RAILS_ENV = 'staging'
16+
const { environment } = require('../index')
17+
18+
const config = environment.toWebpackConfig()
19+
expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
20+
expect(config.output.publicPath).toEqual('/packs-staging/')
21+
expect(config).toMatchObject({
22+
devtool: 'nosources-source-map',
23+
stats: 'normal'
24+
})
25+
})
26+
})
27+
})

package/__tests__/test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* test expect, describe, afterAll, beforeEach */
2+
3+
const { resolve } = require('path')
4+
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5+
6+
chdirTestApp()
7+
8+
describe('Test environment', () => {
9+
afterAll(chdirCwd)
10+
11+
describe('toWebpackConfig', () => {
12+
beforeEach(() => jest.resetModules())
13+
14+
test('should use test config and production environment', () => {
15+
process.env.RAILS_ENV = 'test'
16+
const { environment } = require('../index')
17+
18+
const config = environment.toWebpackConfig()
19+
expect(config.output.path).toEqual(resolve('public', 'packs-test'))
20+
expect(config.output.publicPath).toEqual('/packs-test/')
21+
})
22+
})
23+
})

package/config.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ const { safeLoad } = require('js-yaml')
33
const { readFileSync } = require('fs')
44
const deepMerge = require('./utils/deep_merge')
55
const { isArray } = require('./utils/helpers')
6-
const env = require('./env')
6+
const { railsEnv } = require('./env')
77

88
const defaultConfigPath = require.resolve('../lib/install/config/webpacker.yml')
99
const configPath = resolve('config', 'webpacker.yml')
1010

11-
const getConfig = () => {
12-
const defaults = safeLoad(readFileSync(defaultConfigPath), 'utf8')[env]
13-
const app = safeLoad(readFileSync(configPath), 'utf8')[env]
14-
15-
if (isArray(app.extensions) && app.extensions.length) {
16-
delete defaults.extensions
17-
}
11+
const getDefaultConfig = () => {
12+
const defaultConfig = safeLoad(readFileSync(defaultConfigPath), 'utf8')
13+
return defaultConfig[railsEnv] || defaultConfig.production
14+
}
1815

19-
const config = deepMerge(defaults, app)
16+
const defaults = getDefaultConfig()
17+
const app = safeLoad(readFileSync(configPath), 'utf8')[railsEnv]
2018

21-
config.outputPath = resolve('public', config.public_output_path)
22-
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')
19+
if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions
2320

24-
return config
25-
}
21+
const config = deepMerge(defaults, app)
22+
config.outputPath = resolve('public', config.public_output_path)
23+
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')
2624

27-
module.exports = getConfig()
25+
module.exports = config

package/dev_server.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
const { isBoolean, isEmpty } = require('./utils/helpers')
1+
const { isBoolean } = require('./utils/helpers')
22
const config = require('./config')
33

44
const fetch = (key) => {
55
const value = process.env[key]
66
return isBoolean(value) ? JSON.parse(value) : value
77
}
88

9-
const devServer = () => {
10-
const devServerConfig = config.dev_server
9+
const devServerConfig = config.dev_server
1110

12-
if (devServerConfig) {
13-
Object.keys(devServerConfig).forEach((key) => {
14-
const envValue = fetch(`WEBPACKER_DEV_SERVER_${key.toUpperCase().replace(/_/g, '')}`)
15-
if (isEmpty(envValue)) return devServerConfig[key]
16-
devServerConfig[key] = envValue
17-
})
18-
}
19-
20-
return devServerConfig || {}
11+
if (devServerConfig) {
12+
Object.keys(devServerConfig).forEach((key) => {
13+
const envValue = fetch(`WEBPACKER_DEV_SERVER_${key.toUpperCase().replace(/_/g, '')}`)
14+
if (envValue) devServerConfig[key] = envValue
15+
})
2116
}
2217

23-
module.exports = devServer()
18+
module.exports = devServerConfig || {}

package/env.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ const { resolve } = require('path')
22
const { safeLoad } = require('js-yaml')
33
const { readFileSync } = require('fs')
44

5+
const NODE_ENVIRONMENTS = ['development', 'production']
6+
const DEFAULT = 'production'
57
const configPath = resolve('config', 'webpacker.yml')
6-
const DEFAULT_ENV = 'production'
78

8-
const env = () => {
9-
const nodeEnv = process.env.NODE_ENV
10-
const railsEnv = process.env.RAILS_ENV
11-
const config = safeLoad(readFileSync(configPath), 'utf8')
12-
const availableEnvironments = Object.keys(config).join('|')
13-
const regex = new RegExp(availableEnvironments, 'g')
9+
const railsEnv = process.env.RAILS_ENV
10+
const nodeEnv = process.env.NODE_ENV
1411

15-
if (nodeEnv && nodeEnv.match(regex)) return nodeEnv
16-
if (railsEnv && railsEnv.match(regex)) return railsEnv
12+
const config = safeLoad(readFileSync(configPath), 'utf8')
13+
const availableEnvironments = Object.keys(config).join('|')
14+
const regex = new RegExp(availableEnvironments, 'g')
1715

18-
/* eslint no-console: 0 */
19-
console.warn(`NODE_ENV=${nodeEnv} and RAILS_ENV=${railsEnv} environment is not defined in config/webpacker.yml, falling back to ${DEFAULT_ENV}`)
20-
return DEFAULT_ENV
16+
module.exports = {
17+
railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
18+
nodeEnv: nodeEnv && NODE_ENVIRONMENTS.includes(nodeEnv) ? nodeEnv : DEFAULT
2119
}
22-
23-
module.exports = env()

0 commit comments

Comments
 (0)