Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play nice with transpilers by using variable requires (fixes #345) #358

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 24 additions & 12 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ var DEFAULT_CLONE_DEPTH = 20,
configSources = [], // Configuration sources - array of {name, original, parsed}
checkMutability = true; // Check for mutability/immutability on first get

// Define soft dependencies so transpilers don't include everything
var COFFEE_DEP = "coffee-script",
ICED_DEP = "iced-coffee-script",
JS_YAML_DEP = "js-yaml",
YAML_DEP = "yaml",
JSON5_DEP = "json5",
HJSON_DEP = "hjson",
TOML_DEP = "toml",
CSON_DEP = "cson",
PPARSER_DEP = "properties",
XML_DEP = "x2js";

/**
* <p>Application Configurations</p>
*
Expand Down Expand Up @@ -537,7 +549,7 @@ util.makeImmutable = function(object, property, value) {
for (var i = 0; i < properties.length; i++) {
var propertyName = properties[i],
value = object[propertyName];

if (!(value instanceof RawConfig)) {
Object.defineProperty(object, propertyName, {
value: value,
Expand Down Expand Up @@ -872,7 +884,7 @@ util.parseFile = function(fullFilename) {
// Coffee = require("coffee-script");
//}

Coffee = require("coffee-script");
Coffee = require(COFFEE_DEP);

// coffee-script >= 1.7.0 requires explicit registration for require() to work
if (Coffee.register) {
Expand All @@ -883,7 +895,7 @@ util.parseFile = function(fullFilename) {
configObject = require(fullFilename);
}
else if (extension === 'iced') {
Iced = require("iced-coffee-script");
Iced = require(ICED_DEP);

// coffee-script >= 1.7.0 requires explicit registration for require() to work
if (Iced.register) {
Expand Down Expand Up @@ -946,12 +958,12 @@ util.parseString = function (content, format) {
// Lazy loading
try {
// Try to load the better js-yaml module
Yaml = require('js-yaml');
Yaml = require(JS_YAML);
}
catch (e) {
try {
// If it doesn't exist, load the fallback visionmedia yaml module.
VisionmediaYaml = require('yaml');
VisionmediaYaml = require(YAML);
}
catch (e) { }
}
Expand Down Expand Up @@ -982,7 +994,7 @@ util.parseString = function (content, format) {
}

if (!JSON5) {
JSON5 = require('json5');
JSON5 = require(JSON5_DEP);
}

configObject = JSON5.parse(content);
Expand All @@ -991,30 +1003,30 @@ util.parseString = function (content, format) {
else if (format === 'json5') {

if (!JSON5) {
JSON5 = require('json5');
JSON5 = require(JSON5_DEP);
}

configObject = JSON5.parse(content);

} else if (format === 'hjson') {

if (!HJSON) {
HJSON = require('hjson');
HJSON = require(HJSON_DEP);
}

configObject = HJSON.parse(content);

} else if (format === 'toml') {

if(!TOML) {
TOML = require('toml');
TOML = require(TOML_DEP);
}

configObject = TOML.parse(content);
}
else if (format === 'cson') {
if (!CSON) {
CSON = require('cson');
CSON = require(CSON_DEP);
}
// Allow comments in CSON files
if (typeof CSON.parseSync === 'function') {
Expand All @@ -1025,13 +1037,13 @@ util.parseString = function (content, format) {
}
else if (format === 'properties') {
if (!PPARSER) {
PPARSER = require('properties');
PPARSER = require(PPARSER_DEP);
}
configObject = PPARSER.parse(content, { namespaces: true, variables: true, sections: true });
} else if (format === 'xml') {

if (!XML) {
XML = require('x2js');
XML = require(XML_DEP);
}

var x2js = new XML();
Expand Down