From ef375127a8e1b7a685e0c432099505e99c67face Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCner?= Date: Fri, 15 Sep 2023 14:06:31 +0200 Subject: [PATCH] feat(bob): add exclude option to config (#450) ### Summary This adds the possibility to pass custom ignore patterns while working with builder bob. ### Test plan 1. Create a new library using `create-react-native-library`. You can select `Native module`, and `Kotlin & Swift` options. 2. Go to `package.json`, and find the `react-native-builder-bob` section 3. Replace `commonjs` target as following: ```json { "exclude": "**/ignore_me/**" "targets": [ "commonjs", "module", [ "typescript", { "project": "tsconfig.build.json" } ] ] } ``` 4. Create a new folder named `src/ignore_me` and put `.ts` files inside it 5. Build the library and make sure the files inside the `ignore_me` aren't included in the `commonjs` and `module` targets. --- README.md | 14 ++++++++++++++ packages/react-native-builder-bob/src/index.ts | 5 +++++ .../src/targets/commonjs.ts | 3 +++ .../react-native-builder-bob/src/targets/module.ts | 3 +++ packages/react-native-builder-bob/src/types.ts | 1 + .../react-native-builder-bob/src/utils/compile.ts | 4 +++- 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b9f99a9c..1ae9bf496 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,20 @@ The name of the folder with the source code which should be compiled. The folder The name of the folder where the compiled files should be output to. It will contain separate folder for each target. +#### `exclude` + +Glob pattern to be used while filtering the unnecessary files. Defaults to `'**/{__tests__,__fixtures__,__mocks__}/**'` if you don't specify it. + +> This option only works with `commonjs` and `module` targets. To exclude files while building `typescript`, please see [the tsconfig exclude field](https://www.typescriptlang.org/tsconfig#exclude). + +Example: + +```json +{ + "exclude": "ignore_me/**" +} +``` + #### `targets` Various targets to build for. The available targets are: diff --git a/packages/react-native-builder-bob/src/index.ts b/packages/react-native-builder-bob/src/index.ts index df2be360e..981a7dec8 100644 --- a/packages/react-native-builder-bob/src/index.ts +++ b/packages/react-native-builder-bob/src/index.ts @@ -390,6 +390,9 @@ yargs ); } + const exclude = + options.exclude ?? '**/{__tests__,__fixtures__,__mocks__}/**'; + const report = { info: logger.info, warn: logger.warn, @@ -418,6 +421,7 @@ yargs root, source: path.resolve(root, source as string), output: path.resolve(root, output as string, 'commonjs'), + exclude, options: targetOptions, report, }); @@ -427,6 +431,7 @@ yargs root, source: path.resolve(root, source as string), output: path.resolve(root, output as string, 'module'), + exclude, options: targetOptions, report, }); diff --git a/packages/react-native-builder-bob/src/targets/commonjs.ts b/packages/react-native-builder-bob/src/targets/commonjs.ts index 67a9cd493..2092906cd 100644 --- a/packages/react-native-builder-bob/src/targets/commonjs.ts +++ b/packages/react-native-builder-bob/src/targets/commonjs.ts @@ -11,12 +11,14 @@ type Options = Input & { sourceMaps?: boolean; copyFlow?: boolean; }; + exclude: string; }; export default async function build({ root, source, output, + exclude, options, report, }: Options) { @@ -31,6 +33,7 @@ export default async function build({ root, source, output, + exclude, modules: 'commonjs', report, field: 'main', diff --git a/packages/react-native-builder-bob/src/targets/module.ts b/packages/react-native-builder-bob/src/targets/module.ts index 3f71414c6..e6cee46c7 100644 --- a/packages/react-native-builder-bob/src/targets/module.ts +++ b/packages/react-native-builder-bob/src/targets/module.ts @@ -11,12 +11,14 @@ type Options = Input & { sourceMaps?: boolean; copyFlow?: boolean; }; + exclude: string; }; export default async function build({ root, source, output, + exclude, options, report, }: Options) { @@ -31,6 +33,7 @@ export default async function build({ root, source, output, + exclude, modules: false, report, field: 'module', diff --git a/packages/react-native-builder-bob/src/types.ts b/packages/react-native-builder-bob/src/types.ts index 4d71052ef..3215129c5 100644 --- a/packages/react-native-builder-bob/src/types.ts +++ b/packages/react-native-builder-bob/src/types.ts @@ -19,4 +19,5 @@ export type Options = { source?: string; output?: string; targets?: (Target | [Target, object])[]; + exclude?: string; }; diff --git a/packages/react-native-builder-bob/src/utils/compile.ts b/packages/react-native-builder-bob/src/utils/compile.ts index f3be314ab..4d089f6b3 100644 --- a/packages/react-native-builder-bob/src/utils/compile.ts +++ b/packages/react-native-builder-bob/src/utils/compile.ts @@ -13,6 +13,7 @@ type Options = Input & { copyFlow?: boolean; modules: 'commonjs' | false; field: 'main' | 'module'; + exclude: string; }; export default async function compile({ @@ -21,6 +22,7 @@ export default async function compile({ output, babelrc = false, configFile = false, + exclude, modules, copyFlow, sourceMaps = true, @@ -31,7 +33,7 @@ export default async function compile({ cwd: source, absolute: true, nodir: true, - ignore: '**/{__tests__,__fixtures__,__mocks__}/**', + ignore: exclude, }); report.info(