Skip to content

Import used by @jsx pragma is elided by typescript transform #8958

Closed

Description

@Jessidhia

Bug Report

Current Behavior
When an imported value is used only implicitly via JSX, the typescript type import elision is incorrectly removing the "type-only" import.

Input Code

  • REPL or Repo link if applicable:
/* @jsx jsx */
import { jsx } from '@emotion/core'

import ReactDOM from 'react-dom'

ReactDOM.render(<div/>, document.appendChild(document.createElement('div')))

Expected behavior/code
The "jsx" import should still be present in the output

Babel Configuration

Literally create-react-app --typescript defaults.

Additional context/Screenshots

Output from webpack:

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react-dom */ "./node_modules/react-dom/index.js");
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_0__);
var _jsxFileName = "/Users/kovensky/src/statsim/src/index.tsx";

/* @jsx jsx */

react_dom__WEBPACK_IMPORTED_MODULE_0___default.a.render(jsx("div", {
  __source: {
    fileName: _jsxFileName,
    lineNumber: 6
  },
  __self: undefined
}), document.appendChild(document.createElement('div')));

Note how the @emotion/core import is missing entirely.

Adding an unused jsx reference anywhere to the file makes the build correct.

Activity

  1. jfrolich commented on Nov 27, 2018

    @jfrolich

    This can be fixed by setting the jsxPragma option in the typescript transform. However now React is removed when Fragments are used.

  2. jfrolich commented on Nov 27, 2018

    @jfrolich

    A fix is to not use the pragma parameter of the typescript plugin (keep the default react). Then use another babel plugin (order matters) to add the pragma. Not ideal. I think the typescript plugin shouldn't need to do anything with the pragma parameter. I think it needs to specify the pragma so it knows it needs to strip out type only imports. However these will be tree shaken out anyway I guess, so do we really need to have this option?

  3. cameron-martin commented on Jan 26, 2019

    @cameron-martin
    Contributor

    Surely there should be a similar option in the typescript preset that mirrors the pragmaFrag option in the react preset.

    Alternatively you can not use the fragment shorthand and write it as <React.Fragment>.

    However these will be tree shaken out anyway I guess, so do we really need to have this option?

    I believe webpack errors if you try to import a binding that does not exist.

  4. added
    outdated
    on Apr 27, 2019
  5. locked as resolved and limited conversation to collaborators on Apr 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions