elidoran / cosmos-browserify

Browserify npm modules for client side in Meteor packages
MIT License
78 stars 12 forks source link

using browserify in meteor package to import react-router #32

Closed almeynman closed 8 years ago

almeynman commented 8 years ago

Hello requiring Npm.depends only works if I use npm package on the server side, however I would like to use react-router package inside my meteor package. Is it posssible to use browserify in meteor package?

almeynman commented 8 years ago

Ups sorry just noticed that it is included in README

almeynman commented 8 years ago

Although I am struggling to import react-router into my meteor local package. Receing following error when builing meteor project:

Cannot find module 'react' from '/Users/almas/Projects/despite/packages/app-web/.npm/package/node_modules/react-router/lib'

Also in my package I now have .npm folder, which was not created when I was using browserify not in meteor package but just in the project...

My package.js package looks as follows:

...
Npm.depends({
  'react-router': '1.0.0-rc3',
  'externalify': '0.1.0',
})

Package.onUse(function(api) {
  api.versionsFrom('1.2.1');

  api.use([
    'ecmascript',
    'app-base',
  ]);
  api.use([
    'react',
    'cosmos:browserify',
  ], ['client']);

  api.addFiles([
    'app.browserify.js',
    'app.browserify.options.js',
    'src/client/routes.jsx',
    'src/client/components/App.jsx',
  ], ['client']);

  api.export([
    'ReactRouter',
  ])
});

app.browserify.js looks as follows:

ReactRouter = require('react-router');

and app.browserify.options.js:

{
  "transforms": {
    "externalify": {
      "global": true,
      "external": {
        "react": "React.require"
      }
    }
  }
}
elidoran commented 8 years ago

Setting specific versions of the packages for api.use() is always a good idea.

The error says it can't find the react module in the package's .npm directory. Make that available by adding that module to the Npm.depends() call.

almeynman commented 8 years ago

@elidoran Thanks for the tip. If version is not specified browserify will try to install the latest module right?

I do not want to install react from npm as there is built in support for react from meteor v1.2, Meteor documentation shows how to do it in meteor app but not in a meteor package https://react-in-meteor.readthedocs.org/en/latest/client-npm/

elidoran commented 8 years ago

I mean the versions of the meteor packages in package.js api.use() call (react and cosmos:browserify). For those, the "no version means latest" is a common misconception about how Meteor works. When the version isn't specified it will use the oldest version considered to be compatible with the other packages in use.

The document you referenced has a section near the bottom about using a library from npm and the meteor react package in your own package. The options file they give:

{
  "transforms": {
    "externalify": {
      "global": true,
      "external": {
        "react": "Package.react-runtime.React.require"
      }
    }
  }
}
almeynman commented 8 years ago

Thanks for spotting this out and sorry for being blind. I also had package.browserify.options.js file instead of package.browserify.options.json... :-(

Thank you for help and keep up the good work! Have a great day