【问题标题】:How to load babel-plugin-react-intl using babel-loader?如何使用 babel-loader 加载 babel-plugin-react-intl?
【发布时间】:2016-09-26 22:36:09
【问题描述】:

我最近使用 create-react-app 初始化了一个 react 应用。 我弹出了应用程序,现在我将所有文件都导出到了我的主目录。 这个设置创建了一个 babel.dev.js 而不是使用 .babelrc(它使用 babel-loader)。

我想弄清楚如何在没有 .babelrc 文件的情况下配置 react-intl 和 babel-plugin-react-intl。

文档说 .babelrc 是推荐的

通过 .babelrc(推荐)

.babelrc

{
  "plugins": [
    ["react-intl", {
        "messagesDir": "./build/messages/"
    }]
  ]
}

使用 babel-loader 有这种行为的语法是什么?现在 babel.dev.js 中的插件是这样的:

plugins: [
  // class { handleClick = () => { } }
  require.resolve('babel-plugin-transform-class-properties'),
  // { ...todo, completed: true }
  require.resolve('babel-plugin-transform-object-rest-spread'),
  // function* () { yield 42; yield 43; }
  [require.resolve('babel-plugin-transform-regenerator'), {
    // Async functions are converted to generators by babel-preset-latest
    async: false
  }],
  // Polyfills the runtime needed for async/await and generators
  [require.resolve('babel-plugin-transform-runtime'), {
    helpers: false,
    polyfill: false,
    regenerator: true,
    // Resolve the Babel runtime relative to the config.
    // You can safely remove this after ejecting:
    moduleName: path.dirname(require.resolve('babel-runtime/package'))
  }]
]

我的组件目前的字符串定义如下:

const messages = defineMessages({
  summaryTitle: {
    "id": "checkout.section.title.summary",
    "description": "Summary Section Title",
    "defaultMessage": "Summary"
  },
  shippingTitle: {
    "id": "checkout.section.title.shipping",
    "description": "Shipping Section Title",
    "defaultMessage": "Shipping"
  }
});

【问题讨论】:

    标签: react-intl babel-plugin-react-intl


    【解决方案1】:

    像这样将 babel-plugin-react-intl 添加到你的插件数组中:

    plugins: [
        ..., // some plugins here
        [require.resolve('babel-plugin-react-intl'), { messageDir: "./build/messages"}]
    ]
    

    这将加载将 messageDir 作为选项传递给它的插件。

    【讨论】:

    • @pablomiranda 太棒了,它就像一个魅力!穆伊托·奥布里加达!
    猜你喜欢
    • 2019-01-19
    • 2016-05-27
    • 1970-01-01
    • 2018-08-27
    • 2018-07-05
    • 2021-07-31
    • 2016-05-30
    • 2018-05-18
    • 2018-08-14
    相关资源
    最近更新 更多