-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consitently use NODE_ENV and restructure npm package for clarity (#1304)
- Loading branch information
1 parent
af471dc
commit 3923768
Showing
30 changed files
with
374 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Webpacker::Env | ||
DEFAULT = "production".freeze | ||
|
||
delegate :config_path, :logger, to: :@webpacker | ||
|
||
def self.inquire(webpacker) | ||
new(webpacker).inquire | ||
end | ||
|
||
def initialize(webpacker) | ||
@webpacker = webpacker | ||
end | ||
|
||
def inquire | ||
fallback_env_warning unless current | ||
(current || DEFAULT).inquiry | ||
end | ||
|
||
private | ||
def current | ||
(ENV["NODE_ENV"] || 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" | ||
end | ||
|
||
def available_environments | ||
if config_path.exist? | ||
YAML.load(config_path.read).keys | ||
else | ||
[].freeze | ||
end | ||
rescue Psych::SyntaxError => e | ||
raise "YAML syntax error occurred while parsing #{config_path}. " \ | ||
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ | ||
"Error: #{e.message}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* global test expect, describe */ | ||
|
||
const { chdirTestApp, chdirCwd } = require('../utils/helpers') | ||
|
||
chdirTestApp() | ||
|
||
describe('DevServer', () => { | ||
beforeEach(() => jest.resetModules()) | ||
afterAll(chdirCwd) | ||
|
||
test('with NODE_ENV set to development', () => { | ||
process.env.NODE_ENV = 'development' | ||
process.env.WEBPACKER_DEV_SERVER_HOST = '0.0.0.0' | ||
process.env.WEBPACKER_DEV_SERVER_PORT = 5000 | ||
|
||
const devServer = require('../dev_server') | ||
expect(devServer).toBeDefined() | ||
expect(devServer.host).toEqual('0.0.0.0') | ||
expect(devServer.port).toEqual('5000') | ||
}) | ||
|
||
test('with NODE_ENV set to production', () => { | ||
process.env.NODE_ENV = 'production' | ||
expect(require('../dev_server')).toEqual({}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* global test expect, describe */ | ||
|
||
const { chdirTestApp, chdirCwd } = require('../utils/helpers') | ||
|
||
chdirTestApp() | ||
|
||
describe('Env', () => { | ||
beforeEach(() => jest.resetModules()) | ||
afterAll(chdirCwd) | ||
|
||
test('with NODE_ENV set to development', () => { | ||
process.env.NODE_ENV = 'development' | ||
expect(require('../env')).toEqual('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') | ||
}) | ||
|
||
test('with a non-standard environment', () => { | ||
process.env.NODE_ENV = 'foo' | ||
process.env.RAILS_ENV = 'foo' | ||
expect(require('../env')).toEqual('production') | ||
delete process.env.RAILS_ENV | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* 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' | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { isBoolean, isEmpty } = 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 | ||
|
||
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 || {} | ||
} | ||
|
||
module.exports = devServer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { resolve } = require('path') | ||
const { safeLoad } = require('js-yaml') | ||
const { readFileSync } = require('fs') | ||
|
||
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') | ||
|
||
if (nodeEnv && nodeEnv.match(regex)) return nodeEnv | ||
if (railsEnv && railsEnv.match(regex)) return railsEnv | ||
|
||
/* 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 = env() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.