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

import t from 'flow-runtime' doesn't get transpiled when sourceType = unambiguous #242

Open
2 of 10 tasks
jedwards1211 opened this issue May 27, 2020 · 0 comments
Open
2 of 10 tasks

Comments

@jedwards1211
Copy link
Collaborator

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

Putting this summary here in case other people run into this issue, it took me lots of debugging to understand.

So even though the docs for the sourceType option state that

"unambiguous" - Consider the file a "module" if import/export statements are present, or else consider it a "script"

From what I'm seeing, if only type imports and exports are present, Babel 7 fails to consider the file a module. It will only consider it a module if there are value imports or exports present.

Consequently, the import t from 'flow-runtime' statement inserted by babel-plugin-flow-runtime doesn't get transpiled into a require statement if Babel fails to consider the file a module.

I don't know if there's any way we can fix this from our end.

The workaround is simple at least, just add your own import t from 'flow-runtime' declaration to the file.

.babelrc

{
  "plugins": [
    ["@babel/plugin-proposal-decorators", {"legacy": true}],
    "babel-plugin-flow-runtime"
  ],
  "presets": [
    ["@babel/preset-env", {"targets": {"node": "current"}}],
    "@babel/preset-flow"
  ],
  "sourceType": "unambiguous"
}

typeOnly.js

// @flow

export type ConnectionStatus = 'connecting' | 'connected' | 'error'

What is the current behaviour?

import t from "flow-runtime";
export const ConnectionStatus = t.type("ConnectionStatus", t.union(t.string("connecting"), t.string("connected"), t.string("error")));

What is the expected behaviour?

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectionStatus = void 0;

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

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

const ConnectionStatus = _flowRuntime.default.type("ConnectionStatus", _flowRuntime.default.union(_flowRuntime.default.string("connecting"), _flowRuntime.default.string("connected"), _flowRuntime.default.string("error")));

exports.ConnectionStatus = ConnectionStatus;

Which package versions are you using?

├── @babel/[email protected] 
├── @babel/[email protected] 
├── @babel/[email protected] 
├── @babel/[email protected] 
├── [email protected] 
└── [email protected] 
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

1 participant