-
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
- Loading branch information
1 parent
f01102a
commit 0d08ab4
Showing
30 changed files
with
393 additions
and
167 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,46 @@ | ||
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 | ||
node_env.presence_in(available_environments) | ||
end | ||
|
||
def fallback_env_warning | ||
logger.info "#{node_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 | ||
|
||
def node_env | ||
ENV.fetch("NODE_ENV") | ||
rescue KeyError | ||
raise "Using webpacker requires that you set NODE_ENV environment variables. " \ | ||
"Valid values are #{available_environments.join(', ')}" | ||
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,31 @@ | ||
/* global test expect, describe */ | ||
|
||
const { chdirTestApp, chdirCwd } = require('../utils/helpers') | ||
|
||
chdirTestApp() | ||
|
||
describe('DevServer', () => { | ||
beforeEach(() => jest.resetModules()) | ||
afterAll(chdirCwd) | ||
|
||
test('with development environment', () => { | ||
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 undefined environment', () => { | ||
delete process.env.NODE_ENV | ||
expect(() => require('../dev_server')).toThrow() | ||
}) | ||
|
||
test('with production environment', () => { | ||
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() | ||
|
||
const error = `Using 'webpacker' requires that you specify 'NODE_ENV' environment variables. | ||
Valid values are default|development|test|production. Instead, received: undefined` | ||
|
||
describe('Env', () => { | ||
beforeEach(() => jest.resetModules()) | ||
afterAll(chdirCwd) | ||
|
||
test('returns set node environment', () => { | ||
process.env.NODE_ENV = 'development' | ||
expect(require('../env')).toEqual('development') | ||
}) | ||
|
||
test('with no NODE_ENV throws an error', () => { | ||
delete process.env.NODE_ENV | ||
expect(() => require('../env')).toThrowError(error) | ||
}) | ||
|
||
test('with non-standard environment', () => { | ||
process.env.NODE_ENV = 'foo' | ||
expect(require('../env')).toEqual('production') | ||
}) | ||
}) |
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,35 @@ | ||
/* global test expect, describe */ | ||
|
||
const { chdirTestApp, chdirCwd } = require('../utils/helpers') | ||
|
||
chdirTestApp() | ||
|
||
describe('Webpack config', () => { | ||
beforeEach(() => jest.resetModules()) | ||
afterAll(chdirCwd) | ||
|
||
test('returns a development config', () => { | ||
process.env.NODE_ENV = 'development' | ||
const { environment } = require('../index') | ||
expect(environment.toWebpackConfig()).toMatchObject({ | ||
devServer: { | ||
host: 'localhost', | ||
port: 3035 | ||
} | ||
}) | ||
}) | ||
|
||
test('with no NODE_ENV throws an error', () => { | ||
delete process.env.NODE_ENV | ||
expect(() => require('../index')).toThrow() | ||
}) | ||
|
||
test('returns a production config for non-standard env', () => { | ||
process.env.NODE_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,27 @@ | ||
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 config = safeLoad(readFileSync(configPath), 'utf8') | ||
const availableEnvironments = Object.keys(config).join('|') | ||
|
||
if (!nodeEnv) { | ||
throw new Error(` | ||
Using 'webpacker' requires that you specify 'NODE_ENV' environment variables. | ||
Valid values are ${availableEnvironments}. Instead, received: ${nodeEnv}`) | ||
} | ||
|
||
const regex = new RegExp(availableEnvironments, 'g') | ||
if (nodeEnv.match(regex)) return nodeEnv | ||
|
||
/* eslint no-console: 0 */ | ||
console.warn(`${nodeEnv} 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.