Skip to content

Commit

Permalink
feat: allow onNodeModuleFile to return a boolean to force include t…
Browse files Browse the repository at this point in the history
…he package to be copied (#8043)
  • Loading branch information
mmaietta authored Feb 9, 2024
1 parent 63a0044 commit bb4a8c0
Show file tree
Hide file tree
Showing 8 changed files with 3,707 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-pumas-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": minor
---

feat: allow `onNodeModuleFile` to return a boolean to force include the package to be copied
2 changes: 1 addition & 1 deletion docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Env file `electron-builder.env` in the current dir ([example](https://github.com
<p><code id="Configuration-appxManifestCreated">appxManifestCreated</code> module:app-builder-lib/out/configuration.__type | String | “undefined” - Appx manifest created on disk - not packed into .appx package yet.</p>
</li>
<li>
<p><code id="Configuration-onNodeModuleFile">onNodeModuleFile</code> - The function (or path to file or module id) to be <a href="#onnodemodulefile">run on each node module</a> file.</p>
<p><code id="Configuration-onNodeModuleFile">onNodeModuleFile</code> - The function (or path to file or module id) to be <a href="#onnodemodulefile">run on each node module</a> file. Returning <code>true</code>/<code>false</code> will determine whether to force include or to use the default copier logic</p>
</li>
<li>
<p><code id="Configuration-beforeBuild">beforeBuild</code> (context: BeforeBuildContext) =&gt; Promise | null - The function (or path to file or module id) to be run before dependencies are installed or rebuilt. Works when <code>npmRebuild</code> is set to <code>true</code>. Resolving to <code>false</code> will skip dependencies install or rebuild.</p>
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/scheme.json
Original file line number Diff line number Diff line change
Expand Up @@ -7529,7 +7529,7 @@
]
}
],
"description": "The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file."
"description": "The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file. Returning `true`/`false` will determine whether to force include or to use the default copier logic"
},
"p5p": {
"anyOf": [
Expand Down
4 changes: 2 additions & 2 deletions packages/app-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ export interface Configuration extends PlatformSpecificBuildOptions {
*/
readonly appxManifestCreated?: ((path: string) => Promise<any> | any) | string | null
/**
* The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file.
* The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file. Returning `true`/`false` will determine whether to force include or to use the default copier logic
*/
readonly onNodeModuleFile?: ((file: string) => void) | string | null
readonly onNodeModuleFile?: ((path: string) => void | boolean) | string | null
/**
* The function (or path to file or module id) to be run before dependencies are installed or rebuilt. Works when `npmRebuild` is set to `true`. Resolving to `false` will skip dependencies install or rebuild.
*
Expand Down
43 changes: 22 additions & 21 deletions packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const topLevelExcludedFiles = new Set([
"Readme",
"readme",
"test",
"__tests__",
"tests",
"__tests__",
"powered-test",
"example",
"examples",
Expand Down Expand Up @@ -64,38 +64,39 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
const sortedFilePaths = await BluebirdPromise.map(
childNames,
name => {
if (onNodeModuleFile != null) {
onNodeModuleFile(dirPath + path.sep + name)
}
const filePath = dirPath + path.sep + name

const forceIncluded = onNodeModuleFile != null && !!onNodeModuleFile(filePath)

if (excludedFiles.has(name) || name.startsWith("._")) {
return null
}

for (const ext of nodeModuleExcludedExts) {
if (name.endsWith(ext)) {
return null
if (!forceIncluded) {
for (const ext of nodeModuleExcludedExts) {
if (name.endsWith(ext)) {
return null
}
}
}

// noinspection SpellCheckingInspection
if (isTopLevel && (topLevelExcludedFiles.has(name) || (moduleName === "libui-node" && (name === "build" || name === "docs" || name === "src")))) {
return null
}
// noinspection SpellCheckingInspection
if (isTopLevel && (topLevelExcludedFiles.has(name) || (moduleName === "libui-node" && (name === "build" || name === "docs" || name === "src")))) {
return null
}

if (dirPath.endsWith("build")) {
if (name === "gyp-mac-tool" || name === "Makefile" || name.endsWith(".mk") || name.endsWith(".gypi") || name.endsWith(".Makefile")) {
if (dirPath.endsWith("build")) {
if (name === "gyp-mac-tool" || name === "Makefile" || name.endsWith(".mk") || name.endsWith(".gypi") || name.endsWith(".Makefile")) {
return null
}
} else if (dirPath.endsWith("Release") && (name === ".deps" || name === "obj.target")) {
return null
} else if (name === "src" && (dirPath.endsWith("keytar") || dirPath.endsWith("keytar-prebuild"))) {
return null
} else if (dirPath.endsWith("lzma-native") && (name === "build" || name === "deps")) {
return null
}
} else if (dirPath.endsWith("Release") && (name === ".deps" || name === "obj.target")) {
return null
} else if (name === "src" && (dirPath.endsWith("keytar") || dirPath.endsWith("keytar-prebuild"))) {
return null
} else if (dirPath.endsWith("lzma-native") && (name === "build" || name === "deps")) {
return null
}

const filePath = dirPath + path.sep + name
return lstat(filePath).then(stat => {
if (filter != null && !filter(filePath, stat)) {
return null
Expand Down
Loading

0 comments on commit bb4a8c0

Please sign in to comment.