-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c250fad
commit cb9a38d
Showing
13 changed files
with
273 additions
and
88 deletions.
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 |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import { | ||
getModuleCodeFromBundle, | ||
isReactRefreshRegistered, | ||
} from '@react-native-esbuild/hmr'; | ||
import type { UpdatedModule } from '../../types'; | ||
AsyncTransformPipeline, | ||
swcPresets, | ||
} from '@react-native-esbuild/transformer'; | ||
import type { BundleOptions } from '@react-native-esbuild/config'; | ||
import type { Config } from '../../types'; | ||
|
||
export const getHmrUpdatedModule = ( | ||
id: string | null, | ||
path: string | null, | ||
bundleCode: string, | ||
): UpdatedModule | null => { | ||
const updatedCode = | ||
id && path ? getModuleCodeFromBundle(bundleCode, id) : null; | ||
const DUMMY_ESBUILD_ARGS = { | ||
namespace: '', | ||
suffix: '', | ||
pluginData: undefined, | ||
} as const; | ||
|
||
return updatedCode | ||
? { | ||
code: updatedCode, | ||
id: id ?? '', | ||
path: path ?? '', | ||
mode: isReactRefreshRegistered(updatedCode) | ||
? 'hot-reload' | ||
: 'full-reload', | ||
} | ||
: null; | ||
export const getHmrRuntimeTransformer = ( | ||
id: number, | ||
root: string, | ||
config: Config, | ||
bundleOptions: BundleOptions, | ||
): ((code: string, path: string) => Promise<string>) => { | ||
const { | ||
stripFlowPackageNames = [], | ||
fullyTransformPackageNames = [], | ||
additionalTransformRules, | ||
} = config.transformer ?? {}; | ||
const additionalBabelRules = additionalTransformRules?.babel ?? []; | ||
const additionalSwcRules = additionalTransformRules?.swc ?? []; | ||
|
||
const transformPipeline = new AsyncTransformPipeline.builder({ | ||
...bundleOptions, | ||
id, | ||
root, | ||
}) | ||
.setSwcPreset(swcPresets.getReactNativeRuntimePreset()) | ||
.setFullyTransformPackages(fullyTransformPackageNames) | ||
.setStripFlowPackages(stripFlowPackageNames) | ||
.setAdditionalBabelTransformRules(additionalBabelRules) | ||
.setAdditionalSwcTransformRules(additionalSwcRules) | ||
.build(); | ||
|
||
return (code: string, path: string) => { | ||
return transformPipeline | ||
.transform(code, { | ||
...DUMMY_ESBUILD_ARGS, | ||
path, | ||
}) | ||
.then((result) => result.code); | ||
}; | ||
}; |
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
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
Oops, something went wrong.