空项目中 forge-dataviz-iot-react-components 的 Autodesk React Forge 问题

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

如果你安装官方 npm package,就可以了。

但是根据official documentation and simply including import { Viewer } from "forge-dataviz-iot-react-components" (like in this示例)在一个空的新反应项目中(使用npx create-react-app)你会得到这个错误:

./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}

我需要在 webpack 上添加哪个 loader 才能避免这个错误?

我最近尝试了这个包,但遇到了同样的问题。 所以我在没有 CRA 的情况下从头开始创建了一个 React 项目,并遵循了这个 repo 的 webpack.config.jsForge Dataviz IOT Reference App

这是我的 webpack.config.js 文件:

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
  },
  resolve: {
    modules: [path.join(__dirname, 'src'), 'node_modules'],
    alias: {
      react: path.join(__dirname, 'node_modules', 'react'),
      PIXI: path.resolve(__dirname, "node_modules/pixi.js/"),
    },
  },
  devServer: {
    port: process.env.PORT || 3000
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          { loader: "babel-loader" }
        ]
      },
      {
        test: /forge-dataviz-iot-react-component.*.jsx?$/,
        use: [
          {
            loader: "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"),
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
        ],
      },

      {
        test: /\.svg$/i,
        use: {
          loader: "svg-url-loader",
          options: {
            // make loader to behave like url-loader, for all svg files
            encoding: "base64",
          },
        },
      },
    ],
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/index.html',
    }),
  ],
};

更新:

如果你想使用 CRA,你可以使用 Customize-CRA 自定义你的 webpack 配置并创建一个 config-overrides.js 像这样:

/* config-overrides.js */
const path = require("path");
 
const {
    override,
    addExternalBabelPlugins,
    babelInclude,
    babelExclude,
    addWebpackAlias
} = require("customize-cra");



module.exports = override(
    babelInclude([
        path.resolve("src"), // make sure you link your own source
        path.resolve("node_modules")
      ]),
    babelExclude([path.resolve("node_modules/forge-dataviz-iot-react-components/node_modules")]),
    addWebpackAlias({
        ['PIXI']: path.resolve(__dirname, 'node_modules/pixi.js/')
    })
  );

我设法在一个新的 CreateReactApp 项目上完成这项工作,因此您应该能够在您的项目上完成它。

无法将包 https://www.npmjs.com/package/forge-dataviz-iot-react-components 包含在使用 npx create-react-app 制作的 React 项目中(希望 Autodesk 能尽快解决这个问题).

您需要分两部分编辑 /node_modules/react-scripts/config/webpack.config.js

一行关于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 || {}),
      },
...

和另一部分关于 /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]',
              },
            },
...

在那之后 /node_modules/forge-dataviz-iot-react-components/client/components/Viewer.jsx 你会得到关于未定义的 Autodesk 变量的错误很容易修复改变 Autodeskwindow.Autodesk.

虽然您不会看到任何其他错误,但程序包将无法运行。