-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Build: transpile packages into one file per entrypoint #73516
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
Conversation
|
There's plenty of e2e failures, let's see what causes them. I did a quick local check before submitting the PR and the editor loaded OK. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Turns out I was looking at a different There is the Now we have the But after this PR, that tree shaking doesn't work any more. Why? There are two ways how things can be eliminated by tree shaking:
The I'll try to fix this by returning the correct value of |
|
Size Change: +49 kB (+1.9%) Total Size: 2.62 MB
ℹ️ View Unchanged
|
packages/block-library/package.json
Outdated
| "sideEffects": [ | ||
| "build-style/**", | ||
| "src/**/*.scss", | ||
| "{src,build,build-module}/*/init.js" |
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.
The init.js files for individual blocks are still problematic. As far as I know, they are not used anywhere in the Gutenberg app. And the built package bundle (build-module/index.js) doesn't include them, because they are not imported anywhere.
They are exposed as package exports by virtue of the "./build-module/*" export in package.json. These exports are currently broken, however, because we don't build them as separate entrypoints yet.
packages/blocks/package.json
Outdated
| "react-native": "src/index", | ||
| "wpScript": true, | ||
| "sideEffects": [ | ||
| "{src,build,build-module}/{index.js,store/index.js}" |
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.
esbuild doesn't support the brace expansion syntax in globs, so this format for sideEffects doesn't work there. I'm making two changes for many package.json files in this PR:
- for the
buildandbuild-modulefolders, declare theindex.jsfile as having side effects. We don't have thebuild-module/store/index.jsfiles any more. - convert the brace expansion syntax to an explicit list, for better bundler compat. We need to be more careful now when we don't use webpack exclusively.
| return { | ||
| path: args.path, | ||
| external: true, | ||
| sideEffects: !! packageJson.sideEffects, |
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.
This tells the bundler whether the external import has side effects, and allows the bundler to eliminate the import completely.
This simple code is rather blunt. If a package declares only *.scss styles as side-effectful, which is rather common, we'll treat the entire package, including its JS code, as side-effectful.
It works for now, but should be expanded into really checking the list of file patterns.
|
The editor itself including e2e tests is now OK after the The @mirka do you have any clue what might cause this? Is there a part of Storybook that works directly with the source, e.g., doing syntax highlighting or something similar? |
The docgen maybe? I believe it's running source files through |
9527f9b to
2692a61
Compare
I managed to fix this error by excluding the Seems that Next possible steps to improve the Storybook build:
|
Related: #72488 |
b61a095 to
d7f84ce
Compare
|
Flaky tests detected in f41e47d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20026376102
|
| const srcPathNormalized = normalizePath( | ||
| path.join( packageDir, srcPath ) | ||
| ); | ||
| // Find the real source file, with one of the supported extensions, with `glob`. |
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.
Another niche feature where I happen to have a package tailored to it 😅 https://github.com/aduth/resolve-file-extension Not sure if the approach may be more optimized.
| const rootCjsExport = rootExport.require || rootExport.default; | ||
| if ( rootCjsExport ) { | ||
| addBuildPath( cjsEntryPoints, rootCjsExport ); | ||
| } | ||
| const rootModuleExport = rootExport.import || rootExport.default; |
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.
Is checking || rootExport.default for both of these valid? Shouldn't default only match for one or the other?
| } | ||
|
|
||
| // Add `index` and `init` scripts for each block. | ||
| if ( packageName === 'block-library' ) { |
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.
Wonder if there's a way we can make this more generic without building in awareness of specific packages. I think the approach has generally been to try to abstract custom wp* properties that are defined at the package level to support specific behaviors like this.
|
For me this PR as it stands feels like it's doing too much (I'm willing to be convinced). It's fixing side effects which seems like a good change but also changing how we transpile packages to avoid just fixing the exported file name extensions. Isn't there a simpler way to solve this that is esbuild native (tell it to generate the right extensions and the right imports) I think "bundling" during transpiration phase is a big change and I'm not sure we understand all what it means. |
Yes, I agree that this is most likely an unsuccessful experiment. It completely reorganizes the package structure, with uncertain consequences for consumers. The bundled I'll now do the following:
|
933f526 to
f41e47d
Compare
This is an alternative to #73422 that solves the same problem with a different method.
When preparing a package to be consumed by other package or published to NPM, we used to transpile every file individually and copy the entire directory structure to
buildorbuild-module. For example,packages/block-editor/src/components/block-actions/index.jswould be transpiled (JSX syntax, TypeScript, ...) and written intopackages/block-editor/build-module/components/block-actions/index.js. One by one.After this PR, we bundle all the sources together into just one big
packages/block-editor/build-module/index.jsfile. That's the only JS export anyway, and the inner structure should be completely hidden to an outside consumer, especially if the consumer respects the modernexportsfield inpackage.json.There's not always just one export: we also have additional exports defined in
wpScriptModuleExports, and we respect them and bundle them separately. For example,block-editorexports.and./utils/fit-text-frontend. Therefore, in thebuild-modulefolder you'll find two output files:Reasons for doing this:
import x from './x.js', exactly all the import that Build: Add post-processing step to ensure fully specified ESM relative import paths #73422 tries to post-process. These relative imports are bundled now.transpilePackagetask, and it already does bundling for CSS imports. These imports are not left unchanged, like the relative JS imports are, but the CSS is inlined. This PR takes the bundling step to its logical end.This will be a breaking change for anyone who does "internal" imports like
import from '@wordpress/block-editor/build-module/...'. This will stop working because the imported file will no longer exist. You need to import everything through the official exports, i.e., the rootindex.jsfile.