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

Next.js babel preset #72

Closed
retyui opened this issue Aug 26, 2019 · 8 comments
Closed

Next.js babel preset #72

retyui opened this issue Aug 26, 2019 · 8 comments

Comments

@retyui
Copy link
Contributor

retyui commented Aug 26, 2019

1) Integration with Next.js (TypeScript)

I don't know how can I fix the next problem:

import * as React from 'react';

// @ts-ignore : 'fbt' is declared but its value is never read.
import fbt from 'fbt'; 

console.log(fbt); // ( 1 ) 

// @ts-ignore
const node = <fbt desc='desc-text'>Some text</fbt>;
  1. When I commented on this line I get next build error: fbt is not bound. Did you forget to require('fbt')

Did It mean that webpack bundle remove unused import before babel transpilation?

2) Empty childParentMappings:{}

And node_modules/babel-plugin-fbt/bin/collectFBT.js couldn't collect any phrases from code


DEMO Next.js App

dependencies": {
    "fbt": "^0.9.51",
    "babel-plugin-fbt": "^0.9.12",
    "babel-plugin-fbt-runtime": "^0.9.1",
}
@retyui retyui changed the title Next.js pabel preset Next.js babel preset Aug 26, 2019
@kayhadrin
Copy link
Collaborator

kayhadrin commented Aug 30, 2019

Hi @retyui, could you create 2 separate issues for this instead?
I'll only reply to 1) here:


It looks like the JS code that our babel fbt plugin is analyzing was missing the import fbt from 'fbt' instruction.

Could you try to log the entire JS code to ensure that it's present before you start the babel fbt transform?

We normally support that kind of ES6 import.
See: https://github.com/facebookincubator/fbt/blob/3751d03fc37a0362f3284797af1f3f43b90922b7/transform/babel-plugin-fbt/__tests__/fbt-test.js#L163-L166

@retyui
Copy link
Contributor Author

retyui commented Sep 1, 2019

@kayhadrin
I find out what happened! It was @babel/plugin-transform-typescript

  1. The next call returns false for ImportDeclaration import fbt from 'fbt';

babel/packages/babel-plugin-transform-typescript/src/index.js#102:106

                  isImportTypeOnly({
                    binding,
                    programPath: path,
                    jsxPragma: fileJsxPragma || jsxPragma,
                  })
  1. And then would be deleted successfully here:
    babel/packages/babel-plugin-transform-typescript/src/index.js#115
                stmt.remove();

@retyui
Copy link
Contributor Author

retyui commented Sep 1, 2019

I also create demo https://github.com/retyui/fbt-issues-72

@kayhadrin
Copy link
Collaborator

Here's a diff I made from your repo at commit: retyui/fbt-issues-72@d23bea120e9368a22612a9064bfdf8e894c8bfdd:

diff --git a/.babelrc.js b/.babelrc.js
index 8dbdcfa..f20e676 100644
--- a/.babelrc.js
+++ b/.babelrc.js
@@ -3,8 +3,8 @@ const fbtEnumManifest = require("./enum-manifest.json");
 
 const presets = ["@babel/preset-typescript"];
 const plugins = [
-  "babel-plugin-fbt-runtime",
-  ["babel-plugin-fbt", { fbtEnumManifest }]
+  // ["babel-plugin-fbt", { fbtEnumManifest }],
+  // "babel-plugin-fbt-runtime",
 ];
 
 module.exports = {
diff --git a/lib/a.tsx b/lib/a.tsx
index 3488c7b..77d57c0 100644
--- a/lib/a.tsx
+++ b/lib/a.tsx
@@ -1,5 +1,5 @@
 import React from "react";
-import "fbt";
+// import "fbt";
 import fbt from "fbt";
 
 declare global {
@@ -17,4 +17,8 @@ declare global {
 
 const a = <fbt desc="title-text">page Home</fbt>;
 
+// If you uncomment this line, the `fbt import` at line 3 will be
+// effectively generated by typescript
+// fbt('test', 'foo');
+
 export default () => a;

I then ran the normal typescript transform with this command:

node node_modules/.bin/babel lib/a.tsx -d dist --extensions '.tsx'

From this, I can see that the typescript transform is unable to detect that the fbt imported module is needed when used in JSX syntax; but if you use the fbt() functional form, it outputs the expected JS code.

Here's the result if I add some fbt() calls in the TSX code and generate the output (after re-enabling the fbt babel transforms):

import React from "react"; // import "fbt";

import fbt from "fbt";

//console.log(fbt);
const a = fbt._("page Home", null, {
  hk: "36x7Vw"
}); // If you uncomment this line, the `fbt import` at line 3 will be
// effectively generated by typescript


fbt._("test", null, {
  hk: "EvohQ"
});

export default (() => a);

PS: I think you have the babel transforms order in the wrong order in your original .babelrc.js file.

So this is more of an issue with the Typescript Babel transform and I'll leave it at that for now.
I hope you can find a way to circumvent this issue.

@retyui
Copy link
Contributor Author

retyui commented Sep 5, 2019

@kathykam
Ok, no problem
I will add a new issue to babel

@jrwats
Copy link
Contributor

jrwats commented Sep 5, 2019

I suspect there's an unfortunate Babel transform ordering requirement here.
(A) The FBT transform needs the typescript transform to run first in order for types to be stripped (I assume the FBT transform would complain otherwise) AND
(B) The typescript transform needs to see the result of the FBT transform (fbt._(...) calls) rather than the source <fbt>...</fbt> to interpret fbt as being "used" if I'm understanding this correctly.

Possibly related to our TODO: #40

@retyui, have you tried running the Typescript transform strictly AFTER the FBT transform? If (A) isn't true, this might solve your problems.

@retyui
Copy link
Contributor Author

retyui commented May 23, 2020

Here is an article about Typescript + fbt where I describe problems that i faced and how to fix them

@Lpaydat
Copy link

Lpaydat commented Sep 8, 2020

@retyui I followed your guideline and its work well <3.

But recently, after I have upgraded Next.Js from 9.3.5 to 9.5.3, another issue occurred.
It has no problem on localhost but on vercel it throw fbt is not defined error.

Maybe it has been removed by webpack as the unused code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants