Skip to content

Instantly share code, notes, and snippets.

@chenglou
Last active August 29, 2015 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenglou/8701065 to your computer and use it in GitHub Desktop.
Save chenglou/8701065 to your computer and use it in GitHub Desktop.
CommonJS-style Asset Bundling

The rough underlying implementation is more or less done. Right now we're deciding betwen two forms, require('a.png') vs. requireStatic('a.png').

require('a.png')

  • Pros: more familiar. One single require for assets too.
  • Cons: Doesn't follow the CommonJS specs. Overloads require.

requireStatic('a.png')

  • Pros: isn't tied to CommonJS.
  • Cons: another thing to learn.
@syranide
Copy link

syranide commented Feb 5, 2014

My feeling about this is that is require() is sweeeeet, requireStatic() is explicit and doesn't break anything.

I want to say require(), but I think the right choice is to use requireStatic() simply because this it "scales", it doesn't prevent anyone from implementing requireBlaBla() and still keeping it in the style, would anyone actually find that useful? I don't know. I often end up implementing intuitive (but magic) behavior, but when I have to choose I'm always drawn to clean descriptive and unambigiuous APIs.

For that reason I would personally vote for requireStatic() (even though I love the idea of require()).

@cpojer
Copy link

cpojer commented Feb 5, 2014

I agree with @syranide, this should definitely not use require.

@sophiebits
Copy link

What does it return – the file contents? a URL? If the latter, I definitely think require is wrong; otherwise I don't have a strong opinion.

@chenglou
Copy link
Author

chenglou commented Feb 5, 2014

Requiring an image returns the image path. Requiring a css returns an object that maps from the class name to a (possibly) namespaced class name.
<div className={a['.class']} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment