-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
fix(optimizer): map relative worker paths to correct relative file location #21434
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
Open
guyco3
wants to merge
19
commits into
vitejs:main
Choose a base branch
from
guyco3:fix-optimize-deps-worker-assets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a0c82f6
fix
guyco3 6572873
fix(deps): worker URLs in optimized dependencies
guyco3 4005c3f
style: fix formatting and remove debug check
guyco3 a38f12b
style: fix formatting and broaden transform filter
guyco3 02ccc4c
docs(optimizer): add note on why replacement has space
guyco3 ca925eb
style
guyco3 e8a7a8c
test(optimize-deps)
guyco3 cc43552
tests
guyco3 6486ed4
tests
guyco3 aa501ea
tests
guyco3 c2673a9
fix(optimizer): use portable relative paths for worker URLs
guyco3 0526018
fix(optimizer): opt-out of transform if no worker URLs are found
guyco3 ea588d6
chore: sync with upstream and resolve lockfile conflicts
guyco3 409737a
test(optimizer): fix path resolution for CI
guyco3 724d07d
fix(optimizer): use precise regex for portable worker URLs + add unit…
guyco3 0f897a2
chore: remove unused imports in playground test
guyco3 97510d0
fix(optimizer): improve worker regex
guyco3 6f9c65c
Merge remote-tracking branch 'upstream/main' into fix-optimize-deps-w…
guyco3 16bbe10
chore: trigger CI rerun
guyco3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import path from 'node:path' | ||
| import type { ImportKind, Plugin, RolldownPlugin } from 'rolldown' | ||
| import { prefixRegex } from '@rolldown/pluginutils' | ||
| import { JS_TYPES_RE, KNOWN_ASSET_TYPES } from '../constants' | ||
| import MagicString from 'magic-string' | ||
| import { FS_PREFIX, JS_TYPES_RE, KNOWN_ASSET_TYPES } from '../constants' | ||
| import type { PackageCache } from '../packages' | ||
| import { | ||
| escapeRegex, | ||
|
|
@@ -11,6 +12,7 @@ import { | |
| isExternalUrl, | ||
| isNodeBuiltin, | ||
| moduleListContains, | ||
| normalizePath, | ||
| } from '../utils' | ||
| import { browserExternalId, optionalPeerDepId } from '../plugins/resolve' | ||
| import { isModuleCSSRequest } from '../plugins/css' | ||
|
|
@@ -298,6 +300,45 @@ export function rolldownDepPlugin( | |
| } | ||
| }, | ||
| }, | ||
| transform: { | ||
| filter: { code: /new\s+URL/ }, | ||
| async handler(code, id) { | ||
| if (!id.includes('node_modules')) return null | ||
|
|
||
| const s = new MagicString(code) | ||
|
|
||
| const workerRE = | ||
| /new\s+URL\s*\(\s*['"]([^'"]+)['"]\s*,\s*import\.meta\.url\s*\)/g | ||
|
|
||
| let match | ||
| while ((match = workerRE.exec(code))) { | ||
| const [fullMatch, url] = match | ||
| const absolutePath = path.resolve(path.dirname(id), url) | ||
| const fsUrl = FS_PREFIX + normalizePath(absolutePath) | ||
guyco3 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // NOTE: add `'' +` to opt-out of Rolldown's static asset analysis. | ||
| // This prevents the optimizer from trying to bundle the absolute path | ||
| // as a relative asset. | ||
| const replacement = `new URL('' + ${JSON.stringify(fsUrl)}, import.meta.url)` | ||
|
|
||
| s.overwrite( | ||
| match.index, | ||
| match.index + fullMatch.length, | ||
| replacement, | ||
| ) | ||
|
|
||
| const assetDir = path.dirname(absolutePath) | ||
|
|
||
| if ( | ||
| environment.config.server.fs.allow && | ||
| !environment.config.server.fs.allow.includes(assetDir) | ||
| ) { | ||
| environment.config.server.fs.allow.push(assetDir) | ||
| } | ||
|
Comment on lines
+335
to
+342
Member
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 guess this part is not needed because it'll be handled by the other internal plugins. |
||
| } | ||
| return { code: s.toString(), map: s.generateMap({ hires: true }) } | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export function urlImportWorker() { | ||
| const worker = new Worker(new URL('./worker.js', import.meta.url), { | ||
| type: 'module', | ||
| }) | ||
| return new Promise((res) => { | ||
| worker.onmessage = (e) => res(e.data) | ||
| }) | ||
| } |
9 changes: 9 additions & 0 deletions
9
playground/optimize-deps/dep-with-worker-repro/nested/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export function urlImportWorker() { | ||
| // Testing the ../ path traversal | ||
| const worker = new Worker(new URL('../worker.js', import.meta.url), { | ||
| type: 'module', | ||
| }) | ||
| return new Promise((res) => { | ||
| worker.onmessage = (e) => res(e.data) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "@vitejs/test-dep-with-worker-repro", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": "./index.js", | ||
| "./nested": "./nested/index.js" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| self.postMessage('worker-success') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the file is bundled by the optimizer, I think we need to resolve the path in
new URL.