Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leegeunhyeok committed Nov 28, 2023
1 parent 081fe18 commit fd076fb
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 34 deletions.
Binary file not shown.
Binary file not shown.
8 changes: 6 additions & 2 deletions packages/core/lib/bundler/helpers/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export const getHmrRuntimeTransformer = (
root: string,
config: Config,
bundleOptions: BundleOptions,
): ((code: string, path: string) => Promise<string>) => {
): ((
code: string,
path: string,
importPaths: Record<string, string>,
) => Promise<string>) => {
const {
stripFlowPackageNames = [],
fullyTransformPackageNames = [],
Expand Down Expand Up @@ -46,7 +50,7 @@ export const getHmrRuntimeTransformer = (
})
.build();

return (code: string, path: string) =>
return (code: string, path: string, _importPaths: Record<string, string>) =>
transformPipeline
.transform(code, { ...DUMMY_ESBUILD_ARGS, path })
.then((result) => result.code);
Expand Down
39 changes: 35 additions & 4 deletions packages/hmr/lib/HmrTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import fs from 'node:fs/promises';
import type { Metafile } from 'esbuild';
import { getReloadByDevSettingsProxy } from '@react-native-esbuild/internal';
import { logger } from './shared';
import type { BundleMeta, BundleUpdate } from './types';
import type { BundleMeta, BundleUpdate, ModuleInfo } from './types';

export class HmrTransformer {
constructor(
private root: string,
private transformer: (code: string, path: string) => Promise<string>,
private transformer: (
code: string,
path: string,
modulePaths: Record<string, string>,
) => Promise<string>,
) {}

/**
Expand Down Expand Up @@ -70,22 +74,49 @@ export class HmrTransformer {
modulePath: string,
bundleMeta: BundleMeta,
): Promise<string> {
const strippedModulePath = this.stripRoot(modulePath);
const reverseDependencies = this.getReverseDependencies(
this.stripRoot(modulePath),
strippedModulePath,
bundleMeta,
);

const transformedCodes = await Promise.all(
[modulePath, ...reverseDependencies].map(async (modulePath) => {
const rawCode = await fs.readFile(modulePath, 'utf-8');
const code = await this.transformer(rawCode, modulePath);
const importPaths = this.getActualImportPaths(modulePath, bundleMeta);
const code = await this.transformer(rawCode, modulePath, importPaths);
return code;
}),
);

return transformedCodes.join('\n');
}

private getTargetModuleInfo(
targetModule: string,
bundleMeta: BundleMeta,
): ModuleInfo {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be `undefined`
if (!bundleMeta.inputs[targetModule]) {
throw new Error(`[HMR] ${targetModule} not found in bundle meta`);
}
return bundleMeta.inputs[targetModule];
}

private getActualImportPaths(
targetModule: string,
bundleMeta: BundleMeta,
): Record<string, string> {
const moduleInfo = this.getTargetModuleInfo(targetModule, bundleMeta);
return moduleInfo.imports.reduce(
(prev, curr) => ({
...prev,
...(curr.original ? { [curr.original]: curr.path } : null),
}),
{} as Record<string, string>,
);
}

private getReverseDependencies(
targetModule: string,
bundleMeta: BundleMeta,
Expand Down
29 changes: 14 additions & 15 deletions packages/hmr/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,22 @@ export interface HmrModule {
}

export type BundleMeta = Metafile & {
inputs: Metafile['inputs'] &
Record<
string,
{
bytes: number;
imports: {
path: string;
kind: ImportKind;
external?: boolean;
original?: string;
}[];
parents?: Set<string>;
format?: 'cjs' | 'esm';
}
>;
inputs: Metafile['inputs'] & Record<string, ModuleInfo>;
};

// Extended type from `esbuild.Metafile['inputs'][file]`
export interface ModuleInfo {
bytes: number;
imports: {
path: string;
kind: ImportKind;
external?: boolean;
original?: string;
}[];
parents?: Set<string>;
format?: 'cjs' | 'esm';
}

export interface BundleUpdate {
id: string;
path: string;
Expand Down
9 changes: 2 additions & 7 deletions packages/transformer/lib/transformer/swc/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const getReactNativeRuntimePreset = (): TransformerOptionsPreset<Options> => {
experimental: {
plugins: [
[
'/Users/ghlee/workspace/swc-plugin-global-esm',
'swc-plugin-global-esm',
{
runtimeModule: false,
},
Expand Down Expand Up @@ -122,9 +122,4 @@ const getMinifyPreset = () => {
});
};

export {
getReactNativeRuntimePreset,
getReactNativeRuntimeHMRPreset,
getJestPreset,
getMinifyPreset,
};
export { getReactNativeRuntimePreset, getJestPreset, getMinifyPreset };
2 changes: 1 addition & 1 deletion packages/transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"md5": "^2.3.0",
"sucrase": "^3.34.0",
"swc-plugin-coverage-instrument": "^0.0.20",
"swc-plugin-global-esm": "0.1.0-alpha.2",
"swc-plugin-global-esm": "^0.1.0-alpha.2",
"swc_mut_cjs_exports": "^0.85.0"
}
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4463,7 +4463,7 @@ __metadata:
md5: ^2.3.0
sucrase: ^3.34.0
swc-plugin-coverage-instrument: ^0.0.20
swc-plugin-global-esm: 0.1.0-alpha.2
swc-plugin-global-esm: ^0.1.0-alpha.2
swc_mut_cjs_exports: ^0.85.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -20504,10 +20504,10 @@ __metadata:
languageName: node
linkType: hard

"swc-plugin-global-esm@npm:0.1.0-alpha.2":
version: 0.1.0-alpha.2
resolution: "swc-plugin-global-esm@npm:0.1.0-alpha.2"
checksum: 2e6740025c15d00afaf041067a0e5ec0203b59c80d7589e907355fb6b841d0aa4345b01a2579f87572597d7fba3d7c672fd57df85835df41888ce802df64b67b
"swc-plugin-global-esm@npm:^0.1.0-alpha.2":
version: 0.1.0-alpha.3
resolution: "swc-plugin-global-esm@npm:0.1.0-alpha.3"
checksum: 203558f79402241fd942949a989606277279cfff5e0acacb572d0aa9eae1927deab00b304651b214f7f27d38c149ccc95dc829c03a5a3ed9ae84311b39b12c07
languageName: node
linkType: hard

Expand Down

1 comment on commit fd076fb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🔴 Statements 15.04% 328/2181
🔴 Branches 14.49% 82/566
🔴 Functions 9.94% 65/654
🔴 Lines 14.38% 298/2072

Test suite run success

83 tests passing in 10 suites.

Report generated by 🧪jest coverage report action from fd076fb

Please sign in to comment.