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

fix: dynamic import assets in inline worker #17859

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

proc07
Copy link
Contributor

@proc07 proc07 commented Aug 10, 2024

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

Copy link

stackblitz bot commented Aug 10, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@proc07 proc07 closed this Aug 11, 2024
@proc07 proc07 reopened this Aug 11, 2024
areFilesEqual(extractedUrl, url),
)
if (importUrl) {
return `import(self.location.origin + "${config.rawBase + importUrl}")`
Copy link
Collaborator

@hi-ogawa hi-ogawa Aug 12, 2024

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.

Copy link
Contributor Author

@proc07 proc07 Aug 12, 2024

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/.

Suggested change
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}/`
}

Copy link
Collaborator

@hi-ogawa hi-ogawa Aug 12, 2024

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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 👍

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.

Dynamic imports don't work from inlined web worker
2 participants