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

defualt ignore files config could not exclude node_modules in packing progress #8385

Open
gweesin opened this issue Jul 30, 2024 · 8 comments

Comments

@gweesin
Copy link
Contributor

gweesin commented Jul 30, 2024

  • Electron-Builder Version: latest
  • Node Version: latest
  • Electron Version: latest
  • Electron Type (current, beta, nightly): current
  • Target: my demo project

I found my app.asar is too large, so I extracted it and found that it included fully node_modules

The problem config:

// @see - https://www.electron.build/configuration/configuration
{
    "$schema": "https://raw.githubusercontent.com/electron-userland/electron-builder/master/packages/app-builder-lib/scheme.json",
    "appId": "com.my.demo",
    "asar": true,
    "productName": "demo",
    "directories": {
        "output": "release/${version}"
    },
    "copyright": "Copyright © 2024 ${author}",
    "files": [
        "dist",
        "dist-electron"
    ],
    "extraFiles": [
        {
            from: "../my-custom-path",
            to: "resources/backend"
        }
    ],
    "mac": {
        "target": [
            "dmg"
        ],
        "artifactName": "${productName}-Mac-${version}-Installer.${ext}"
    },
    "win": {
        "publisherName": "demo",
        "target": [
            {
                "target": "nsis",
                "arch": [
                    "x64"
                ]
            }
        ],
        "artifactName": "${productName}-Windows-${version}-Setup.${ext}"
    },
    "nsis": {
        "oneClick": false,
        "perMachine": false,
        "allowToChangeInstallationDirectory": true,
        "deleteAppDataOnUninstall": false
    },
    "linux": {
        "target": [
            "AppImage"
        ],
        "artifactName": "${productName}-Linux-${version}.${ext}"
    }
}

image

expected:

Add node_modules excluding by handle:

// @see - https://www.electron.build/configuration/configuration
{
    "$schema": "https://raw.githubusercontent.com/electron-userland/electron-builder/master/packages/app-builder-lib/scheme.json",
    "appId": "com.my.demo",
    "asar": true,
    "productName": "demo",
    "directories": {
        "output": "release/${version}"
    },
    "copyright": "Copyright © 2024 ${author}",
    "files": [
        "dist",
        "dist-electron",
        "!node_modules" // hope have this configuration by default
    ],
    "extraFiles": [
        {
            from: "../my-custom-path",
            to: "resources/backend"
        }
    ],
    "mac": {
        "target": [
            "dmg"
        ],
        "artifactName": "${productName}-Mac-${version}-Installer.${ext}"
    },
    "win": {
        "publisherName": "demo",
        "target": [
            {
                "target": "nsis",
                "arch": [
                    "x64"
                ]
            }
        ],
        "artifactName": "${productName}-Windows-${version}-Setup.${ext}"
    },
    "nsis": {
        "oneClick": false,
        "perMachine": false,
        "allowToChangeInstallationDirectory": true,
        "deleteAppDataOnUninstall": false
    },
    "linux": {
        "target": [
            "AppImage"
        ],
        "artifactName": "${productName}-Linux-${version}.${ext}"
    }
}

image

@beyondkmp
Copy link
Collaborator

The asar file needs to include node_modules, but it will only include the dependencies listed in your project's package.json and their dependencies. If you directly filter out all of node_modules, the code may not run unless you've bundled all the dependency code into the dist folder.

@gweesin
Copy link
Contributor Author

gweesin commented Jul 30, 2024

Could I consider that all projects need to be bundled? Otherwise the app will contain the electron package in node_modules, it's about 256MB and it's duplicated and not used.

@xyloflake
Copy link
Contributor

xyloflake commented Jul 30, 2024

Could I consider that all projects need to be bundled? Otherwise the app will contain the electron package in node_modules, it's about 256MB and it's duplicated and not used.

If you want the size to be reduced, I'd recommend removing the unnecessary locales for electron.

Here's how I patched electron-builder to only include the English locale unless specified otherwise. This resulted in a 38MB unpackaged app size decrease.

staniel359/muffon#173

@xyloflake
Copy link
Contributor

xyloflake commented Jul 30, 2024

Otherwise the app will contain the electron package in node_modules, it's about 256MB and it's duplicated and not used.

Electron package in node modules is NOT a duplicate. It provides bindings to call the electron application through js. If you remove them, you won't be able to call the ABI methods through JS like you do. For example creating a new BrowserWindow.

The other electron package you're referring to provides the ABI through which the node js electron package can call methods that have been defined in the ABI.

@mmaietta
Copy link
Collaborator

If you want the size to be reduced, I'd recommend removing the unnecessary locales for electron.

This should already be supported via electronLanguages string array 🙂
https://www.electron.build/configuration/configuration

@gweesin
Copy link
Contributor Author

gweesin commented Aug 13, 2024

I have tried ignore node_modules directly, nothing happened.
and try to configure electronLanguages, then the package bundle size reduce.

@iongion
Copy link

iongion commented Sep 5, 2024

I am trying to exclude the entire node_modules directory and can't find any way - I am indeed bundling all dependencies myself

  files: [
    // What to copy
    "build",
    "LICENSE",
    // What to exclude
    "!**/node_modules/*/@(CHANGELOG.md|README.md|README|readme.md|readme)",
    "!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",
    "!**/node_modules/*/{test,__tests__,tests,powered-test,example,examples}",
    "!**/node_modules/*.d.ts",
    "!**/node_modules/.bin",
    "!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}",
    "!.editorconfig",
    "!**/._*",
    "!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,.gitignore,.gitattributes}",
    "!**/{__pycache__,thumbs.db,.flowconfig,.idea,.vs,.nyc_output}",
    "!**/{appveyor.yml,.travis.yml,circle.yml}",
    "!**/{npm-debug.log,yarn.lock,.yarn-integrity,.yarn-metadata.json}",
    "!**/node_modules/*",
    "!**/node_modules/**/*",
    "!node_modules/*",
    "!node_modules/**/*"
  ],

Even after this, my asar file contains things like babel transpiler.

Is there no way to completely avoid node_modules from being bundled in the asar ?

@mmaietta
Copy link
Collaborator

mmaietta commented Sep 5, 2024

This was my config when I was bundling all dependencies as part of the app files and was successfully able to exclude node_modules. Does it happen to work for you?

    files: [
        "out",
        "!node_modules",
        "!**/*.node",
        "!**/*.map"
    ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants