Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to compile code with Macros while using Babel plugins. #9945

Open
zeel01 opened this issue Sep 6, 2024 · 4 comments
Open

Unable to compile code with Macros while using Babel plugins. #9945

zeel01 opened this issue Sep 6, 2024 · 4 comments

Comments

@zeel01
Copy link

zeel01 commented Sep 6, 2024

🐛 bug report

Using:

import macro from "./macro.js" with { type: "macro" };

Fails if I have any custom Babel configurations defined. I'm enabling a few plugins.

I get the following error:

You are using import attributes, without specifying the desired output syntax.
Please specify the "importAttributesKeyword" generator option, whose value can be one of:
 - "with"        : `import { a } from "b" with { type: "json" };`
 - "assert"      : `import { a } from "b" assert { type: "json" };`
Server running at http://localhost:1234
🚨 Build failed.

@parcel/transformer-js: Unexpected token `:`. Expected identifier, string literal, numeric literal or [ for the computed key

  /mnt/o/... 
  > 1 | import macro from "./macro.js" with { type: "macro" };
  >   |                                           ^

I can't seem to figure out how to define the option listed there, I've tried:

{
    "plugins": [
        ["@babel/plugin-syntax-import-attributes", { "importAttributesKeyword": "with" }],
        [...]
    ]
}

Which does nothing, and:

{
    "importAttributesKeyword": "with",
    "plugins": [
        ["@babel/plugin-syntax-import-attributes", { "importAttributesKeyword": "with" }],
        [...]
    ]
}

Which produces an error that that isn't a valid option.

If I use the configuration example from the Parcel Macro docs under the Babel section:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "shippedProposals": true
      }
    ]
  ]
}

Paracel complains a bunch about using redundant Babel stuff:

@parcel/transformer-babel: Parcel includes transpilation by default. Babel config babel.config.json includes the following redundant   
presets: @babel/preset-env. Removing these may improve build performance.

The code compiles, but produces nonsensical runtime errors indicating that the bundle is in some way invalid.

Am I missing something, or is this a bug?

🎛 Configuration (.babelrc, package.json, cli command)

babel.config.json:

{
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "version": "2023-11" }],
        ["@babel/plugin-syntax-import-attributes"],
        ["@babel/plugin-proposal-json-modules"],
        ["@babel/plugin-proposal-do-expressions"],
        ["@babel/plugin-proposal-async-do-expressions"]
    ]
}

I'm using this same config to build my backend code directly with Babel, it it has no issue with the import attributes and doesn't complain about the above config.

@devongovett
Copy link
Member

@babel/plugin-syntax-import-attributes should work to enable parsing import attributes. In the latest version it should already configure the importAttributesKeyword for you. Are you sure you're using the latest Babel version? https://github.com/babel/babel/blob/a2025d7d695b0f3b0506f66225d6b15bcfa1cf6a/packages/babel-plugin-syntax-import-attributes/src/index.ts#L23

@zeel01
Copy link
Author

zeel01 commented Sep 29, 2024

Yes, and as noted Babel compiles the backend just fine. The same config file, same workspace, but different behavior in Parcel.

@devongovett
Copy link
Member

I would need a reproduction in order to debug this further for you.

@jamiegluk
Copy link

This issue got me stuck for hours. I guess maybe I have something set up wrong. I gave up and ended up just hacking it.

I ditched @babel/plugin-syntax-import-attributes. Well I ditched any custom babel setup entirely, because Parcel Macros are all I needed it for.

@parcel/transformer-babel comes with @parcel/config-default. It uses @babel/generator, which is the tool issuing the error you get. So I used patch-package (or yarn patch works too) to patch node_modules/@parcel/transformer-babel/lib/BabelTransformer.js and add in the importAttributesKeyword: "with" line it's complaining about.

This is the patch:
@parcel+transformer-babel+2.12.0.patch

Now it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants