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

Windows 10 and Ubuntu output different code #226

Open marcog83 opened 5 years ago

marcog83 commented 5 years ago

I compiled my current project with the babel-plugin-rewire.

I get ReferenceError: _get__ is not defined when i try to run it on windows 10

On ubuntu and Mac it everything works, so I compared the final js file and I found that on Windows the plugin transpiles

!*** ../node_modules/core-js/modules/_has.js ***!
/***/ (function(module, exports) {

eval("var hasOwnProperty = {}.hasOwnProperty;\n\nmodule.exports = function (it, key) {\n  return _get__(\"hasOwnProperty\").call(it, key);\n};");

/***/ }),

and on Mac / Ububntu


 !*** ../node_modules/core-js/modules/_has.js ***!
/***/ (function(module, exports) {

eval("var hasOwnProperty = {}.hasOwnProperty;\nmodule.exports = function (it, key) {\n  return hasOwnProperty.call(it, key);\n};\n");

/***/ }),

As you can see in the first case I have _get__(\"hasOwnProperty\") and in the second hasOwnProperty

my package.json

    "@babel/core": "7.4.3"
    "@babel/preset-env": "7.4.3",
    "@babel/preset-flow": "7.0.0",
    "@babel/preset-react": "7.0.0",
    "@babel/plugin-proposal-class-properties": "7.4.0",
    "@babel/plugin-proposal-object-rest-spread": "7.4.3",
    "@babel/plugin-syntax-dynamic-import": "7.2.0",
    "babel-plugin-angularjs-annotate": "0.10.0",  
    "babel-plugin-rewire": "1.2.0",

my babel config looks like the follow

{
presets: [
        '@babel/flow',
        '@babel/react',
        [
          '@babel/env',
          {
           useBuiltIns: 'entry',
    corejs: 2,
    modules: false,
    loose: true,
    shippedProposals: true,
    exclude: [
      'es6.math.acosh',
      'es6.math.asinh',
      'es6.math.atanh',
      'es6.math.cbrt',
      'es6.math.clz32',
      'es6.math.cosh',
      'es6.math.expm1',
      'es6.math.fround',
      'es6.math.hypot',
      'es6.math.imul',
      'es6.math.log1p',
      'es6.math.log10',
      'es6.math.log2',
      'es6.math.sign',
      'es6.math.sinh',
      'es6.math.tanh',
      'es6.math.trunc'
    ],
            targets: {
              browsers: ['chrome >= 60'],
              node: true
            }
          }
        ]
      ]
plugins:[
    '@babel/proposal-class-properties',
    '@babel/proposal-object-rest-spread',
    [
      'angularjs-annotate',
      {
        explicitOnly: true
      }
    ],
    '@babel/syntax-dynamic-import',
   'rewire'
  ]
}
lifeart commented 4 years ago

['babel-plugin-rewire', {ignoredIdentifiers: ['hasOwnProperty']}] should fix you issue

juanger commented 1 year ago

In my case, the problem was an exclude config passed from my webpack configuration for the babel loader:

{
    test: /\.[jt]sx?$/,
    exclude: /node_modules\/(?!ABC\/).*/,     // <- \/ is not enough, something like [\/\\] can work
}

Since those expressions are applied directly to the path, you need to make the expression match both, or you can get the path separator programatically.