-
-
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(worker): using data URLs for inline shared worker #12014
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e03cc1a
feat(worker): support inlineUrl of worker config
fi3ework ccc8fd5
refactor: change 'data' to 'base64'
fi3ework 6309d34
fix: separate blob and base64 code
fi3ework 3e12174
fix: correct shared worker test
fi3ework c5164d2
test: add more
fi3ework edfb5a8
chore: remove inlineUrl
fi3ework 0991090
fix: try catch createObjectURL fail
fi3ework a1fd676
refactor: clean code
fi3ework 68a612d
Update packages/vite/src/node/plugins/worker.ts
fi3ework ec3dc32
chore: update test
sapphi-red 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
Next
Next commit
feat(worker): support inlineUrl of worker config
- Loading branch information
commit e03cc1a55572459b474fd21af91dbd5e6cbc29ec
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -278,7 +278,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { | |
|
||
// stringified url or `new URL(...)` | ||
let url: string | ||
const { format } = config.worker | ||
const { format, inlineUrl } = config.worker | ||
const workerConstructor = | ||
query.sharedworker != null ? 'SharedWorker' : 'Worker' | ||
const workerType = isBuild | ||
|
@@ -300,7 +300,9 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { | |
export default function WorkerWrapper() { | ||
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob); | ||
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. if setting 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. updated. Separate the blob and base64 code to make it more readable for human. |
||
try { | ||
return objURL ? new ${workerConstructor}(objURL) : new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions}); | ||
return objURL && ${ | ||
inlineUrl === 'blob' | ||
} ? new ${workerConstructor}(objURL) : new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions}); | ||
} finally { | ||
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL); | ||
} | ||
|
This file contains 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
19 changes: 19 additions & 0 deletions
19
playground/worker/__tests__/inline-url/inline-url-worker.spec.ts
This file contains 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,19 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { describe, expect, test } from 'vitest' | ||
import { isBuild, testDir } from '~utils' | ||
|
||
describe.runIf(isBuild)('build', () => { | ||
// assert correct files | ||
test('inlined code generation', async () => { | ||
const assetsDir = path.resolve(testDir, 'dist/inline-url/assets') | ||
const files = fs.readdirSync(assetsDir) | ||
const index = files.find((f) => f.includes('main-module')) | ||
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8') | ||
|
||
// inlined | ||
expect(content).toMatch( | ||
`;try{return new Worker("data:application/javascript;base64,"+`, | ||
) | ||
}) | ||
}) |
This file contains 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 @@ | ||
module.exports = require('../../vite.config-inline-url') |
This file contains 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 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,5 @@ | ||
const config = require('./vite.config-es') | ||
config.worker.inlineUrl = 'data' | ||
config.base = '/inline-url/' | ||
config.build.outDir = 'dist/inline-url' | ||
module.exports = config |
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.
It better to rename by
base64
?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.
Good naming, updated.