babel-plugin-rewire icon indicating copy to clipboard operation
babel-plugin-rewire copied to clipboard

Rewire only in a test environment

Open BenjaminVanRyseghem opened this issue 7 years ago • 11 comments

Rewire conflict with babel-plugin-wildcard.

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

BenjaminVanRyseghem avatar Feb 28 '18 08:02 BenjaminVanRyseghem

@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?

speedskater avatar Mar 15 '18 21:03 speedskater

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?

BenjaminVanRyseghem avatar Mar 15 '18 22:03 BenjaminVanRyseghem

@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?

speedskater avatar Mar 15 '18 22:03 speedskater

Thanks! I'll have a look :smile:

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

BenjaminVanRyseghem avatar Mar 15 '18 22:03 BenjaminVanRyseghem

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?

BenjaminVanRyseghem avatar Mar 27 '18 08:03 BenjaminVanRyseghem

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. 🙂

tfrommen avatar Jun 06 '18 17:06 tfrommen

ping?

BenjaminVanRyseghem avatar Oct 09 '18 14:10 BenjaminVanRyseghem

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.

josh08h avatar Oct 28 '18 16:10 josh08h

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.

theKashey avatar Nov 04 '18 09:11 theKashey

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.

Sinewyk avatar May 20 '19 16:05 Sinewyk

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"
      ]
    }
  }
}

KosnIre avatar Jan 26 '22 17:01 KosnIre