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.

Auto import fbt in babel plugin

See original GitHub issue

šŸš€ Feature Proposal

Add support to auto import fbt in the babel plugin. Similar to how the new react automatic runtime feature in @babel/plugin-transform-react-jsx

Motivation

Personally I often forget to import fbt until the compilation fails, making this automatic would improve the DX for at least me.

This would also fix use with ā€œorganize importsā€ in VS Code, where it removed the fbt import since it canā€™t see itā€™s being used.

Example

// input

function App() {
  return (
    <fbt desc="lorem text">Lorem ipsum dolor</fbt>
  )
}

// output

import fbt from 'fbt'

function App() {
  return (
    <fbt desc="lorem text">Lorem ipsum dolor</fbt>
  )
}

Pitch

It makes sense for this feature to be in the core babel plugin, I guess itā€™s possible to create a custom plugin but then you need to be careful about ordering etc

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
alexandernanbergcommented, Feb 23, 2021

Created a stand-alone plugin for now, if it works out well I might create a PR sometime in the future

module.exports = function fbtImportPlugin({ types: t }) {
  let root;

  return {
    visitor: {
      Program(path) {
        root = path;
      },
      JSXElement(path) {
        const hasFbtImport = !path.context.scope.getBinding('fbt');

        if (path.node.openingElement.name.name === 'fbt' && hasFbtImport) {
          const importDeclaration = t.importDeclaration(
            [t.importDefaultSpecifier(t.identifier('fbt'))],
            t.stringLiteral('fbt')
          );
          const [importPath] = root.unshiftContainer('body', importDeclaration);
          // Babel doesn't automatically update the bindings when we insert the
          // import statement, so manually update the binding here.
          root.scope.registerBinding('module', importPath);
        }
      },
    },
  };
};
// .babelrc
{
  "plugins": [
    "./babel-plugin-fbt-import", // need to be before the other fbt plugins
    "babel-plugin-fbt-runtime",
    [
      "babel-plugin-fbt",
      {
        "extraOptions": { "__self": true, "__source": true }
      }
    ]
  ]
}
1reaction
alexandernanbergcommented, Jan 20, 2022

Iā€™ve published the code as babel-plugin-fbt-import on npm now, and code can be found here https://github.com/alexandernanberg/babel-plugin-fbt-import

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-plugin-fbt-import - npm
A babel plugin to automatically import `fbt`. ... Start using babel-plugin-fbt-import in your project by running `npm i babel-plugin-fbt-import`.
Read more >
babel/preset-react
automatic auto imports the functions that JSX transpiles to. classic does not automatic import anything. development. boolean , defaults to false .
Read more >
Enumerations | FBT - Meta Open Source
and fbt.enum` both provide the ability to add your ad-hoc or ... manifest that makes them available to the babel-plugin-fbt at "build-time".
Read more >
babel-plugin-auto-import examples - CodeSandbox
Learn how to use babel-plugin-auto-import by viewing and forking example apps that make use of babel-plugin-auto-import on CodeSandbox.
Read more >
How to add support Typescript for FBT an internationalization ...
import * as React from "react"; import fbt from "fbt"; ... And when you combine @babel/preset-typescript + babel-plugin-fbt you will beĀ ...
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