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

Having trouble using ts-transformer in webpack build #2110

Closed
arawden opened this issue Sep 17, 2020 · 5 comments
Closed

Having trouble using ts-transformer in webpack build #2110

arawden opened this issue Sep 17, 2020 · 5 comments

Comments

@arawden
Copy link
Contributor

arawden commented Sep 17, 2020

Which package?
@formatjs/ts-transformer

Describe the bug
I have an application in which I am currently explicitly setting IDs. My workflow looks like:

  1. Add a FormattedMessage component to my codebase
  2. Extract the messages using the formatjs CLI
  3. Compile the messages
  4. Assign the IDs which are in the extracted JSON to each FormattedMessage component
  5. Build the application and run it.

I am trying to eliminate step 4 by using the ts-transformer tooling within my webpack file as described here

However, after following the documentation, upon loading of the component, I get an error that an ID must be provided to format a message, and the entire application crashes.

I've created a sandbox to try to recreate the problem.

To Reproduce
Steps to reproduce the behavior:

  1. Create a React application with react-intl, webpack, and ts-transformer
  2. Add a FormattedMessage component to the application without any ID declaration
  3. Configure webpack to use ts-transformer in ts-loader
  4. Build the application and load it in the browser. The application will crash when it encounters the the FormattedMessage component without an ID.

Expected behavior
The build system injects the IDs into the compiled JS output

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome 85
  • Version:
    • react-intl: 5.8.1
    • webpack: 4.42.0
    • webpack-cli: 3.3.39
    • ts-transformer: 2.9.1
    • ts-loader: 6.2.0
@arawden arawden added the bug label Sep 17, 2020
@pyrocat101
Copy link
Member

pyrocat101 commented Sep 27, 2020

@arawden thanks for reporting the issue. While we are looking into it, you can use the babel plugin to unblock yourself:

module.exports = {
  mode: "development",
  entry: {
    bundle: "./src/index.tsx"
  },
  devtool: false,
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-react',
                '@babel/preset-typescript'
              ],
              plugins: [
                [
                  "react-intl",
                  {
                    "extractFromFormatMessageCall": true,
                    "idInterpolationPattern": "[sha512:contenthash:base64:6]",
                    "ast": true
                  }
                ]
              ]
            }
          }
        ],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"]
  },
  output: {
    filename: "[name].js"
  }
};

Note that you might want to run the type checking separately since Babel doesn't do that for you.

@stale
Copy link

stale bot commented Oct 12, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 12, 2020
@longlho
Copy link
Member

longlho commented Oct 13, 2020

Ok looks like create-react-app doesn't allow custom webpack.config.js so you'd have to eject

@longlho longlho closed this as completed Oct 13, 2020
@unframework
Copy link
Contributor

I ran into the same issue, I think.

One of the core causes was that ts-transformer docs do not mention the extractFromFormatMessageCall flag. Also, it looks like the ts-transformer docs mention an option called idInterpolationPattern but the overrideIdFn option is what actually pulls double-duty as accepting either a function or a pattern string (as per the recent change in #1926).

Could I suggest adding the idInterpolationPattern option to match the configuration approach of e.g. Babel plugin for consistency? The fact that it would not affect existing IDs is important to highlight as well.

Happy to contribute to the docs myself, but the above proposal means changing functionality too.

@longlho
Copy link
Member

longlho commented Oct 17, 2020

@unframework any contribution is welcome :)

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