Skip to content

Commit

Permalink
Detangle NODE_ENV and RAILS_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravtiwari committed Mar 15, 2018
1 parent 3825371 commit c0d28df
Show file tree
Hide file tree
Showing 28 changed files with 230 additions and 115 deletions.
2 changes: 1 addition & 1 deletion lib/install/bin/webpack
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lib/install/bin/webpack-dev-server
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions lib/install/config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()
2 changes: 2 additions & 0 deletions lib/install/config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()
2 changes: 2 additions & 0 deletions lib/install/config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()
2 changes: 2 additions & 0 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ENV["NODE_ENV"] ||= "production"

$stdout.sync = true

def ensure_log_goes_to_stdout
Expand Down
3 changes: 1 addition & 2 deletions lib/webpacker/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/webpacker/dev_server_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/webpacker/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions package/__tests__/dev_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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({})
})
Expand Down
30 changes: 30 additions & 0 deletions package/__tests__/development.js
Original file line number Diff line number Diff line change
@@ -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
}
})
})
})
})
34 changes: 26 additions & 8 deletions package/__tests__/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
})
})
31 changes: 0 additions & 31 deletions package/__tests__/index.js

This file was deleted.

27 changes: 27 additions & 0 deletions package/__tests__/production.js
Original file line number Diff line number Diff line change
@@ -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'
})
})
})
})
27 changes: 27 additions & 0 deletions package/__tests__/staging.js
Original file line number Diff line number Diff line change
@@ -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'
})
})
})
})
23 changes: 23 additions & 0 deletions package/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -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/')
})
})
})
26 changes: 12 additions & 14 deletions package/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 8 additions & 13 deletions package/dev_server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const { isBoolean, isEmpty } = require('./utils/helpers')
const { isBoolean } = require('./utils/helpers')
const config = require('./config')

const fetch = (key) => {
const value = process.env[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 || {}
24 changes: 10 additions & 14 deletions package/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading

0 comments on commit c0d28df

Please sign in to comment.