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

Fix exposing of .env variables in Create React App #560

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@ npm-debug.log
yarn-error.log
/dist/
/coverage/
.nyc_output/

# lockfiles
yarn.lock
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -14,7 +14,7 @@ module.exports = function (config) {
// Test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
reporters: ['mocha'],
Copy link
Author

Choose a reason for hiding this comment

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

So we can see that browser env was selected.


// Web server port
port: 9876,
Expand Down
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -25,9 +25,12 @@
"license": "MIT",
"scripts": {
"lint": "xo",
"test": "npm run test:node && npm run test:browser",
"test:node": "istanbul cover _mocha -- test.js",
"pretest": "rm -rf .nyc_output",
"test": "npm --env=node run test:node && npm --env=electron run test:electron && npm run test:browser",
"test:browser": "karma start --single-run",
"test:node": "nyc --no-clean mocha --exit",
"test:electron": "nyc --no-clean mocha --exit",
"posttest": "nyc report --reporter lcov",
"test:coverage": "cat ./coverage/lcov.info | coveralls"
},
"dependencies": {
Expand All @@ -37,13 +40,14 @@
"brfs": "^2.0.1",
"browserify": "^16.2.3",
"coveralls": "^3.0.2",
"istanbul": "^0.4.5",
"karma": "^3.1.4",
"karma-browserify": "^6.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.2.0",
"nyc": "^13.1.0",
"xo": "^0.23.0"
},
"main": "./src/index.js",
Expand Down
49 changes: 27 additions & 22 deletions src/browser.js
Expand Up @@ -10,6 +10,14 @@ exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();

/**
* Are we in Electron?
* @returns {boolean}
*/
function isElectron() {
return typeof process !== 'undefined' && (process.type === 'renderer' || process.__nwjs);
}

/**
* Colors.
*/
Expand Down Expand Up @@ -106,7 +114,7 @@ function useColors() {
// NB: In an Electron preload script, document will be defined but not fully
// initialized. Since we know we're in Chrome, we'll just detect this case
// explicitly
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
if (isElectron()) {
return true;
}

Expand Down Expand Up @@ -204,20 +212,12 @@ function save(namespaces) {
* @api private
*/
function load() {
let r;
try {
r = exports.storage.getItem('debug');
return exports.storage.getItem('debug');
} catch (error) {
// Swallow
// XXX (@Qix-) should we be logging these?
}

// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if (!r && typeof process !== 'undefined' && 'env' in process) {
Copy link
Author

Choose a reason for hiding this comment

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

This is the critical part that was exposing process.env in the browser. It's been pulled out into the electron-specific flow.

r = process.env.DEBUG;
}

return r;
}

/**
Expand All @@ -242,18 +242,23 @@ function localstorage() {
}
}

module.exports = require('./common')(exports);
if (isElectron()) {
module.exports = exports;
module.exports.humanize = require('ms');
} else {
module.exports = require('./common')(exports);

const {formatters} = module.exports;
const {formatters} = module.exports;

/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/

formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};
formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};
}
28 changes: 28 additions & 0 deletions src/electron.js
@@ -0,0 +1,28 @@
exports = require('./browser');

const browserLoad = exports.load;

// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
exports.load = function () {
let r = browserLoad();
if (!r && 'env' in process) {
r = process.env.DEBUG;
}
return r;
};

module.exports = require('./common')(exports);

const {formatters} = module.exports;

/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/

formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};
6 changes: 5 additions & 1 deletion src/index.js
Expand Up @@ -4,7 +4,11 @@
*/

if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
module.exports = require('./browser.js');
if (process.browser === true) {
module.exports = require('./browser.js');
} else {
module.exports = require('./electron.js');
}
} else {
module.exports = require('./node.js');
}
37 changes: 35 additions & 2 deletions test.js
@@ -1,9 +1,24 @@
/* eslint-env mocha */

/** pre-load conditions */
let env = 'browser';
if (process.env.npm_config_env === 'electron') {
// Force Electron mode
process.type = 'renderer';
env = 'Electron';
} else if (process.env.npm_config_env === 'node') {
env = 'node';
}
env = ' (' + env + ')';
process.env.DEBUG_YES = 'yes';
process.env.DEBUG_NO = 'no';
process.env.DEBUG_NULL = 'null';
process.env.DEBUG_NUM = 111;

const assert = require('assert');
const debug = require('./src');

describe('debug', () => {
describe('debug' + env, () => {
it('passes a basic sanity check', () => {
const log = debug('test');
log.enabled = true;
Expand All @@ -30,6 +45,7 @@ describe('debug', () => {
});

it('uses custom log function', () => {
debug.useColors = () => false;
const log = debug('test');
log.enabled = true;

Expand All @@ -39,8 +55,13 @@ describe('debug', () => {
log('using custom log function');
log('using custom log function again');
log('%O', 12345);
log('%o', 12345);
log('%j', {abc: 12345});
assert.deepStrictEqual(messages.length, 5);

assert.deepStrictEqual(messages.length, 3);
const numInstance = debug.instances.length;
assert(log.destroy());
assert.strictEqual(debug.instances.length, numInstance - 1);
});

describe('extend namespace', () => {
Expand Down Expand Up @@ -118,4 +139,16 @@ describe('debug', () => {
assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String));
});
});

if (env === 'node') {
describe('inspectOpts', () => {
it('handles env vars', () => {
const d = debug.inspectOpts;
assert.strictEqual(d.no, false);
assert.strictEqual(d.null, null);
assert.strictEqual(d.num, 111);
assert.strictEqual(d.yes, true);
});
});
}
});