stampit-org / react-stampit

A specialized stampit factory for React
131 stars 1 forks source link

Webpack #20

Closed troutowicz closed 9 years ago

troutowicz commented 9 years ago

Issue #18

The idea here is to provide distributables, packaged with webpack, for non Node projects. For Node projects, it is up to the developer to package project dependencies.

troutowicz commented 9 years ago

I'm kind of stuck on this one. When importing this with <script>, a global stampit object is returned. This object is the exports object, containing the default property (the main react-stampit function) and stamp (the decorator export). This is fine when importing the module in a node, but not when trying to use the library as a global.

The developer would need to call the library via stampit.default(React, {})...

I want to provide a library that works as a global just like it would as a regular node import. How can we do this?

BerkeleyTrue commented 9 years ago

I don't think it's possible with es6. You can hack it this way

Object.defineProperty(exports, '__esModule', {
  value: true
});

exports.ReactStampit = rStampit; // global object
exports['default'] = rStampit; // es6 default import
exports.stamp = stamp; // import { stamp } from 'react-stampit' and const { stamp } = window.ReactStampit
troutowicz commented 9 years ago

The way to do this, it turns out, is to change the lib structure around a bit.

|-- src
|   |-- index.js
|   |-- addons.js
|   |-- utils
|   |   |-- index.js
|   |   |-- cache.js
|   |   |-- decorator.js
import stampit from 'react-stampit';
import stamp from 'react-stampit/utils/decorator';

Then for the browser, we could make two builds,react-stampit.js and react-stampit-with-addons.js. For react-stampit-with-addons.js, src/addons.js would attach all utils to stampit.addons possibly. I'm pretty sure this will work, I'm basing it off of alt's structure.