-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
fix: dynamic import assets in inline worker #17859
base: main
Are you sure you want to change the base?
fix: dynamic import assets in inline worker #17859
Conversation
Run & review this pull request in StackBlitz Codeflow. |
areFilesEqual(extractedUrl, url), | ||
) | ||
if (importUrl) { | ||
return `import(self.location.origin + "${config.rawBase + importUrl}")` |
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 approach might have an issue with relative base. I'm checking pnpm -C playground/worker build:relative-base
+ preview:relative-base
and I see this code in the inline worker:
import(self.location.origin + "./worker-chunks/worker_chunk-vite-D2sZgpE4.js")
// TypeError: Failed to resolve module specifier 'http://localhost:4173./worker-chunks/worker_chunk-index-JyeH1t5q.js'
I'm not sure if there's any mechanism possible to actually handle this though. (Maybe main script needs to tell workers where the main script is and compute relative path based on that?)
Also base
can be a full url to cdn https://vitejs.dev/config/shared-options.html#base, so that might also break self.location.origin + config.rawBase
.
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.
Can this be solved in this way? Use config.build.outDir to extract the path we need, if outDir = "dist" or './dist' use origin + / + url, if outDir = 'dist/es/' use origin + /es/.
return `import(self.location.origin + "${config.rawBase + importUrl}")` | |
let outDir = config.build.outDir | |
if (outDir.startsWith('./')) { | |
outDir = outDir.slice(2) // Removes the './' | |
} | |
// Filter out the first folder | |
let basePath = outDir | |
.split('/') | |
.filter((path) => path) | |
.slice(1) | |
.join('/') | |
if (basePath === '') { | |
basePath = '/' | |
} else { | |
basePath = `/${basePath}/` | |
} |
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.
I don't think that's how relative base should be handled. Relative base means it's relocatable and shouldn't hard-code /dist
in the output.
For example, for this playground, simple http server like sirv should be able to serve the app from an arbitrary root directory.
pnpm -C playground/worker build:relative-base
# app available in http://localhost:8080
pnpm dlx sirv-cli playground/worker/dist/relative-base
# app available in http://localhost:8080/relative-base
pnpm dlx sirv-cli playground/worker/dist
# app available in http://localhost:8080/dist/relative-base
pnpm dlx sirv-cli playground/worker
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.
You're right, we can't get the full base path, maybe set it up from the main script.
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.
I think this is a tricky problem to solve in a general manner. For example, I think it's technically possible to have an inline worker inside inline worker, but I would imagine supporting that would cause more complication.
We need a discussion with a team, but it might be acceptable, for example, to limit the use case only for non relative base case to simplify things.
Either way, thanks for initiating a PR 👍
fix #17825
After using inline, the code will be converted to base64 encoding, so that the relative path in the worker file cannot get the corresponding actual file path