Skip to content

Plugins are not executed before Presets (doc/impl error) + Exception combining preset-env+fast-async #7815

Open

Description

@swernerx

Bug Report

Two issues:

  1. The documentation mentions that plugins are executed before presets: https://babeljs.io/docs/plugins/#pluginpreset-ordering (v6) or https://new.babeljs.io/docs/en/plugins (v7) which is clearly not what I see when I am combining two plugins/solutions for async/await transpilation.
  2. Crashes when using Preset-Env (loose=false) together with fast-async (aka nodent).

Input Code

async function executeTasks(tasks) {
  for (const taskName of tasks) {
    try {
      await executeCommands()
    } catch (error) {
      console.error("Error")
    }
  }
}

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    [ "@babel/env", {
      "debug": true,
      "loose": true
      // "exclude": ["transform-regenerator", "transform-async-to-generator"]
    }]
  ],

  "plugins": [
    "module:fast-async"
  ]
}

Expected Behavior

According to the documentation I would expect that fast-async is executed before any plugins from preset-env.

Current Behavior

Without the excludes on preset-env, the async code is transpiled to generators and the fast-async plugin is not executed anymore. Adding the excludes the code is correctly transpiled by fast-async, but this seems to happen after the preset is executed! This is another issue which is tricky as I'd like to instruct fast-async that the engine is es6 compatible (fast-async's es6target=true option) to let it output arrow functions, etc. which are then later env-optimized transpiled by preset-env - at least that's the idea.

Related: The whole thing crashes when loose is set to false (probably related to the complex spec transform of for...of loops together with nodent) with the following stack trace:

TypeError: Cannot read property 'length' of undefined
    at Buffer._append (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/buffer.js:115:26)
    at Buffer.append (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/buffer.js:81:10)
    at Generator._append (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:202:52)
    at Generator.word (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:126:10)
    at Generator.Identifier (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/generators/types.js:43:8)
    at /Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:315:23
    at Buffer.withSource (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/buffer.js:178:28)
    at Generator.withSource (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:182:15)
    at Generator.print (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:314:10)
    at Generator.printJoin (/Users/swerner/test-async-babel/node_modules/@babel/generator/lib/printer.js:382:12)

This exception happen as well with Babel v6 and fast-async:

TypeError: entry.js: Cannot read property 'length' of undefined
    at Buffer._append (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/buffer.js:100:26)
    at Buffer.append (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/buffer.js:72:10)
    at Generator._append (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:206:52)
    at Generator.word (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:131:10)
    at Generator.Identifier (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/generators/types.js:38:8)
    at /Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:298:23
    at Buffer.withSource (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/buffer.js:159:28)
    at Generator.withSource (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:189:15)
    at Generator.print (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:297:10)
    at Generator.printJoin (/Users/swerner/test-async-babel-v6/node_modules/babel-generator/lib/printer.js:366:12)

Possible Solution

Clearly we need some better control in babelrc on the order or priorities in which plugins/presets are being executed. I also found a PR #5735 here which holds some discussion but somehow stops somewhere in 2017. Not very promising.

I figure one issue here is that we put presets and plugins into separate lists. This is hard to document, understand and implement. I am wondering why we are not just think of presets as plugins and and expanding them at the place they are in one list. Think of merging presets and plugins into one array called transforms.

I figure this has been discussed before. So no worries if so. It would just help us preset developers when some docs would be brought in place on how to solve such - in my opinion - basic requirements.

Context

I am working on my https://github.com/sebastian-software/babel-preset-edge to be updated to Babel v7.

Your Environment

software version(s)
Babel 7.0.0-beta.46
node 10.0
npm 6.0
Operating System macOS 10.13.4

Activity

  1. babel-bot commented on Apr 26, 2018

    @babel-bot
    Collaborator

    Hey @swernerx! We really appreciate you taking the time to report an issue. The collaborators
    on this project attempt to help as many people as possible, but we're a limited number of volunteers,
    so it's possible this won't be addressed swiftly.

    If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
    community that typically always has someone willing to help. You can sign-up here
    for an invite.

  2. changed the title [-]Plugins are not executed before Presets (doc/implementation error)[/-] [+]Plugins are not executed before Presets (doc/impl error) + Exception combining preset-env+fast-async[/+] on Apr 26, 2018
  3. Jessidhia commented on Apr 26, 2018

    @Jessidhia
    Member

    Related: #5854 and #6921

    The problem here is that babel is merging visitors for performance, and in this specific case, those plugins don't seem to be compatible with each other; probably they both expect to be first to visit a given node. passPerPreset: true would "fix" it but is not the real solution 🤔

  4. swernerx commented on Apr 26, 2018

    @swernerx
    Author

    Thanks @Kovensky - this would explain the requirement for "exclude", but does not yet explain the crash in the code generator.

    Also it would still mean that the docs definitely have to get some update on the details of processing which happens inside babel.

    Regarding passPerPreset - I figure it would make all things slower right?

  5. swernerx commented on Apr 26, 2018

    @swernerx
    Author

    BTW: I made a small repository for showcasing the issues and playing around with different configurations: https://github.com/swernerx/test-babel-async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions