Open leegeunhyeok opened 12 months ago
Supports hot reload on development environment.
// before var __commonJS = (cb, mod) => function __require2() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; // after global.cachedModule = {}; var __commonJS = (cb, mod) => { var name = __getOwnPropNames(cb)[0]; Object.defineProperty(global.cachedModule, name, { get: () => mod }); return function __require2() { return mod || (0, cb[name])((mod = { exports: {} }).exports, mod), mod.exports; }; }; // when file changes detected -> don't rebuild, just transform target file with swc only. // `import { a, b, c } from 'module-path';` try { const { a, b, c } = global.cachedModule['module-path']; // ... } catch (error) { // hot reload failed. fully reload instead }
// Esbuild metafile interface Metafile { // `inputs` key is module file path. inputs: Record<string, { byte: number; imports: { path: string; // eg. 'node_modules/react/cjs/index.js' original: string; // eg. 'react' kind: '...', }[]; }>[], // ... } // { // react: 'node_modules/react/cjs/index.js', // '../components': 'src/components/index.ts', // } const mappedModules = metafile.inputs[changedFilePath]?.imports.reduce((prev, curr) => { return { ...prev, [curr.original]: curr.path }; }, {}); // Example usage import { transform } from '@swc/core'; await transform(code, { jsc: { experimental: { plugins: [ // https://github.com/leegeunhyeok/swc-plugin-react-native-esbuild-module ['react-native-esbuild-module-plugin', { alias: mappedModules ?? {}, }], ], }, }, });
1st PoC
https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/4ff44d54-57c7-4bbc-942f-4c2fb7b4e4fe
https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/8249589c-ceba-43d9-a8e3-aa9e49d830d2
'../Component'
/path/to/source/Component.tsx
https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/f384f89b-6766-4ad4-8291-d6cc3470e252
Reverse dependencies
Mistake > https://github.com/leegeunhyeok/react-native-esbuild/commit/de55ffb175d66437e18c8075122d94524eefdcb0
sideEffects: false
please bring HMR to this project, it would be crazy
Description
Supports hot reload on development environment.
Tasks
References
Limitations
Notes