reactjswebpackautodesk-forgeautodeskwebpack-loader

Autodesk React Forge problem with forge-dataviz-iot-react-components in a empty project


If you install the official npm package, it works.

But according to the official documentation and simply including import { Viewer } from "forge-dataviz-iot-react-components" (like in this example) in a empty new react project (using npx create-react-app) you will get this error:

./node_modules/forge-dataviz-iot-react-components/client/components/BasicTree.jsx 107:16
Module parse failed: Unexpected token (107:16)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|         if (node.children.length > 0) {
|             return (
>                 <TreeItem
|                     id={`tree-node-${node.id}`}
|                     key={node.id}

Which loader do I need to add on webpack to avoid this error?


Solution

  • it is not possible to include the package https://www.npmjs.com/package/forge-dataviz-iot-react-components inside a react project made with npx create-react-app (hoping Autodesk is going to fix this problem soon).

    You need to edit /node_modules/react-scripts/config/webpack.config.js in 2 parts:

    one line about PIXI

    ...
          alias: {
            'PIXI': "pixi.js/",
    
            // Support React Native Web
            // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
            'react-native': 'react-native-web',
            // Allows for better profiling with ReactDevTools
            ...(isEnvProductionProfile && {
              'react-dom$': 'react-dom/profiling',
              'scheduler/tracing': 'scheduler/tracing-profiling',
            }),
            ...(modules.webpackAliases || {}),
          },
    ...
    

    and another part about /forge-dataviz-iot-react-component

    ...
        module: {
          strictExportPresence: true,
          rules: [
            // Disable require.ensure as it's not a standard language feature.
            { parser: { requireEnsure: false } },
            {
              // "oneOf" will traverse all following loaders until one will
              // match the requirements. When no loader matches it will fall
              // back to the "file" loader at the end of the loader list.
              oneOf: [
    
    
                {
                  test: /forge-dataviz-iot-react-component.*.jsx?$/,
                  use: [
                    {
                      loader: require.resolve('babel-loader'),
                      options: {
                        presets: ["@babel/react", ["@babel/env", { "targets": "defaults" }]],
                        plugins: ["@babel/plugin-transform-spread"]
                      }
                    },
                  ],
                  exclude: path.resolve(__dirname, "node_modules", "forge-dataviz-iot-react-components", "node_modules"),
                },
    
    
    
                // TODO: Merge this config once `image/avif` is in the mime-db
                // https://github.com/jshttp/mime-db
                {
                  test: [/\.avif$/],
                  loader: require.resolve('url-loader'),
                  options: {
                    limit: imageInlineSizeLimit,
                    mimetype: 'image/avif',
                    name: 'static/media/[name].[hash:8].[ext]',
                  },
                },
    ...
    

    after that on /node_modules/forge-dataviz-iot-react-components/client/components/Viewer.jsx you will get errors about undefined Autodesk variable easily fixable changing Autodesk with window.Autodesk.

    Although you will not see any other errors, the package will not work.