Coder Social home page Coder Social logo

Comments (9)

MabelCheng avatar MabelCheng commented on June 15, 2024 1

Hi, I met the same problem when running it in an electron-react app. Do you fix the problem? I tried to write a stand-alone script, but it failed. Thank you.

from babel-plugin-react-intl.

dabosin avatar dabosin commented on June 15, 2024

@eddiewang Do you use "defaultMessage" prop in your FormattedMessage?

from babel-plugin-react-intl.

eddiewang avatar eddiewang commented on June 15, 2024

@dabosin yes, I do.

from babel-plugin-react-intl.

lzhuor avatar lzhuor commented on June 15, 2024

Hi, I encountered the same problem before. Please kindly check your babel plugins config in webpack.

What I did to fix the problem is that I wrote a stand-alone script to extract messages so the AST will not be polluted by additional babel plugins.

Reference: #125

from babel-plugin-react-intl.

lzhuor avatar lzhuor commented on June 15, 2024

Hi @MabelCheng , yes I did resolve it. I put a code snippet in this issue: #125

FYI:

Above problem has been resolved by:

  1. Translating files using babel-runtime therefore we can control presets and plugins better. Prior that, we were applying babel-plugin-react-intl plugin during the build time (with WebPack). Build time has lots more babel-plugins applied which may have side effects on the syntax trees.

  2. Sharing the chunk of code we made:

console.log('🥁 Starting Babel');

globSync('./src/**/!(*.stories|*.test|actions|selectors|reducers|validate).js').forEach(filePath => {
  try {
    console.log(`🏥 Babel is transforming ${filePath}`);
    babel.transformFileSync(path.normalize(filePath), {
      presets: ['flow'],
      plugins: [
        ['transform-es2015-arrow-functions', { spec: true }],
        'transform-class-properties',
        'transform-object-rest-spread',
        'transform-runtime',
        'flow-react-proptypes',
        [
          'react-intl',
          {
            messagesDir: './src/messages',
            enforceDescriptions: false,
          },
        ],
      ],
    });
  } catch (err) {
    console.error(err.message);
  }
});
  1. You need set bindings to true to get class properties being parsed. Just FYI - How referencesImport() is implemented - https://npmdoc.github.io/node-npmdoc-babel-core/build/apidoc.html#apidoc.element.babel-core.traverse.NodePath.prototype.referencesImport
['transform-es2015-arrow-functions', { spec: true }],
// examples: https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-arrow-functions

from babel-plugin-react-intl.

eddiewang avatar eddiewang commented on June 15, 2024

@lzhuor can you post what your npm script looks like? Do you have a script just for parsing the translations through babel-plugin-react-intl?

Also, what do you mean by setting bindings to true? Most of my components are not arrow functions, but rather class Component extends React.Component.

from babel-plugin-react-intl.

teriteriteri avatar teriteriteri commented on June 15, 2024

@eddiewang hi, I'm new to react, just wondering how do we extract messages using babel-plugin-react-intl? Do we need to write a script to scan through the code which contains defineMessage and formattedMessage then save into json files? If so, what's the purpose of using babel-plugin when the script basically does all the jobs already?

Thanks!!!

from babel-plugin-react-intl.

lzhuor avatar lzhuor commented on June 15, 2024

@eddiewang The above example is my script for parsing intl - translate.js.

The npm scripts:

"scripts": {
    ...
    "translate": "babel-node ./tools/translate/translate.js",
    ...
  },

FYI:

from babel-plugin-react-intl.

longlho avatar longlho commented on June 15, 2024

close due to stale

from babel-plugin-react-intl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.