question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[@babel/preset-typescript]`alias` will be truncated in `import funcA as alias` if `alias` is not used in source code

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current behavior

import {
    useEffect,
    useState,
    createElement as h,
    memo
} from 'react';
// side effect in @babel/preset-typescript, if h is not mentioned the as will be truncated
h;

If I remove the hard-coded h, which means there is an import alias and no reference, the alias will be truncated as typescript type ‘as’ and the test will fail:

    ReferenceError: h is not defined

      345 | 
      346 |     it( 'Test React.memo behavior', async() => {
    > 347 |         render( <MemoContainer />, fixture.container );
          |         ^

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
        // '^.+\\.tsx?$': 'ts-jest'
        // TODO: switch to babel-jest later
        '^.+\\.tsx?$': [ 'babel-jest', {
                exclude: 'node_modules/**',
                presets: [
                    [ '@babel/preset-env', {
                        // modules: 'commonjs'
                        targets: {
                            // browsers: [ 'last 2 versions', 'ie >= 11' ]
                            browsers: [ 'last 1 chrome versions' ]
                        }
                    } ],
                    // https://github.com/facebook/create-react-app/blob/f5c3bdb65480f93b2d4a4c2f3214fc50753de434/packages/babel-preset-react-app/create.js
                    [ '@babel/preset-react', {
                        // pragma: 'this.$createElement'
                        // align with vue
                        pragma: 'h',
                        pragmaFrag: 'h.Fragment',
                        // development: true,
                        useBuiltIns: true
                    } ],
                    '@babel/preset-typescript'
                ]
                // babelHelpers: 'bundled',
                // extensions: [ '.js', '.jsx', '.ts', '.tsx' ]
        } ]
    }

Environment

git clone https://github.com/Few-UI/few
# remove L19 in react.test.ts
npm i
npm test
  • Babel version(s): [7.10.2]
  • Node/npm version: [e.g. Node v12.18.2/npm 6.14.5]
  • OS: [e.g. OSX 10.15.5]
  • Monorepo: [no]
  • How you are using Babel: [jest babel loader]

Possible Solution Is is possible to get a fix or workaround is fine? Thanks!

Additional context Add any other context about the problem here. Or a screenshot if applicable

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Aug 2, 2020

If that’s a type-only import, you should use import type { ComponentDef } from './types'; to make Babel remove it.

1reaction
JLHwungcommented, Jul 21, 2020

As a workaround, you can set onlyRemoveTypeImports: true in preset-typescript.

['@babel/preset-typescript', { onlyRemoveTypeImports: true }]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module Resolution or Import Alias: The Final Guide - Raul Melo
Tired to have weird imports? It's time to learn how can you enable aliases to your imports and let code more organized.
Read more >
Importing BFILE Columns and Directory Aliases
When you import table data that contains BFILE columns, the BFILE locator is imported with the directory alias and file name that was...
Read more >
Avoiding imports and aliases - Andreas Möller
With imports, I can import the fully-qualified class name and reference the ... With aliases, navigating the code becomes more difficult.
Read more >
Kawa: Frequently Asked Questions - GNU.org
The name(s) defined by import are by default private. A name defined using define-alias is by default exported; to avoid that use define-private-alias...
Read more >
src/main/java/org/conscrypt/TrustedCertificateStore.java
you may not use this file except in compliance with the License. ... <p>The CAs are accessed via {@code KeyStore} style aliases. Aliases....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found