-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Platform Docs: Update intro.md
#60087
Changes from 1 commit
fe80dfe
8bd0748
f6dd539
015a33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,13 +30,39 @@ To build a block editor, you need to install the following dependencies: | |||||
|
||||||
We're going to be using JSX to write our UI and components. So one of the first steps we need to do is to configure our build tooling, By default vite supports JSX and and outputs the result as a React pragma. The Block editor uses React so there's no need to configure anything here but if you're using a different bundler/build tool, make sure the JSX transpilation is setup properly. | ||||||
|
||||||
## Side note: `process.env.IS_GUTENBERG_PLUGIN` | ||||||
|
||||||
The Gutenberg block editor reads from the `process.env.IS_GUTENBERG_PLUGIN` variable. It | ||||||
is used to distinguish the code that is part of the Gutenberg block editor | ||||||
[plugin](https://wordpress.org/plugins/gutenberg/) and the code that is part of | ||||||
WordPress core. | ||||||
|
||||||
For our purposes, as we're building a standalone block editor that is completely | ||||||
separate from WordPress, we want to set this variable to `false`. | ||||||
|
||||||
When using Vite, we can do it by creating a `vite.config.js` file: | ||||||
|
||||||
```js | ||||||
// vite.config.js | ||||||
import { defineConfig } from "vite"; | ||||||
|
||||||
export default defineConfig({ | ||||||
define: { | ||||||
"process.env.IS_GUTENBERG_PLUGIN": JSON.stringify(false), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually don't think we should add this to the docs, it doesn't make sense for third-party developers and shouldn't be required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked a bit into this and found that a similar request for the My understanding is that we cannot use This is the options I see:
I'd like to summon @gziolo @jsnajdr @swissspidy @talldan @t-hamano that have participated in similar conversations, in case they have thoughts/advice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FWIW, I think we already publish our scripts as modules. So is the idea to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK why Gutenberg uses
Tip: when linking to files on GitHub, copy its permalink via the three-dots menu or press Y on the keyboard to get the permalink. This way we can see the file in the version at the time of writing, even when the file is later modified or moved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For bundled scripts yes, but packages can ship and support environment variables. process.env variables have always been the standard in the JS community for env variables but lately import.meta is becoming the standard when it comes to ES modules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'd love if we can experiment with this #60087 (comment) and use consistently for all of our env variables. SCRIPT_DEBUG is a runtime variable, so it's different. That said, I don't want to block this PR more. I'm fine with mentioning the variable in our docs temporarily while we figure this out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mentioned it in slack, but will mention it here too. There's also usage of I don't know if build tools handle that property in a special way or if it also causes the same issues as Just mentioning it as I'd hate to see effort go into fixing |
||||||
}, | ||||||
}); | ||||||
``` | ||||||
|
||||||
If you are using Webpack, you can use the | ||||||
[DefinePlugin](https://webpack.js.org/plugins/define-plugin/) to accomplish the | ||||||
same thing. | ||||||
|
||||||
## Bootstrap your block editor | ||||||
|
||||||
It's time to render our first block editor. | ||||||
It's time to render our first block editor. Update your `index.jsx` file with the following code: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using "Vanilla" starter
Suggested change
It’s one more small reason to prefer the "React" starter as relevant files will already be I'd be happy to put the switch to "React" starter in a follow-up PR though because, well, like a goof I already created one that does that (and has many of the changes in this one) because I didn’t check first to see if it was being worked on. |
||||||
|
||||||
- Update your `index.jsx` file with the following code: | ||||||
```jsx | ||||||
import { createElement, useState } from "react"; | ||||||
import React, { useState } from "react"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine but I kind of thought that vite uses the automatic JSX config by default. it's not the case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are more details in https://v2.vitejs.dev/guide/features.html#jsx but TL;DR is that it's not the case out of the box. We can also do one of those things
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it simple and just import React like you did for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you create the Vite app with the "React" / "JavaScript" config instead of "Vanilla" / "JavaScript" config, this works automatically. The vite config becomes: import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, starting with the "Vanilla" vite config cerates a few issues (jsx, react), why not change the docs to suggeste starting with the "React" vite config? Those things will be solved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If starting with "Vanilla" then I'd favor instructing to use the "React" variant. It seems like it simplifies the instructions and provides a better starting place as it adds some helpful packages like eslint with react-related plugins. |
||||||
import { createRoot } from 'react-dom/client'; | ||||||
import { | ||||||
BlockEditorProvider, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should fix this in Gutenberg directly. We should make sure that it works by default without setting this rather than having npm consumers learn about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that makes sense! I see that this is actually a GB issue because it should have been replaced at build time and we even have a lint for it.
Do you have any idea why that would not be the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the version of the packages published to npm does not have the value of
process.env.IS_GUTENBERG_PLUGIN
replaced. E.g.: You can check here: https://unpkg.com/browse/@wordpress/[email protected]/build/resolvers.jsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalczaplinski yes, that's fine, because Gutenberg also consumes the built npm packages like any other app. The problem is that it shouldn't cause an error by default but should assume "false" if you don't define the variable.