babel-plugin-rewire
babel-plugin-rewire copied to clipboard
Cannot redefine property: __Rewire__ after build
I m using this library to make rewire inside of a project and I m having some troubles while requiring the due module in another one.
My module is the following : https://github.com/Skahrz/express-konnector
I try to use the express-konnector (locally) from that little script called index.js :
/* ... */
const expressKonnector = require('../express-konnector')
expressKonnector(app, injector, routeParser)
/*...*/
And when I try to run the script using :
node index.js
I have the following error that is thrown :
C:\Project\javascript\express-konnector\dist\app.js:1
(function (exports, require, module, __filename, __dirname) { "use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}function __GetDependency__(e){return __$Getters__[e]()}function __Rewire__(e,_){__$Setters__[e](_)}function __ResetDependency__(e){__$Resetters__[e]()}Object.defineProperty(exports,"__esModule",{value:!0});var _appExpressKonnector=require("./app/expressKonnector"),_appExpressKonnector2=_interopRequireDefault(_appExpressKonnector),__$Getters__=[],__$Setters__=[],__$Resetters__=[],__RewireAPI__={__GetDependency__:__GetDependency__,__get__:__GetDependency__,__Rewire__:__Rewire__,__set__:__Rewire__,__ResetDependency__:__ResetDependency__},expressKonnector=_appExpressKonnector2["default"];__$Getters__.expressKonnector=function(){return expressKonnector},__$Setters__.expressKonnector=function(e){expressKonnector=e},__$Resetters__.expressKonnector=function(){expressKonnector=_appExpressKonnector2["default"]};var _d
TypeError: Cannot redefine property: __Rewire__
at Function.defineProperty (native)
at Object.<anonymous> (C:\Project\javascript\express-konnector\dist\app.js:1:1077)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Project\javascript\test\t.js:6:26)
at Module._compile (module.js:409:26)
I m using (for building the lib) :
- node v4.4.3
- "babel-core": "^5.5.0",
- "babel-plugin-rewire": "^0.1.22",
- "babel-preset-es2015": "^6.5.0",
my .babel.rc :
{
"plugins":[
"babel-plugin-rewire"
]
}
Any idea of how to correct it ?
@Skahrz thanks for reporting the issue. I will have a look at the problem on monday. Would it be possible tfor you o create a PR with a failing sample in the mean time?
@Skahrz sorry for the long delay, in tackling your issue. Do you have more details on how to reproduce it (or is it already fixed in 1.0.0 ?)
Not sure if it's related but I'm getting Uncaught TypeError: Cannot redefine property: __get__
with Karma and Webpack.
but only when Karma points to a single file which requires all other test files:
const tests = require.context('.', true, /\.test\.js$/); tests.keys().forEach(tests);
If Karma is setup to find and process all test files (without the require.context) it works.
I managed to make a minimal reproducible example, see https://github.com/eventlistener/babel-plugin-rewire-issue-144/pull/1.
It's a subtle issue where the following conditions need to be met in order to reproduce it:
- The entry point should (directly or indirectly) import a module that contains at least two re-exports in the form
export * from ...
- At least two of the re-exported modules should be considered rewireable by the plugin (e.g. their exported function(s) should reference a variable from outer scope)
The re-exporting module can be located anywhere, including node_modules
. Whether or not any methods provided by the plugin are used at all doesn't matter either.
any update on this issue? I'm quite stuck right now, and I don't really know how to workaround this issue 😕
is there a way to prevent this plugin to apply to a dependency?
In my case, I know which dependency is triggering the issue, and I don't need to apply rewire to it. Is there a way, either as a plugin argument, or directly into babel to prevent rewire to apply on this specific dep?
to answer my own question, I ended up having a babel.config.js
looking like:
module.exports = {
env: {},
overrides: [
{
include: (filename) => filename.indexOf("/node_modules/") !== -1,
env: {
test: {
plugins: ["@babel/plugin-transform-modules-commonjs"],
},
},
},
{
exclude: (filename) => filename.indexOf("/node_modules/") !== -1,
env: {
test: {
plugins: [
"rewire",
"@babel/plugin-transform-modules-commonjs",
],
},
},
},
],
};
Hope it helps