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

Build LABjs string from source #846

Merged
merged 5 commits into from
Jan 31, 2017
Merged

Build LABjs string from source #846

merged 5 commits into from
Jan 31, 2017

Conversation

gigabo
Copy link
Contributor

@gigabo gigabo commented Jan 21, 2017

I'm going to start making substantive changes to the LABjs code we put at the top of every page. I want to have useful diffs and commit history for those changes, so as a baseline this PR replaces the pre-minified source string with the (mostly) vanilla source file.

A few benefits:

  • Can opt-in to viewing debug output with DEBUG_LAB=true react-server start
  • It's 210 bytes smaller (I'm going to make it smaller still in follow up PRs)
  • It's human-readable (and hackable)

A few drawbacks:

  • A dep on uglify
  • A few hundred millisecond startup penalty

Could minify during build to avoid those, but we'd lose the debug-ability which is really nice.

@gigabo gigabo added the enhancement New functionality. label Jan 21, 2017
@gigabo
Copy link
Contributor Author

gigabo commented Jan 21, 2017

I guess we could pre-build two copies (normal and debug) and select among them with an option... 🤔

That would actually let us turn on debugging at run-time which could be useful.

@gigabo
Copy link
Contributor Author

gigabo commented Jan 21, 2017

Heh. 👹

image

@doug-wade
Copy link
Collaborator

doug-wade commented Jan 21, 2017

🤔 iiuc, we're just checking in the source of lab.js to react server core, but is that really where it belongs? Maybe it makes sense to make it a separate npm package? Maybe it even makes sense to make it into an npm package that lives outside of the react server repo, maybe something like https://npmjs.com/package/labjs? It's under and MIT license, and it doesn't really seem like its something react server specific...

We could also add a git dependency to react-server/package.json

@doug-wade
Copy link
Collaborator

Actually, looks like there's plans to put it on npm

@gigabo
Copy link
Contributor Author

gigabo commented Jan 21, 2017

Oh, good thought. I'm going to make some modifications for its use in React Server, so if anything it would probably have to be react-server-labjs or something like that. But really, I'm not sure that it would be useful externally? It's basically part of the React Server runtime in the browser.

@doug-wade
Copy link
Collaborator

doug-wade commented Jan 21, 2017

Makes sense. I vote we pry it out into react-server-labjs (which also means you can postpone fixing the linting errors if you'd like 👹) and hope that we can take a dependency on the npm package when it comes out.

@gigabo
Copy link
Contributor Author

gigabo commented Jan 21, 2017

hope that we can take a dependency on the npm package when it comes out.

We already can't. We would need getify/LABjs#81 to land for non-debug.

@gigabo
Copy link
Contributor Author

gigabo commented Jan 21, 2017

But anyway once this (or whatever this turns into) lands I've got another PR coming on its heels that starts really hacking it up. 😈

const path = require("path");
const {readFileSync} = require("fs");

var lab = readFileSync(path.join(__dirname, "LAB.src.js"), "utf-8");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't this mean that this module can't be used on the client 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.

Yep, this module is only used in the render middleware. Its output is a string of JS that is put into an inline script tag.

lab = lab.replace(/\/\*!START_DEBUG(?:.|[\n\r])*?END_DEBUG\*\//g, "");

// After the debug code is removed just uglify.
lab = require("uglify-js").minify(lab, {fromString: true}).code;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already run this whole package through Babel with Gulp, why not uglify it there and save the uglified version in target/(client|server)/util/LABString.js? What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What am I missing?

The full extent of my laziness. 👹

As I think about it more I like @doug-wade's idea of pulling this out into a separate package. Going to manage it inside the React Server repo for now just for convenience. We'll be able to pull it out later using lerna export if we want to.

The debug code gets totally removed in the minified build.
@gigabo
Copy link
Contributor Author

gigabo commented Jan 27, 2017

I've updated this PR to put LAB into a dedicated package and move the minification to build time. 🍻

@gigabo
Copy link
Contributor Author

gigabo commented Jan 27, 2017

Hard to believe flab wasn't taken. 😈

@@ -0,0 +1,9 @@
console.log(
'module.exports = '+
require("fs").readFileSync("/dev/stdin", "utf-8")
Copy link
Member

Choose a reason for hiding this comment

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

I feel like I should know this, but... do we officially support windows? Seems like /dev/stdin wouldn't work there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh... this is only at build time. Do we need to support windows even for that? 😩

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ignore the mockery stuff. The memory-stream package allows you to pass a stream object to a library and then call .toString() on what was written to the stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh cool thanks @drewpc.

I'm tempted to use get-stdin, which is a bit more targeted to what this little one-liner needs to do.

@gigabo
Copy link
Contributor Author

gigabo commented Jan 27, 2017

Okay, got rid of the use of /dev/stdin. 👍

@drewpc drewpc modified the milestone: 0.6.1 Jan 27, 2017
@drewpc
Copy link
Collaborator

drewpc commented Jan 27, 2017

While you're involved in this, it probably is a good idea to look into getify/LABjs#113. It might address #77 and #253 without much work from this end. Whoops... @doug-wade was on it and already mentioned this!

@gigabo
Copy link
Contributor Author

gigabo commented Jan 27, 2017

Oh, nice. Thanks for the link.


This is based on the awesome [LABjs](https://github.com/getify/LABjs) package.

TODO: Flesh out this README. :grin:
Copy link
Collaborator

Choose a reason for hiding this comment

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

😆

@@ -0,0 +1,39 @@
{
"name": "flab",
Copy link
Collaborator

Choose a reason for hiding this comment

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

😆

"loader"
],
"author": "Bo Borgerson",
"license": "MIT",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Apache 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LABjs is MIT. Can MIT be converted to Apache 2?

"bugs": {
"url": "https://github.com/redfin/react-server/issues"
},
"homepage": "https://github.com/redfin/react-server#readme",
Copy link
Collaborator

Choose a reason for hiding this comment

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

out-of-scope: should we update all of the modules to point to react-server.io as their homepage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, maybe... 🤔

Copy link
Collaborator

@doug-wade doug-wade left a comment

Choose a reason for hiding this comment

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

I dig it. Thanks @gigabo 🎉

@gigabo
Copy link
Contributor Author

gigabo commented Jan 31, 2017

Onward! 👉

@gigabo gigabo merged commit 77248a3 into redfin:master Jan 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants