speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Rewire only in a test environment #210

Open BenjaminVanRyseghem opened 6 years ago

BenjaminVanRyseghem commented 6 years ago

Rewire conflict with babel-plugin-wildcard.

As a workaround (which could benefits others), I make rewire run only in a test environment.

speedskater commented 6 years ago

@BenjaminVanRyseghem sorry for the late reply. I have just had a look at your PR.Thank you for your work in providing this feature to babel-plugin-rewire. If I haven't missunderstood your intention, I think the same behaviour can be achieved by specifying babel-plugin-rewire only in the desired babel environment in the babelrc file. I think this leaves the decision when to apply the plugin to the user. Is this okay for you?

BenjaminVanRyseghem commented 6 years ago

Thanks for your answer.

I'm kinda new to babel, and didn't find how to specify environment in babelrc. Do you have some documentation for me?

speedskater commented 6 years ago

@BenjaminVanRyseghem this is a sample of one of the projects I am involved. Please keep in mind that this is not the complete configuration and it is not checked against babel 7. For further information I would recommend the babel documentation itself: https://babeljs.io/docs/usage/babelrc/

In this example we have general plugins and presets which are applicable to all environments. Further we have a specific configuration for our karma tests which adds istanbul for code coverage and the babel-plugin-rewire.

{
    "plugins": [
        "transform-runtime",
        "syntax-flow",
        "transform-flow-strip-types"
    ],
    "presets": [
        "es2015",
        "react"
    ],
    "babelrc": false,
    "env": {
        "karma": {
            "plugins": [
                ["istanbul", {
                    "exclude": [
                        "tests/**/*"
                    ]
                }], "rewire"
            ]
        }
    }
}

Hope this is able to clarify your issue?

BenjaminVanRyseghem commented 6 years ago

Thanks! I'll have a look :smile:

I found this stating it's deprecated, but I'll give it a try asap :smile:

BenjaminVanRyseghem commented 6 years ago

sorry for the delay :smile:

It works, thank you. But as it seems deprecated, it looks like this will be needed anytime soon. What do you think?

tfrommen commented 6 years ago

As Babel 7 does/will not use env any longer, this PR is actually still interesting. I'm with @BenjaminVanRyseghem here, for what it's worth. 🙂

BenjaminVanRyseghem commented 5 years ago

ping?

josh08h commented 5 years ago

I'd be interested in getting this PR merged. We are on babel 7 and would like to only have this plugin run against spec files.

theKashey commented 5 years ago

Disabling ‘rewire’ in not-test environments could be a bad thing, as long using rewire in not-test(And not production) environment could be a good.

Sinewyk commented 5 years ago

I think this PR is still not needed, ok env will probably be deprecated. But nothing is stopping you from using a .babelrc.js instead of a .babelrc, and once you have a js file you can do whatever you want.

KosnIre commented 2 years ago

It looks like the babel team eventually decided they would continue to support env-specific configs in babelrc, so I think this issue can be closed.

https://github.com/babel/babel/issues/5276#issuecomment-348598859

https://babeljs.io/docs/en/configuration#how-babel-merges-config-items

I'm on babel 7 and this config is working for me:

{
  "env": {
    "test": {
      "plugins": [
        "rewire"
      ]
    }
  }
}