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. */ 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,