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

Runtime types generated when not used #239

Open
2 of 10 tasks
skeggse opened this issue Oct 12, 2019 · 4 comments
Open
2 of 10 tasks

Runtime types generated when not used #239

skeggse opened this issue Oct 12, 2019 · 4 comments

Comments

@skeggse
Copy link
Contributor

skeggse commented Oct 12, 2019

This is a:

  • Bug Report
  • Feature Request
  • Question
  • Other

Which concerns:

  • flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators
  • flow-runtime-mobx
  • flow-config-parser
  • The documentation website

Relates to #202.


What is the current behaviour?

Transforming a barebones file that imports types from a dependency causes that dependency to get imported, even if the types aren't used - and the runtime types aren't available.

In this particular case, I'm passing the annotate: false option to babel-plugin-flow-runtime to try and work around #202.

import type { APIGatewayEvent, Context, ProxyResult } from 'flow-aws-lambda';

import { load } from './load';

export function handle(evt: APIGatewayEvent<any>, ctx: Context): Promise<ProxyResult> {
  return load(evt, ctx);
}

Babel generates the following from the above file, which imports the flow-aws-lambda package; this package exports types, but does not export runtime types. I could try and use @babel/register to work around this, but for my purposes I'd like to have babel spit out code that's directly executed by node, for auditing and deployment purposes.

"use strict";

exports.handle = handle;

var _flowAwsLambda = require("flow-aws-lambda");

var _load = require("./load");

var _flowRuntime = _interopRequireDefault(require("flow-runtime"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const APIGatewayEvent = _flowRuntime.default.tdz(() => _flowAwsLambda.APIGatewayEvent);

const Context = _flowRuntime.default.tdz(() => _flowAwsLambda.Context);

const ProxyResult = _flowRuntime.default.tdz(() => _flowAwsLambda.ProxyResult);

function handle(evt, ctx) {
  return (0, _load.load)(evt, ctx);
}

What is the expected behaviour?

Generate only those runtime types that actually get used (in this case, none), and omit the associated imports if any:

--- dist/index.js
+++ dist/index.js
@@ -1,8 +1,6 @@
 "use strict";
 
 exports.handle = handle;
-
-var _flowAwsLambda = require("flow-aws-lambda");
 
 var _load = require("./load");
 
@@ -10,12 +8,6 @@
 
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
-const APIGatewayEvent = _flowRuntime.default.tdz(() => _flowAwsLambda.APIGatewayEvent);
-
-const Context = _flowRuntime.default.tdz(() => _flowAwsLambda.Context);
-
-const ProxyResult = _flowRuntime.default.tdz(() => _flowAwsLambda.ProxyResult);
-
 function handle(evt, ctx) {
   return (0, _load.load)(evt, ctx);
 }

Along these lines, it'd also be nice to be able to disable annotations for a specific function, but I think for now disabling annotations just for specific files in babel's rc file will suffice.


Which package versions are you using?

  • flow-runtime@0.17.0
  • babel-plugin-flow-runtime@0.19.0
@gajus
Copy link
Owner

gajus commented Oct 12, 2019

I can reward a bounty to this issue if you are available to work.

@skeggse
Copy link
Contributor Author

skeggse commented Oct 22, 2019

@gajus I might be able to find time to work on this, though I have looked at the code, and I'm not sure I'll have enough context to implement a solution. I'm somewhat partial to the workaround in #240 for my own use-cases, but I don't think it's a great solution for #239 for everyone.

@gajus
Copy link
Owner

gajus commented Oct 22, 2019

What is a good amount to reward to this bounty?

@skeggse
Copy link
Contributor Author

skeggse commented Nov 5, 2019

I'm very unsure about how I'd evaluate that, sorry.

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

2 participants