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

Disable workers with string literals and improve diagnostics #6536

Merged
merged 15 commits into from Jul 6, 2021

Conversation

devongovett
Copy link
Member

@devongovett devongovett commented Jul 2, 2021

Closes T-1087

This disables workers of all types from being created with a string literal. It's now required to use new Worker(new URL('filename.js', import.meta.url), {type: 'module'}). This is because strings passed to the worker constructor are supposed to be resolved relative to the page URL, not to the current file. Using the URL constructor solves this issue so that Parcel is more web compatible. This is also how other bundlers like webpack now work.

This also makes some formatting improvements to diagnostics, especially when multiple diagnostics are displayed at once. The code frame and hints are now indented underneath each error message so it's clear what it is associated with.

image

In addition, it's now possible to have multiple code frames pointing to different files within the same diagnostic. This way, errors can show context for the error in another file without creating a separate diagnostic which looks like a different error.

image

There's also some other small diagnostic improvements in this PR which you can see in the code.

@height
Copy link

height bot commented Jul 2, 2021

This pull request has been linked to and will mark 1 task as "Done" when merged:

💡Tip: You can link multiple Height tasks to a pull request.

@@ -63,7 +65,7 @@ export type Diagnostic = {|
language?: string,

/** A code frame points to a certain location(s) in the file this diagnostic is linked to (optional) */
codeFrame?: ?DiagnosticCodeFrame,
codeFrame?: ?DiagnosticCodeFrame | ?Array<DiagnosticCodeFrame>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could possibly make this always an array if we wanted. I just didn't want to have to change all the tests. Any opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think BuildFailureEvent originally also contained either a diagnostic or an array of them, and it was rather cumbersome to use because you always had to somehow normalize them into an array anyway yourself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've refactored it to rename codeFrame to codeFrames and make it always an array. Also moved language and filePath into code frames rather than on the top-level diagnostic object.

@parcel-benchmark
Copy link

parcel-benchmark commented Jul 2, 2021

Benchmark Results

Kitchen Sink 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

React HackerNews ✅

Timings

Description Time Difference
Cold 11.40s +95.00ms
Cached 505.00ms -17.00ms

Cold Bundles

No bundle changes detected.

Cached Bundles

Bundle Size Difference Time Difference
dist/logo.1e014c76.png 274.00b +0.00b 359.00ms +30.00ms ⚠️

AtlasKit Editor 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

Three.js ✅

Timings

Description Time Difference
Cold 7.93s -38.00ms
Cached 396.00ms -14.00ms

Cold Bundles

No bundle changes detected.

Cached Bundles

No bundle changes detected.

Click here to view a detailed benchmark overview.

@devongovett
Copy link
Member Author

Turns out importScripts() also has the same problem: the specified urls are supposed to resolve relative to the entry worker script, which we cannot know at build time. I've added error diagnostics for this as well. Unfortunately, it means there is no way to import into a worker at all unless you use {type: 'module'} since the new URL trick won't work due to import.meta.url not being supported in scripts.

image

image

Copy link
Member

@DeMoorJasper DeMoorJasper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, not totally sure if we should remove filepath if there's no codeframe?

@devongovett
Copy link
Member Author

Yeah I wondered the same. It just seemed weird to me to have filePath at the top-level if each code frame can refer to a different file. I think it's still possible to have a code frame with only a file path though, and no code highlights. Not really sure when that would happen though.

@devongovett devongovett merged commit 79923ce into v2 Jul 6, 2021
@devongovett devongovett deleted the disable-worker-string branch July 6, 2021 01:45
lettertwo added a commit that referenced this pull request Jul 13, 2021
* v2: (34 commits)
  Wrap assets recursively when any incoming dependency is wrapped (#6572)
  Improvements for library targets (#6570)
  Diagnostic for undeclared external dependencies in library builds (#6564)
  More bugs (#6567)
  Don't require `url:` for image transformer (#6565)
  Remove 'Name already registered with serializer' error (#6566)
  Fix live bindings and `this` of external CommonJS modules (#6548)
  JS runtime improvements (#6531)
  Make sure the absolute path isn't contained in the cache (#5900)
  Adds '@parcel/diagnostic' to dependencies (#6563)
  Disable workers with string literals and improve diagnostics (#6536)
  Bug fixes (#6541)
  Don't attempt to resolve URLs starting with '#' (#6504)
  Correctly set worker's output format if not support by environment (#6534)
  Babel: Recognize peerDependencies in isJSX (#6497)
  fix setHeaders ordering on dev server (#6500)
  Graph: Remove Node interface (#6530)
  Fix TS build script for old Node versions (#6526)
  Improve library targets (#6517)
  Fix TypeScript and other sourcemaps by always creating an initial sourcemap (#6472)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants