Skip to content

Commit 859c1d7

Browse files
authored
feat: strip empty import statements from Flow type imports (#1128)
* fix: add removeEmptyImports option to flow remove types APIs * docs: update flow-remove-types related docs * chore: bump flow-remove-types to 2.268.0 * chore: update lockfile * chore: federation tester version warning * chore: changeset
1 parent 2af1811 commit 859c1d7

11 files changed

Lines changed: 60 additions & 23 deletions

File tree

.changeset/lazy-eels-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": minor
3+
---
4+
5+
Strip empty import statements that are a leftover from stripping flow type imports

apps/tester-federation-v2/configs/rspack.host-app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default (env) => {
4949
'react-native': {
5050
singleton: true,
5151
eager: true,
52-
requiredVersion: '0.78.0',
52+
requiredVersion: '0.79.1',
5353
},
5454
'@react-navigation/native': {
5555
singleton: true,

packages/repack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"estree-util-is-identifier-name": "^1.1.0",
8585
"events": "^3.3.0",
8686
"execa": "^5.0.0",
87-
"flow-remove-types": "^2.250.0",
87+
"flow-remove-types": "^2.268.0",
8888
"gradient-string": "^2.0.2",
8989
"image-size": "^1.1.1",
9090
"jsonwebtoken": "^9.0.2",

packages/repack/src/loaders/flowLoader/options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { validate } from 'schema-utils';
44
export interface FlowLoaderOptions {
55
all?: boolean;
66
ignoreUninitializedFields?: boolean;
7-
pretty?: true;
7+
pretty?: boolean;
8+
removeEmptyImports?: boolean;
89
}
910

1011
type Schema = Parameters<typeof validate>[0];
@@ -16,6 +17,7 @@ export const optionsSchema: Schema = {
1617
all: { type: 'boolean' },
1718
ignoreUninitializedFields: { type: 'boolean' },
1819
pretty: { type: 'boolean' },
20+
removeEmptyImports: { type: 'boolean' },
1921
},
2022
};
2123

packages/repack/src/types/flow-remove-types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ declare module 'flow-remove-types' {
33
code: string,
44
options: {
55
all?: boolean;
6-
pretty?: boolean;
76
ignoreUninitializedFields?: boolean;
7+
pretty?: boolean;
8+
removeEmptyImports?: boolean;
89
}
910
) => {
1011
toString(): string;

packages/repack/src/utils/getFlowTransformRules.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ interface GetFlowTransformRulesOptions {
5050
* rather than only removing the type.
5151
*/
5252
ignoreUninitializedFields?: boolean;
53+
54+
/**
55+
* Whether to remove empty import statements which were
56+
* only used for importing flow types.
57+
*/
58+
removeEmptyImports?: boolean;
5359
}
5460

5561
/**
@@ -61,6 +67,7 @@ interface GetFlowTransformRulesOptions {
6167
* @param options.exclude Array of module names to exclude from Flow transformation (defaults to empty array)
6268
* @param options.all If true, bypasses looking for @flow pragma comment before parsing (defaults to true)
6369
* @param options.ignoreUninitializedFields If true, removes uninitialized class fields completely rather than only removing the type (defaults to false)
70+
* @param options.removeEmptyImports If true, removes empty import statements which were only used for importing flow types (defaults to true)
6471
*
6572
* @returns Array of rules for transforming Flow typed modules
6673
*/
@@ -69,6 +76,7 @@ export function getFlowTransformRules({
6976
exclude = [],
7077
all = true,
7178
ignoreUninitializedFields = false,
79+
removeEmptyImports = true,
7280
}: GetFlowTransformRulesOptions = {}) {
7381
return [
7482
{
@@ -78,7 +86,7 @@ export function getFlowTransformRules({
7886
exclude: getModulePaths(exclude),
7987
use: {
8088
loader: '@callstack/repack/flow-loader',
81-
options: { all, ignoreUninitializedFields },
89+
options: { all, ignoreUninitializedFields, removeEmptyImports },
8290
},
8391
},
8492
];

packages/repack/src/utils/getJsTransformRules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ interface GetJsTransformRulesOptions {
3131
all?: boolean;
3232
/** If true, removes uninitialized class fields completely rather than only removing the type */
3333
ignoreUninitializedFields?: boolean;
34+
/** If true, removes empty import statements which were only used for importing flow types */
35+
removeEmptyImports?: boolean;
3436
};
3537
/** Configuration for enabling/disabling codegen transformations */
3638
codegen?: {

pnpm-lock.yaml

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/src/latest/api/loaders/flow-loader.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type FlowLoaderOptions = {
1313
all?: boolean;
1414
ignoreUninitializedFields?: boolean;
1515
pretty?: boolean;
16+
removeEmptyImports?: boolean;
1617
};
1718
```
1819

@@ -37,6 +38,13 @@ If true, removes uninitialized class fields (`foo;`, `foo: string;`) completely
3738

3839
If true, removes types completely rather than replacing with spaces. This may require using source maps.
3940

41+
### removeEmptyImports
42+
43+
- Type: `boolean`
44+
- Default: `true`
45+
46+
If true, removes empty import statements (`import {} from 'flow-typed-module';`) which were only used for importing flow types.
47+
4048
## Example
4149

4250
:::info

website/src/latest/api/utils/get-flow-transform-rules.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface GetFlowTransformRulesOptions {
1818
exclude?: string[];
1919
all?: boolean;
2020
ignoreUninitializedFields?: boolean;
21+
removeEmptyImports?: boolean;
2122
}
2223
```
2324

@@ -85,6 +86,13 @@ Whether to bypass looking for `@flow` pragma comment before parsing
8586

8687
Whether to remove uninitialized class fields completely rather than only removing the type (defaults to false)
8788

89+
### options.removeEmptyImports
90+
91+
- Type: `boolean`
92+
- Default: `true`
93+
94+
Whether to remove empty import statements which were only used for importing flow types (defaults to true)
95+
8896
## Example
8997

9098
```js title=rspack.config.cjs
@@ -97,6 +105,7 @@ module.exports = {
97105
include: ["react-native", "@react-native"],
98106
all: true,
99107
ignoreUninitializedFields: false,
108+
removeEmptyImports: true,
100109
}),
101110
],
102111
},

0 commit comments

Comments
 (0)