-
-
Notifications
You must be signed in to change notification settings - Fork 213
refactor: extract common code in common package #443
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
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
707fd5d
refactor: extract refresh runtime in swc plugin in common package
sapphi-red 20d67ed
refactor: use common refresh runtime in babel plugin
sapphi-red c78fd21
refactor: extract preamble to common package
sapphi-red 2ea890a
refactor: extract refresh wrapper to common package
sapphi-red d144462
refactor: extract silenceUseClientWarning to common package
sapphi-red e31f225
fix: don't parse source map if not needed
sapphi-red 8d56222
refactor: don't expose getPreambleCode
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './refresh-utils' | ||
| export * from './warning' |
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,16 @@ | ||
| { | ||
| "name": "@vitejs/react-common", | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "private": true, | ||
| "exports": { | ||
| ".": "./index.ts", | ||
| "./refresh-runtime": "./refresh-runtime.js" | ||
| }, | ||
| "peerDependencies": { | ||
| "vite": "^4.2.0 || ^5.0.0 || ^6.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "vite": "^6.2.2" | ||
| } | ||
| } |
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,84 @@ | ||
| export const runtimePublicPath = '/@react-refresh' | ||
|
|
||
| const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/ | ||
| const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/ | ||
|
|
||
| // NOTE: this is currently exposed publicly via plugin-react | ||
| export const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice( | ||
| 1, | ||
| )}" | ||
| injectIntoGlobalHook(window); | ||
| window.$RefreshReg$ = () => {}; | ||
| window.$RefreshSig$ = () => (type) => type;` | ||
|
|
||
| // NOTE: this is exposed publicly via plugin-react | ||
| export const getPreambleCode = (base: string): string => | ||
| preambleCode.replace('__BASE__', base) | ||
|
|
||
| export function addRefreshWrapper<M extends { mappings: string } | undefined>( | ||
| code: string, | ||
| map: M | string, | ||
| pluginName: string, | ||
| id: string, | ||
| ): { code: string; map: M | string } { | ||
| const hasRefresh = refreshContentRE.test(code) | ||
| const onlyReactComp = !hasRefresh && reactCompRE.test(code) | ||
| if (!hasRefresh && !onlyReactComp) return { code, map } | ||
|
|
||
| const newMap = typeof map === 'string' ? (JSON.parse(map) as M) : map | ||
| let newCode = code | ||
| if (hasRefresh) { | ||
| newCode = `let prevRefreshReg; | ||
| let prevRefreshSig; | ||
|
|
||
| if (import.meta.hot && !inWebWorker) { | ||
| if (!window.$RefreshReg$) { | ||
| throw new Error( | ||
| "${pluginName} can't detect preamble. Something is wrong." | ||
| ); | ||
| } | ||
|
|
||
| prevRefreshReg = window.$RefreshReg$; | ||
| prevRefreshSig = window.$RefreshSig$; | ||
| window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)}); | ||
| window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform; | ||
| } | ||
|
|
||
| ${newCode} | ||
|
|
||
| if (import.meta.hot && !inWebWorker) { | ||
| window.$RefreshReg$ = prevRefreshReg; | ||
| window.$RefreshSig$ = prevRefreshSig; | ||
| } | ||
| ` | ||
| if (newMap) { | ||
| newMap.mappings = ';'.repeat(17) + newMap.mappings | ||
| } | ||
| } | ||
|
|
||
| newCode = `import * as RefreshRuntime from "${runtimePublicPath}"; | ||
| const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; | ||
|
|
||
| ${newCode} | ||
|
|
||
| if (import.meta.hot && !inWebWorker) { | ||
| RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => { | ||
| RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify( | ||
| id, | ||
| )}, currentExports); | ||
| import.meta.hot.accept((nextExports) => { | ||
| if (!nextExports) return; | ||
| const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify( | ||
| id, | ||
| )}, currentExports, nextExports); | ||
| if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage); | ||
| }); | ||
| }); | ||
| } | ||
| ` | ||
| if (newMap) { | ||
| newMap.mappings = ';;;' + newMap.mappings | ||
| } | ||
|
|
||
| return { code: newCode, map: newMap } | ||
| } |
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,29 @@ | ||
| import type { BuildOptions, UserConfig } from 'vite' | ||
|
|
||
| export const silenceUseClientWarning = ( | ||
| userConfig: UserConfig, | ||
| ): BuildOptions => ({ | ||
| rollupOptions: { | ||
| onwarn(warning, defaultHandler) { | ||
| if ( | ||
| warning.code === 'MODULE_LEVEL_DIRECTIVE' && | ||
| warning.message.includes('use client') | ||
| ) { | ||
| return | ||
| } | ||
| // https://github.com/vitejs/vite/issues/15012 | ||
| if ( | ||
| warning.code === 'SOURCEMAP_ERROR' && | ||
| warning.message.includes('resolve original location') && | ||
| warning.pos === 0 | ||
| ) { | ||
| return | ||
| } | ||
| if (userConfig.build?.rollupOptions?.onwarn) { | ||
| userConfig.build.rollupOptions.onwarn(warning, defaultHandler) | ||
| } else { | ||
| defaultHandler(warning) | ||
| } | ||
| }, | ||
| }, | ||
| }) |
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
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,6 @@ | ||
| import { copyFileSync } from 'node:fs' | ||
|
|
||
| copyFileSync( | ||
| 'node_modules/@vitejs/react-common/refresh-runtime.js', | ||
| 'dist/refresh-runtime.js', | ||
| ) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.