From bb64ec9462b5adef141c838e3920de1a26400615 Mon Sep 17 00:00:00 2001 From: Pokai Chang Date: Wed, 25 Dec 2024 20:11:29 +0800 Subject: [PATCH 1/2] remove redundant `treeshake` option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since it’s also set by `reactNativeCommonJsPlugin`. --- packages/vxrn/src/utils/getReactNativeConfig.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vxrn/src/utils/getReactNativeConfig.ts b/packages/vxrn/src/utils/getReactNativeConfig.ts index d335a3e4e..03135be91 100644 --- a/packages/vxrn/src/utils/getReactNativeConfig.ts +++ b/packages/vxrn/src/utils/getReactNativeConfig.ts @@ -253,7 +253,6 @@ export async function getReactNativeConfig( }, rollupOptions: { input: options.entries.native, - treeshake: false, preserveEntrySignatures: 'strict', output: { preserveModules: true, From d796af65467ccc39ef421cdc3bcd2316d7aa431d Mon Sep 17 00:00:00 2001 From: Pokai Chang Date: Wed, 25 Dec 2024 20:47:10 +0800 Subject: [PATCH 2/2] rn: enable tree-shaking for specific modules --- .../src/plugins/reactNativeCommonJsPlugin.ts | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/vxrn/src/plugins/reactNativeCommonJsPlugin.ts b/packages/vxrn/src/plugins/reactNativeCommonJsPlugin.ts index d3abb1c48..78eff82dd 100644 --- a/packages/vxrn/src/plugins/reactNativeCommonJsPlugin.ts +++ b/packages/vxrn/src/plugins/reactNativeCommonJsPlugin.ts @@ -60,7 +60,20 @@ export function reactNativeCommonJsPlugin(options: { reportCompressedSize: false, rollupOptions: { - treeshake: false, + treeshake: { + // Disable treeshaking in general + correctVarValueBeforeDeclaration: false, + propertyReadSideEffects: false, + tryCatchDeoptimization: false, + unknownGlobalSideEffects: false, + annotations: true, + moduleSideEffects: (id, external) => { + // Enable treeshaking for eligible modules + // Set `moduleSideEffects` to true if a module is not tree-shakeable + // The `annotations: true` above seems to be needed to make tree-shakeable modules being tree-shaken + return !isModuleTreeShakeable(id) + }, + }, output: { preserveModules: true, @@ -152,7 +165,7 @@ export function reactNativeCommonJsPlugin(options: { return { code: code + '\n' + forceExports, - moduleSideEffects: 'no-treeshake', + ...(isModuleTreeShakeable(id) ? {} : { moduleSideEffects: 'no-treeshake' }), } } catch (err) { console.warn(`Error forcing exports, probably ok`, id) @@ -256,6 +269,18 @@ function getAllImportedIdentifiers(importStatement: string): string[] { .filter(Boolean) } +/** + * Given a module path, returns whether the module is tree-shakeable. + * For safety, currently we assume that all modules are not tree-shakeable unless we know otherwise. + */ +function isModuleTreeShakeable(modulePath: string) { + if (modulePath.includes('node_modules/react-scan/')) { + return true + } + + return false +} + /** * List of reserved words in JS. From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words. */