-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Remove babel-plugin-inline-json-import in favor of native webpack JSON support #53470
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
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +3.78 kB (0%) Total Size: 1.51 MB
ℹ️ View Unchanged
|
@youknowriad explored replacing Babel plugin with the proposal for JSON modules in #34176. That could be used as a reference as we didn't continue with the PR because it was a stage 3 proposal, and WordPress uses only accepted proposals. Anyway, it's good to cross-check changes applied to ensure all necessary file paths get updated. It's a separate discussion whether we are fine using more custom features supported by bundlers like JSON imports. Edit: I'm a little bit confused about the current status of import proposals as they include different syntax: |
1f919af
to
8772fea
Compare
The My PR doesn't use the import assertions at all, it just makes use of webpack's support for JSON module. The assertions are there for security purposes -- to make it harder to trick the browser into parsing source of type X as source of type Y. There is still need to stop using the named exports, like I have two immediately actionable takeaways from this work:
|
IMO we can't defer json imports to the application layer (webpack) without relying on the import json proposal because `import something from "./file.json" is not standard and shouldn't be included in the "output" of npm packages. |
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
That's true, although I've been surprised how good the support for import assertions already is. Except Firefox, evergreen browsers all support JSON imports. And Firefox has an experimental https://caniuse.com/mdn-javascript_statements_import_import_assertions_type_json |
I can be onboard with import assertions but the fact that the initial proposal changed already make me a bit nervous. |
I looked at some of the TC39 meeting notes and slides and this is a summary of what happened:
That's true, but for practical purposes, it's "almost there". Node 16+ supports it, it will just print a warning:
And the HTML standard already says that a compliant HTML implementation must support JSON modules. But we can wait until it really becomes an undisputed standard, at this moment I'd say it's a matter of months, not years. |
IMO, we can still make the migration in the source code if we find a way to "bundle" these in the output of the packages. And "months" sounds good to me. |
8772fea
to
f34481a
Compare
Fixes #49458. Removes the
babel-plugin-inline-json-import
in favor of native webpack 5 JSON imports.Webpack can optimize these imports. In code like:
it detects that only the
name
field is used and will inline a truncated version of the JSON:Very similar to the previous inlining behavior, but optimized!
The patch had to solve several issues:
block.json
files are no longer inlined at Babel level, we need to copy theblock.json
files to thebuild-module
directories of individual packages, so that webpack can find them there. Previously,block.json
files were inlined by Babel, and webpack was not aware of them at all.import { name } from './block.json'
. It supports only default exports. Therefore we need to rewrite theimport { name }
pattern toimport metadata
+metadata.name
.There was a tricky unit test failure I had to fix. When a block is being serialized, we don't serialize attribute values that are equal to their default values, per
block.json
schema. But the===
comparison:gutenberg/packages/blocks/src/api/serializer.js
Line 241 in 5fe62fd
doesn't work reliably for values like
[]
. Apparently before this PR, the attribute schemas were inlined two times at two different places, so thatschema1.default !== schema2.default
. But now the schema is shared, so the two[]
values are identical to each other.A good fix would be to use
fast-deep-equal
for the default value comparison.