-
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
@wordpress/scripts - webpack runtime issue: blocks importing code from a local package, when compiled with npm run build
, do not load in editor
#23607
Comments
npm run build
does not, and when including a regular script on the headernpm run build
, and including a regular script on the header
This same problem happens when registering blocks too, when a block is referencing a package from the same plugin. My plugin has this layout:
The package under {
"name": "@graphqlapi/components",
} To reference this package, the blocks have this internal dependency in their {
"devDependencies": {
"@graphqlapi/components": "file:../../packages/components",
}
} When any of the blocks imports a component from this package, like this: import { getEditableOnFocusComponentClass } from '@graphqlapi/components'; And it is compiled using
from line function s(t) {
if (r[t])
return r[t].exports;
var n = r[t] = {
i: t,
l: !1,
exports: {}
};
return e[t].call(n.exports, n, n.exports, s),
n.l = !0,
n.exports
} Because wp.blocks.getBlockTypes() returns the list without that block! |
npm run build
, and including a regular script on the headernpm run build
do not appear on wp.blocks.getBlockTypes()
I found out a hack that solves the issue. For some obscure reason, running But if we add this piece of code, on any const fixIncrediblyWeirdBug = {
...{},
} The issue seems to be related to the spread operator function(e, t) {
e.exports = function(e, t, n) {
return t in e ? Object.defineProperty(e, t, {
value: n,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[t] = n, e
}
} I wonder if that
|
I created gists with the working and non-working Non-working: https://gist.github.com/leoloso/98c1673018dbdacfefd835ebbde70bc9 Working: https://gist.github.com/leoloso/f6de95659fc2c92b0fdedf42d1aa3087 |
The output from I executed
Please notice that this dependency is under the block. This same module is also built, but belonging to the referenced package, for both when it works and when it fails:
So maybe the problem is related to Babel? |
Alright, this is just crazy. In another block, it happens the opposite situation! The block already has a spread operator: return {
...state,
schemaConfigurations: action.schemaConfigurations,
hasRetrievedSchemaConfigurations: true,
retrievingSchemaConfigurationsErrorMessage: action.errorMessage,
}; In this case, the block compiled with It works after removing the spread operator: return Object.assign( {}, state, {
schemaConfigurations: action.schemaConfigurations,
hasRetrievedSchemaConfigurations: true,
retrievingSchemaConfigurationsErrorMessage: action.errorMessage,
} ); |
If I have to guess, the problem is related to library Currently it is not part of Gutenberg. I wonder if it is the solution to make the spread operator work consistently well. |
I have one block that also doesn't work, and the reason is connected to passing props to a React component through the spread operator: <SchemaConfigOptionsCard
isPublicPrivateSchemaEnabled={ isPublicPrivateSchemaEnabled }
isSchemaNamespacingEnabled={ isSchemaNamespacingEnabled }
{ ...props }
/> Yes, having that For this issue, I see no solution (I can't pass all the properties, one by one). So until solved, I must compile this block using: wp-scripts start --webpack--devtool= |
npm run build
do not appear on wp.blocks.getBlockTypes()
npm run build
do not load in editor, JS error thrown
To summarize, the error is this one:
The environment for the error is:
For instance, some block The error happens with either of these situations:
In 1., adding the function(e, t) {
e.exports = function(e, t, n) {
return t in e ? Object.defineProperty(e, t, {
value: n,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[t] = n, e
}
} Please notice that this code deals with In 2., Hence, it seems that the issue is not about code being in the output or not, but when/where it is added. In addition to this code, the working/non-working From what I've read in other repos (eg: here, here, here), |
Can you share a link to the plugin to reproduce the issue? Are you defining |
I tried just now. I don't know if it fixed the error, but for sure it generated others. Now, the console shows two errors:
and:
One block doesn't work at all, and another one lost its styles: I also added
I just tried this: module.exports = {
//...
output: {
jsonpFunction: 'wpJsonpFlightsWidget'
}
}; I've got mixed results from it. On one side, it seems to fix the issue when registering the script for the Document TabPanel, which was the issue in the first comment. But on the other, for the blocks importing code from a package within the same plugin, now 3 blocks are failing, and it shows some other errors on the console:
and, for each failing block:
My repo is private, but I can invite you to it. Are you OK with that? Otherwise, I plan to make the repo public in around 2 months (once the plugin in it is ready). |
Actually, results from using
I just discovered something. I had added Actually, those new JS errors belong to the other 3 blocks. I removed again that code for those 3 and kept it for the failing block only, and now they all work! In summary, doing
|
Sorry, I wasn't clear here. It's better to not have this setting, at least not until you know what it does. If |
Thanks for the link and suggestion, I'll check it out.
Did it happen also in Gutenberg? Is this something that should be taken care of already in Gutenberg core? Or at least, to add documentation in Gutenberg warning about the issue? (after all, I have no experience with webpack, I only use it through Gutenberg) I offered to share access to my private repo, but it makes no sense. I'll get to work already on making my repo public, so I can share the reproduction of the error, and maybe we can check if the error applies to a generic case, or it's just too specific to my plugin. I'll share the link in a few days. |
@gziolo @ocean90 I can announce that Dominik's solution fixes the issue 😀 The error is happening when a block imports code from a local package. Then, there are two webpack configurations involved in the compilation of the block: the block's, and the package's. So I created a custom const config = require( '@wordpress/scripts/config/webpack.config' );
/**
* Because the block and the package have their own webpack configuration,
* they must provide a unique name for the global scope (which is used to lazy-load chunks),
* otherwise it throws a JS error when loading blocks compiled with `npm run build`
* @see https://github.com/WordPress/gutenberg/issues/23607
* @see https://webpack.js.org/configuration/output/#outputjsonpfunction
*/
// ------------------------------------------------------
config.output.jsonpFunction = 'SOME_UNIQUE_NAME_HERE';
// ------------------------------------------------------
module.exports = config; By adding Is this something to be considered for |
Btw, I tried two alternatives, only one works:
|
npm run build
do not load in editor, JS error thrownnpm run build
, do not load in editor
In my opinion this is definitely something to be considered.
|
Thanks @leoloso for a deep look into the issue. I was having a similar issue. And thanks to you I was able to fix it in my project. I'm using npx @wordpress/create-block to create my blocks. When I run yarn start (which runs wp-scripts start) the block works for me fine. As soon as I build it yarn build (which runs wp-scripts build )everything breaks. In my case the error I was getting in my console was (Uncaught TypeError: Object(...) is not a function at Module. After reading all this thread I decided to do what you did, create a webpack.config.js file in my block and add an id to config.output.jsonFunction.
I agree with @bobbingwide recommendation. Specially point 3. Documentation is a key. |
…h other plugins built with @wordpress/scripts (see: WordPress/gutenberg#23607)
I did a quick check how It aligns with the proposal shared. Let's do it! Unless webpack 5 upgrade that we are almost ready to land resolves it 😄 |
Thank you @dnpg - I can confirm that this fixes it for me. My GPT-3 block was conflicting with Jetpack and Stackable blocks. I had the same error message as others in this thread. One way to avoid the problem for me was to use |
It looks like this issue was resolved in webpack 5: https://webpack.js.org/blog/2020-10-10-webpack-5-release/#automatic-unique-naming
We are about to switch to webpack 5 so it should non issue when that happens ✨ |
Thank you for opening this @leoloso!! I have been tearing my hair out trying to figure out why this was happening... For reference, I have 5 block plugins installed and they would all conflict with one another after running the wp-scripts build script, no problems with the start script... I finally was able to pinpoint the issue to any plugins that import ./style.scss files. Removing the style.scss import would resolve the issue. Ultimately, this solution fixed the issue for me as well so I could import those ./style.scss files again: |
In the meantime, we landed webpack 5 upgrade in #26382 but we had to revert because of several incompatibilities with the plugins used, plus Storybook that Gutenberg uses doesn't work with webpack 4 yet. Anyway, I plan to apply the patch to resolve the issue for webpack 4 as proposed in #23607 (comment) by @ocean90. |
I hope that #27985 will fix it. |
@leoloso I am seeing this issue. I did not use create-block. I have a plugin with 3 blocks in it. It looks like:
Can I point |
@leoloso sorry about that, right after I hit comment I noticed something that I looks like it fixes it and accomplishes the same thing. This is what I did (for each blocks script):
|
Describe the bug
I am scaffolding a single-script plugin (not a single-block plugin) as documented here:
@wordpress/create-block
index.asset.php
contains the following dependencies:Please notice that
wp_register_script
is inserting the script in the header, not in the footer. Then, an anomaly happens: the code below is also added on the header, and not on the footer (where it is normally added):I guess this happens from one of the dependencies from the script triggering that code to be added right then, which is the header.
Now, there is a weird thing happening: if the blocks registered for that page have been compiled using
npm start
, then it works fine. However, if they were compiled usingnpm run build
, then the script fails, the screen is all white, and the console shows this error:Please see the screenshot below from the debugging, on the line where the error happens. As can be observed:
n
is undefined, son.attributes
explodes"graphql-api/schema-configuration"
was compiled usingnpm run build
window.wp.blocks.getBlockTypes()
doesn't contain block"graphql-api/schema-configuration"
among them!Hence, the last item is the problem: the block was not added to the list of types, and so doing
select( 'core/blocks' ).getBlockType( "graphql-api/schema-configuration" )
returnsnull
, and then the script fails.If the script is registered in the footer, then it works fine (with both
npm start
andnpm run build
).Then, at least 1 of the following must be fixed:
@wordpress/create-project
package to scaffold scripts and styles #23514npm run build
is not being added towindow.wp.blocks.getBlockTypes()
npm start
andnpm run build
is differentTo reproduce
Steps to reproduce the behavior:
npm run build
from the@wordpress/scripts
packageExpected behavior
Either the error should not happen, or the documentation must mention that registering a script with certain dependencies can only be done in the footer
Screenshots
Editor version (please complete the following information):
@wordpress/scripts
v12.0.0
, but I believe it fails sincev10.0.0
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: