Giter Site home page Giter Site logo

Comments (12)

avesus avatar avesus commented on May 22, 2024 3

See babel/babel#3083 for details.

from babel-plugin-transform-builtin-extend.

stevenvachon avatar stevenvachon commented on May 22, 2024 2
export default class ValidationError extends Error {
  constructor(invalidations) {
    super('Invalid input');
    this.invalidations = invalidations;
    this.name = 'ValidationError';
  }
}
try {
  throw new ValidationError(['message']);
} catch(error) {
  if (error instanceof ValidationError) {
    console.log('ok');
  } else {
    throw error;
  }
}

You know, I think my current project is not importing this library (due to facebook/create-react-app#2952) Even with it added locally to babel-preset-react-app, it doesn't log "ok".

from babel-plugin-transform-builtin-extend.

loganfsmyth avatar loganfsmyth commented on May 22, 2024

Got an example of your configuration? It should work.

from babel-plugin-transform-builtin-extend.

loganfsmyth avatar loganfsmyth commented on May 22, 2024

That example prints ok in my test. I'll need a reproducible repository ideally.

from babel-plugin-transform-builtin-extend.

panganibanpj avatar panganibanpj commented on May 22, 2024

It works fine for me in Node, but not in browser

from babel-plugin-transform-builtin-extend.

loganfsmyth avatar loganfsmyth commented on May 22, 2024

@panganibanpj I need example code and info about your config, like I asked above.

from babel-plugin-transform-builtin-extend.

panganibanpj avatar panganibanpj commented on May 22, 2024

In my case it has something to do with using "babel-preset-env" too. Here's some package.json:

  "scripts": {
    "build-fails": "BABEL_ENV=fails babel ./main.js --out-file ./build.js",
    "build-works": "BABEL_ENV=works babel ./main.js --out-file ./build.js",
    "test": "babel-node node_modules/.bin/mocha ./main.js",
    "test-still-works": "BABEL_ENV=fails babel-node node_modules/.bin/mocha ./main.js"
  },

...

  "babel": {
    "env": {
      "fails": {
        "presets": [
          "env"
        ]
      },
      "works": {
        "presets": [
          "env"
        ],
        "plugins": [
          [
            "babel-plugin-transform-builtin-extend",
            {
              "globals": [
                "Error"
              ]
            }
          ]
        ]
      }
    },
    "plugins": [
      "transform-undefined-to-void",
      [
        "babel-plugin-transform-builtin-extend",
        {
          "globals": [
            "Error"
          ]
        }
      ]
    ]
  }

Here's the full example: extend-error.zip

from babel-plugin-transform-builtin-extend.

loganfsmyth avatar loganfsmyth commented on May 22, 2024

My guess would be that your usage of the env block in the config to toggle based on the env var is not working right. Unfortunately that's a problem with Babel's current approach to transformation. The order that you list plugins and presets in can affect their behavior.

It's a pain and unfortunate, but I don't think there's anything that I can explicitly do in this plugin to change that. If you're using env blocks in your config, I would recommend putting your full config in each block and not relying on inheriting pieces of the config from higher levels like you are doing now.

from babel-plugin-transform-builtin-extend.

panganibanpj avatar panganibanpj commented on May 22, 2024

I thought so too but I just tried with a different preset (babel-preset-stage-3) in the env block and the issue went away. Furthermore, the transform-undefined-to-void plugin in the example was meant to show that the plugin is still applied in both fails and works envs.

I think it's something to do with using the env block to apply babel-preset-env specifically, because this works fine:

  "babel": {
    "presets": ["env"],
    "plugins": [["babel-plugin-transform-builtin-extend", {"globals": ["Error"]}]]
    ]
  }

I'm ok with this issue being outside the scope of this plugin, but if it would help others, I'm hoping we could track down what's going on here and mention something in the "Limitations" section of README like OP said. I've never built a plugin or preset for babel so I don't have any intuitions about this but I wouldn't mind looking into it. Any advice on where to look would be appreciated :)

from babel-plugin-transform-builtin-extend.

loganfsmyth avatar loganfsmyth commented on May 22, 2024

It's likely specifically the ordering between this plugin and the one that transforms class syntax, so stage-3 would not cause the issue.

meant to show that the plugin is still applied in both fails and works envs.

Right. That plugin doesn't break when the ordering changes because there isn't any dependent ordering. With this plugin, it needs to run before the classes are transformed.

If you'd like to PR an update to the documentation, I'd be happy to accept it. I don't have a ton of time to focus on this plugin at the moment.

from babel-plugin-transform-builtin-extend.

sergej-s avatar sergej-s commented on May 22, 2024

Hi!

I have the same issue with the next config:

  "presets": [
    ["env", { "modules": false }],
    "react",
    "stage-2"
  ],
  "plugins": [
    ["babel-plugin-transform-builtin-extend", {"globals": ["Error"]}],
    "lodash"
  ],
  "env": {
    "development": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

What am I doing wrong? Can you help me?

from babel-plugin-transform-builtin-extend.

Jack-X-Yang avatar Jack-X-Yang commented on May 22, 2024

I have same issues @sergej-s . It seems like istanbul plugin cause the issue. I add blow comment in my code.

/* istanbul ignore next */

And it works.

from babel-plugin-transform-builtin-extend.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.