Skip to content

Commit

Permalink
Merge pull request #203 from jpap/settheme-webpack-warning
Browse files Browse the repository at this point in the history
Remove setTheme dynamic require(...) that is problematic with webpack
  • Loading branch information
DABH committed Feb 16, 2018
2 parents 1d7180a + 0da00b2 commit 5c84a86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
6 changes: 5 additions & 1 deletion examples/normal-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ console.log("this is an input".input);
console.log('Generic logging theme as file'.green.bold.underline);

// Load a theme from file
colors.setTheme(__dirname + '/../themes/generic-logging.js');
try {
colors.setTheme(require(__dirname + '/../themes/generic-logging.js'));
} catch (err) {
console.log(err);
}

// outputs red text
console.log("this is an error".error);
Expand Down
24 changes: 8 additions & 16 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ function applyStyle() {
return str;
}

function applyTheme (theme) {
colors.setTheme = function (theme) {
if (typeof theme === 'string') {
console.log('colors.setTheme now only accepts an object, not a string. ' +
'If you are trying to set a theme from a file, it is now your (the caller\'s) responsibility to require the file. ' +
'The old syntax looked like colors.setTheme(__dirname + \'/../themes/generic-logging.js\'); ' +
'The new syntax looks like colors.setTheme(require(__dirname + \'/../themes/generic-logging.js\'));');
return;
}
for (var style in theme) {
(function(style){
colors[style] = function(str){
Expand All @@ -132,21 +139,6 @@ function applyTheme (theme) {
}
}

colors.setTheme = function (theme) {
if (typeof theme === 'string') {
try {
colors.themes[theme] = require(theme);
applyTheme(colors.themes[theme]);
return colors.themes[theme];
} catch (err) {
console.log(err);
return err;
}
} else {
applyTheme(theme);
}
};

function init() {
var ret = {};
Object.keys(styles).forEach(function (name) {
Expand Down

0 comments on commit 5c84a86

Please sign in to comment.