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

@wordpress/babel-preset-default@4.0.0 is causing an "Identifier 'createElement' has already been declared" error #14381

Closed
ocean90 opened this issue Mar 11, 2019 · 15 comments · Fixed by #14482
Labels
[Tool] Babel preset /packages/babel-preset-default [Type] Developer Documentation Documentation for developers

Comments

@ocean90
Copy link
Member

ocean90 commented Mar 11, 2019

Describe the bug
After upgrading to @wordpress/babel-preset-default 4.0.0 I'm seeing with the setup from here this error:

ERROR in ./blocks/team-list/edit.js 7:9
Module parse failed: Identifier 'createElement' has already been declared (7:9)
You may need an appropriate loader to handle this file type.
| import _inherits from "@babel/runtime/helpers/inherits";
| import { createElement } from "@wordpress/element";
> import { createElement } from "@wordpress/element";
|
| /**
 @ ./blocks/team-list/index.js 9:0-26 50:8-12
 @ ./blocks/index.js
 @ ./editor.js

To Reproduce

  1. git clone --single-branch --branch gutenberg git@github.com:wearerequired/wp-team-list.git
  2. cd wp-team-list
  3. npm run build:js -- everything works
  4. npm install @wordpress/babel-preset-default@4.0.0 --save
  5. npm run build:js -- build is broken

Expected behavior
Step 3 and 5 should both successfully produce a build.

Additional context
Might be related to #13809 and/or #14106.

@swissspidy swissspidy added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. [Tool] Babel preset /packages/babel-preset-default Needs Technical Feedback Needs testing from a developer perspective. labels Mar 11, 2019
@swissspidy
Copy link
Member

Currently facing the same issue at ampproject/amp-wp#1952

@swissspidy
Copy link
Member

According to #13540 and the babel-preset-default changelog, one apparently does not have to manually include the @wordpress/babel-plugin-import-jsx-pragma plugin anymore.

Removing it from the setup resolves the issue for me.

@aduth
Copy link
Member

aduth commented Mar 12, 2019

According to #13540 and the babel-preset-default changelog, one apparently does not have to manually include the @wordpress/babel-plugin-import-jsx-pragma plugin anymore.

Ah, it seems the issue may then be that there's two separate instances of the @wordpress/babel-plugin-import-jsx-pragma plugin being applied, hence the error about the identifier already having been defined?

It makes sense, though in retrospect I don't think was as consciously considered as it should have been (i.e. upgrade instructions for those using both the preset and the plugin already). Furthermore, perhaps the plugin could be made more tolerant to being run twice. I suspect the issue is that, by the implementation, both encounter and decide at the occurrence of the JSX tag to add the import.

@youknowriad
Copy link
Contributor

Anything to do here? Since 4.0.0 is a breaking change, I'm fine just closing the issue. We could also add a note somewhere in the CHANGELOG.

@youknowriad youknowriad removed [Type] Bug An existing feature does not function as intended Needs Technical Feedback Needs testing from a developer perspective. Needs Testing Needs further testing to be confirmed. labels Mar 14, 2019
@swissspidy
Copy link
Member

I think the changelog is quite clear. One just has to read it :-)

@youknowriad
Copy link
Contributor

Got it, let's close :)

@gziolo gziolo assigned gziolo and unassigned gziolo Mar 14, 2019
@gziolo
Copy link
Member

gziolo commented Mar 14, 2019

I'm glad it was a version upgrade issue. The latest implementation should auto-detect whether createElement is in the scope and add proper pragma only when necessary.

@ocean90
Copy link
Member Author

ocean90 commented Mar 14, 2019

https://github.com/WordPress/gutenberg/blob/master/packages/babel-plugin-import-jsx-pragma/README.md#usage should still get an update.

@ocean90 ocean90 reopened this Mar 14, 2019
@ocean90 ocean90 added the [Type] Developer Documentation Documentation for developers label Mar 14, 2019
@gziolo
Copy link
Member

gziolo commented Mar 14, 2019

How should it be updated exactly? The referenced usage looks legitimate. I mean, I can update but I want to learn what’s is wrong there.

@ocean90
Copy link
Member Author

ocean90 commented Mar 14, 2019

As mentioned in the issue, when you use both plugins (as mentioned in Usage) or copy the example configuration from Options, you get the error due to the duplicate declaration.

@youknowriad
Copy link
Contributor

It looks like the README is not using both presets at the same time, it seems correct as is for me? Anything I'm missing?

@ocean90
Copy link
Member Author

ocean90 commented Mar 15, 2019

@youknowriad Updated steps from #14381 (comment) to reproduce:

  1. git clone --single-branch --branch master git@github.com:wearerequired/wp-team-list.git
  2. cd wp-team-list
  3. npm install
  4. npm run build:js - everything works
  5. Apply this diff: (which is was https://www.npmjs.com/package/@wordpress/babel-plugin-import-jsx-pragma#options is suggesting)
diff --git .babelrc .babelrc
index dcff30b..20fabb0 100644
--- .babelrc
+++ .babelrc
@@ -3,6 +3,14 @@
     "@wordpress/default"
   ],
   "plugins": [
+    [
+      "@wordpress/babel-plugin-import-jsx-pragma",
+      {
+        "scopeVariable": "createElement",
+        "source": "@wordpress/element",
+        "isDefault": false
+      }
+    ],
     [
       "@babel/transform-react-jsx", {
         "pragma": "createElement"
  1. npm run build:js - Build is broken

@swissspidy
Copy link
Member

(which is was https://www.npmjs.com/package/@wordpress/babel-plugin-import-jsx-pragma#options is suggesting

The difference is that this README does not mention @wordpress/default, which you are using. Kinda makes sense, as one might want to use it standalone.

The changelog for @wordpress/default however mentions that it is not necessary to manually include @wordpress/babel-plugin-import-jsx-pragma anymore when using the default config.

So, to be clear, are you suggesting to add a note to https://www.npmjs.com/package/@wordpress/babel-plugin-import-jsx-pragma#options a la "Not needed when already using @wordpress/default"?

@gziolo
Copy link
Member

gziolo commented Mar 15, 2019

Yes, let’s do it. There might be some issues when versions of the plugin differ between preset and the plugin itself.

@gziolo
Copy link
Member

gziolo commented Mar 18, 2019

I hope #14482 will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] Babel preset /packages/babel-preset-default [Type] Developer Documentation Documentation for developers
Projects
None yet
5 participants