From 7487c0dc2e71edecfc532add0f210605e71456b9 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Wed, 2 Oct 2024 08:29:52 +0800 Subject: [PATCH] fix: fix the main matcher patterns for !node_modules/@test/xxxx (#8547) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix https://github.com/electron-userland/electron-builder/issues/8460 Reproducible demo ``` { "name": "TestApp", "productName": "Test App ßW", "description": "My Electron application description", "keywords": [], "main": "./main.js", "version": "1.0.0", "author": "beyondkmp", "scripts": { "start": "electron .", "dist": "electron-builder" }, "license": "MIT", "build": { "appId": "electron-blog-example", "files": [ "!node_modules/@electron/remote/*" ], "win": { "target": "nsis" } }, "devDependencies": { "electron": "32.1.0", "electron-builder": "25.1.6" }, "dependencies": { "tar": "7.4.3", "@electron/remote": "2.1.2" } } ``` --------- Co-authored-by: beyondkmp --- .changeset/three-fans-worry.md | 5 ++++ .../app-builder-lib/src/util/appFileCopier.ts | 24 +++++++++++++------ test/src/globTest.ts | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 .changeset/three-fans-worry.md diff --git a/.changeset/three-fans-worry.md b/.changeset/three-fans-worry.md new file mode 100644 index 00000000000..9500fea14bb --- /dev/null +++ b/.changeset/three-fans-worry.md @@ -0,0 +1,5 @@ +--- +"app-builder-lib": patch +--- + +fix the main matcher patterns for !node_modules/xxxx diff --git a/packages/app-builder-lib/src/util/appFileCopier.ts b/packages/app-builder-lib/src/util/appFileCopier.ts index 93a4a645693..cd3b8f82247 100644 --- a/packages/app-builder-lib/src/util/appFileCopier.ts +++ b/packages/app-builder-lib/src/util/appFileCopier.ts @@ -188,32 +188,42 @@ export async function computeNodeModuleFileSets(platformPackager: PlatformPackag const result = new Array() let index = 0 const NODE_MODULES = "node_modules" - const getRealSource = (source: string) => { - const parentDir = path.dirname(source) + const getRealSource = (name: string, source: string) => { + const normalizedName = name.split("/").join(path.sep) + if (!source.endsWith(normalizedName)) { + throw new Error("Path does not end with the package name") + } + + // get the parent dir of the package, input: /root/path/node_modules/@electron/remote, output: /root/path/node_modules + const parentDir = source.slice(0, -normalizedName.length - 1) + // for the local node modules which is not in node modules if (!parentDir.endsWith(path.sep + NODE_MODULES)) { return parentDir } - // use main matcher patterns, so, user can exclude some files !node_modules/xxxx + + // use main matcher patterns,return parent dir of the node_modules, so user can exclude some files !node_modules/xxxx return path.dirname(parentDir) } - const collectNodeModules = async (dep: NodeModuleInfo, destination: string) => { + + const collectNodeModules = async (dep: NodeModuleInfo, realSource: string, destination: string) => { const source = dep.dir - const matcher = new FileMatcher(getRealSource(source), destination, mainMatcher.macroExpander, mainMatcher.patterns) + const matcher = new FileMatcher(realSource, destination, mainMatcher.macroExpander, mainMatcher.patterns) const copier = new NodeModuleCopyHelper(matcher, platformPackager.info) const files = await copier.collectNodeModules(dep, nodeModuleExcludedExts) result[index++] = validateFileSet({ src: source, destination, files, metadata: copier.metadata }) if (dep.conflictDependency) { for (const c of dep.conflictDependency) { - await collectNodeModules(c, path.join(destination, NODE_MODULES, c.name)) + await collectNodeModules(c, realSource, path.join(destination, NODE_MODULES, c.name)) } } } for (const dep of deps) { const destination = path.join(mainMatcher.to, NODE_MODULES, dep.name) - await collectNodeModules(dep, destination) + const realSource = getRealSource(dep.name, dep.dir) + await collectNodeModules(dep, realSource, destination) } return result diff --git a/test/src/globTest.ts b/test/src/globTest.ts index f7abca6f329..613834277c2 100644 --- a/test/src/globTest.ts +++ b/test/src/globTest.ts @@ -168,6 +168,7 @@ test.ifAll.ifDevOrLinuxCi("ignore node_modules", () => { //noinspection SpellCheckingInspection data.dependencies = { "ci-info": "2.0.0", + "@electron/remote": "2.1.2", } }), packed: context => {