-
Notifications
You must be signed in to change notification settings - Fork 374
[ Vite ] Regroup preserve-*-loaders-imports Vite plugin inside a vite-extension
#3002
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
Merged
+163
−130
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ed1a44
Regroup preserve loaders imports Vite plugin inside a vite-extension
mho22 921dfb0
Lint PHP.wasm Web vite config file
mho22 737eec8
Add a verification step in the extension and comments
mho22 120ae07
Lint PHP.wasm Web vite config file
mho22 80e28d8
Lint PHP.wasm Web vite config file
mho22 4787ad3
Correct and improve comments
mho22 275c784
Rename the plugin, update docstrings
adamziel 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
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
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,78 @@ | ||
| import type { Plugin } from 'vite'; | ||
|
|
||
| export interface PreserveLoadersRule { | ||
| regex: RegExp; | ||
| transform: (specifier: string) => string; | ||
| } | ||
|
|
||
| export function vitePreserveLoadersImports( | ||
| rules: PreserveLoadersRule[] | ||
| ): Plugin { | ||
| let command: 'build' | 'serve'; | ||
|
|
||
| const matchedRules = new Set<PreserveLoadersRule>(); | ||
|
|
||
| return { | ||
| /** | ||
| * Vite can't extract static asset in the library mode: | ||
| * https://github.com/vitejs/vite/issues/3295 | ||
| * | ||
| * This workaround replaces the actual php.js modules paths used | ||
| * in the dev mode with their filenames. Then, the filenames are marked | ||
| * as external further down in this config. As a result, the final | ||
| * bundle contains literal `import('php.js')` and | ||
| * `import('php.wasm')` statements which allows the consumers to use | ||
| * their own loaders. | ||
| * | ||
| * This keeps the dev mode working AND avoids inlining 5mb of | ||
| * wasm via base64 in the final bundle. | ||
| */ | ||
| name: 'vite-preserve-loaders-imports', | ||
|
|
||
| configResolved(config) { | ||
| command = config.command; | ||
| }, | ||
|
|
||
| resolveDynamicImport(specifier) { | ||
| if (command !== 'build' || typeof specifier !== 'string') return; | ||
|
|
||
| for (const rule of rules) { | ||
| if (new RegExp(rule.regex).test(specifier)) { | ||
| matchedRules.add(rule); | ||
| /** | ||
| * | ||
| * Example : transform: specifier => `../${specifier.split('/').slice(-3).join('/')}` | ||
| * | ||
| * The '../' is weird but necessary to make the final build say | ||
| * import("./php/jspi/php.js") | ||
| * and not | ||
| * import("php/jspi/php.js") | ||
| * | ||
| * The -3 will ensure the 'php/jspi/' | ||
| * portion of the path is preserved. | ||
| */ | ||
| return rule.transform(specifier); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }, | ||
|
|
||
| buildEnd() { | ||
| if (command !== 'build') return; | ||
|
|
||
| const unusedRules = rules.filter((rule) => !matchedRules.has(rule)); | ||
|
|
||
| if (unusedRules.length > 0) { | ||
| const details = unusedRules | ||
| .map((rule) => `- ${rule.regex}`) | ||
| .join('\n'); | ||
|
|
||
| this.error( | ||
| `vite-preserve-loaders-imports: The following rules did not match any dynamic imports:\n${details}\n\n` + | ||
| `This is likely a misconfiguration or a stale regex.` | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.