Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree-shaking for RN #340

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/vxrn/src/plugins/reactNativeCommonJsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
*/
Expand Down
1 change: 0 additions & 1 deletion packages/vxrn/src/utils/getReactNativeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ export async function getReactNativeConfig(
},
rollupOptions: {
input: options.entries.native,
treeshake: false,
preserveEntrySignatures: 'strict',
output: {
preserveModules: true,
Expand Down
Loading