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 1 commit
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
35 changes: 23 additions & 12 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,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 +872,8 @@ util.parseFile = function(fullFilename) {
// Coffee = require("coffee-script");
//}

Coffee = require("coffee-script");
var dep = "coffee-script"
Coffee = require(dep);

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

@lorenwest lorenwest Nov 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javascript hoists variables, and having individual var dep gives the misleading impression that these are different things, when they are all the same variable.

A better pattern would be to define var dep = undefined; at the top of the function and use that variable without (misleadingly) re-defining it in each instance.

Even better would be to have a block at the top of the file like this

// Define soft dependencies so transpilers don't include everything
var ICED_COFFEE_SCRIPT = "iced-coffee-script";
var JS_YAML = "js-yaml"
...

Then simply use these variables instead of the hardcoded strings require(ICED_COFFEE_SCRIPT);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to that. I blindly implemented the isolated solution given for one variable in the issue thread. I'll push an update now.

Iced = require(dep);

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

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

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

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

configObject = JSON5.parse(content);

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

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

configObject = HJSON.parse(content);

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

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

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

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

var x2js = new XML();
Expand Down