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

Importing Meteor packages solutions? #3455

Closed
DylanLyon opened this issue Apr 20, 2018 · 6 comments
Closed

Importing Meteor packages solutions? #3455

DylanLyon opened this issue Apr 20, 2018 · 6 comments

Comments

@DylanLyon
Copy link

Hey all,

I know there has been some talk in the past about handling Meteor import statements into storybook (e.g. 'import Random from meteor/meteor') but I never saw any concrete ways to make it work. Did anyone ever get a method working that doesn't involve rewriting all their code? (rewriting isn't really an option for this project.)

Thanks everyone for your help!

@danielduan
Copy link
Member

I don't think anyone is looking into adding support. If you'd like to open a PR, we'll gladly take a look.

@tmeasday
Copy link
Member

Hi @DylanLyon, I guess this is covered by this ticket: #3329

I don't think explicitly supporting Meteor from within Storybook is the best way to solve it. Probably the best way forward in the shorter term is investigating things like https://github.com/ardatan/meteor-webpack and seeing if you can use that to make SB in it's current form work with Meteor (let us know if it works!)

@ksinas
Copy link

ksinas commented May 19, 2020

It's an old thread, but I run into the similar issue. So thought it might help someone. I usually try to make components without any Meteor logic and then wrap it in a separate file, so I could play around with storybook. In some cases there are still some dependences, but then I mock these dependences.

To do that, you need to setup webpack. This is how my .storybook/main.js file looks like:

const webpack = require('webpack');
const path = require('path');

module.exports = {
  stories: ['../stories/**/*.stories.jsx'],
  addons: ['@storybook/addon-actions', '@storybook/addon-knobs'],
  webpackFinal: async (config) => {
    config.plugins.push(new webpack.NormalModuleReplacementPlugin(
      /^meteor/,
      resource => {
        // Gets absolute path to mock `meteor` packages
        const absRootMockPath = path.resolve(
          __dirname,
          "../__mocks__/meteor",
        );

        // Gets relative path from requesting module to our mocked module
        const relativePath = path.relative(
          resource.context,
          absRootMockPath,
        );

        // Updates the `resource.request` to reference our mocked module instead of the real one
        resource.request = resource.request.replace(/meteor/, relativePath);
      },
    ));

    // Return the altered config
    return config;
  },
};

Then it's up to you to mock your dependences, which should be stored in __mocks__/meteor folder.

@tmeasday
Copy link
Member

@ksinas would you be interested in looking at making a Storybook preset to encode this setup?

@ksinas
Copy link

ksinas commented May 20, 2020

@tmeasday to make an npm package out of it doesn't make much sense to me. At the end it's user's responsibility to write mocks. And if you don't try to write your components as pure as possible, there might be a lot to mock.. And a package like this might cause a lot of misunderstanding and bug reports.

If you have a good idea how to circumvent it, I don't mind making a preset.

@tmeasday
Copy link
Member

I guess I just literally meant something to do the webpack config you posted above.

Having said that, I suspect a built-in mechanism in SB, maybe .storybook/mocks/<module-name> might be a good idea!

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

No branches or pull requests

4 participants