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

don't load css in node environment #2492

Merged
merged 1 commit into from Mar 17, 2017
Merged

Conversation

kellyselden
Copy link
Contributor

Now that #2484 has landed, we can begin to iterate on the node experience and make it better. This removes the need to mock require to ignore css requiring in node (which is a bundler-only feature).

/**
* Check if running in a browser.
*/
function isBrowser () {
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be a function? I'd do maybe:

module.exports.isBrowserEnvironment = !process;

module.exports.isNodeEnvironment = !!process;

Copy link
Member

Choose a reason for hiding this comment

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

We can add a unit test for these in Karma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was matching the convention in the file. isIOS () and isGearVR () are functions when I don't think they need to be either.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I guess they don't need to be. Maybe later we can make those properties.

Copy link
Member

Choose a reason for hiding this comment

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

To clear up, I mean I think we should make isBrowserEnvironment + isNodeEnvironment properties now, and make isIOS + isGearVR properties later too.

/**
* Check if running in node on the server.
*/
module.exports.isNode = function () {
Copy link
Member

Choose a reason for hiding this comment

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

We can also add a unit test for these in our Node tests

Copy link
Member

@ngokevin ngokevin left a comment

Choose a reason for hiding this comment

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

.

@dmarcos
Copy link
Member

dmarcos commented Mar 15, 2017

I guess the advantage of having them as function is that the logic to determine if the environment isGearVR, isOS, isNode does not run unless is required.

@ngokevin
Copy link
Member

These are always run though since we call them from index.js and a-scene.js. The disadvantage is that we are calling them multiple times.

@kellyselden kellyselden force-pushed the node-css branch 2 times, most recently from 36c2589 to c124243 Compare March 16, 2017 05:31
@kellyselden
Copy link
Contributor Author

kellyselden commented Mar 16, 2017

I changed to value exports instead of functions.

When adding tests. I found out that not only is process mocked by browserify/karma, all node globals are mocked (found by debugging chrome while karma was running). This meant I had to get a little more explicit with environment detection.

/**
* Check if running in a browser.
*/
// we need to check a node api that isn't mocked on either side
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I did the commenting right. I was trying to separate the public comments that go in the api docs, from private implementation comments.

Copy link
Member

Choose a reason for hiding this comment

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

I think you can just use normal comments throughout for these all properties. We don't actually generate docs from this, it's mostly all developer comments, but in JSDoc form. (with preference to capitalized / ending in period comments)

/**
* Check if running in a browser or spoofed browser.
*/
var isBrowserLikeEnvironment = isBrowserEnvironment || isBundlerEnvironment;
Copy link
Member

Choose a reason for hiding this comment

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

Can we narrow it to two?

  • isBrowserEnvironment - true if the environment is even browser-like.
  • isNodeEnvironment - Not browser-like.

And then we can export straight up in two lines:

module.exports.isBrowserEnvironment = !process || !!(process && process.browser);

module.exports.isNodeEnvironment = !!module.exports.isBrowserEnvironment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

const Module = require('module');
const sinon = require('sinon');

const _require = Module.prototype.require;

describe('node acceptance tests', function () {
Copy link
Member

Choose a reason for hiding this comment

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

can we run mocha in tdd mode to match the other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in #2496 and will rebase.


suite('environment', function () {
test('isNodeEnvironment is false for browser tests', function () {
assert.strictEqual(device.isNodeEnvironment, false);
Copy link
Member

Choose a reason for hiding this comment

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

could use assert.ok or assert.notOk

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to test the correct type as well. I don't want to accidently return a truthy/falsy object (which I almost did: process && process.browser was returning undefined instead of false).

Copy link
Member

Choose a reason for hiding this comment

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

OK

@ngokevin
Copy link
Member

Can rebase this now

@kellyselden
Copy link
Contributor Author

rebased and updated.

@ngokevin ngokevin merged commit cafbadc into aframevr:master Mar 17, 2017
@ngokevin
Copy link
Member

Thanks! Love the test code deletions.

@kellyselden kellyselden deleted the node-css branch March 17, 2017 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants