簡體   English   中英

Webpack:錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object

[英]Webpack: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

我想創建自定義組件並使用 webpack 構建

// 包/按鈕/src/index.js

import React from 'react';

function Button({ href }) {
  return <a href={href}>Test</a>;
}

export default Button;

// webpack.config.js

const path = require('path');

const entries = {
  button: {
    import: '/packages/button/src/index.js',
    filename: './button/dist/button.js',
  },
};

module.exports = {
  entry: { ...entries },
  output: {
    filename: '[name].js',
    path: __dirname + '/packages',
    asyncChunks: true,
    libraryTarget: 'umd',
    globalObject: 'this',
  },
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          {
            loader: 'css-loader',
          },
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                strictMath: true,
              },
            },
          },
        ],
      },

      {
        test: /\.m?js$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-react'],
          },
        },
        resolve: {
          fullySpecified: false,
        },
      },
      {
        test: /\.m?js/,
        resolve: {
          fullySpecified: false,
        },
      },
    ],
  },
  optimization: {
    splitChunks: {
      chunks: 'async',
      minSize: 20000,
      minRemainingSize: 0,
      minChunks: 1,
      maxAsyncRequests: 30,
      maxInitialRequests: 30,
      enforceSizeThreshold: 50000,
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          reuseExistingChunk: true,
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true,
        },
      },
    },
  },
  resolve: {
    alias: {
      Styles: path.resolve(__dirname, '/shared/styles'),
    },
  },
};

它構建組件但是當我想從 node_modules 導入它時出現錯誤

// 應用程序.js

import React from 'react';
import Button from '@custom/button';

const App = () => {
   return <Button />
}

錯誤:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:object。

我已將捆綁器從 webpack 切換到匯總,它解決了我的問題

//匯總.config.js

import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import postcss from 'rollup-plugin-postcss';

const packages = ['button', 'select'];
const COMMON = {
  external: ['react', 'react-dom'],
  plugins: [
    resolve({ extensions: ['.jsx', '.js', '.tsx'] }),
    postcss({
      minimize: true,
      modules: true,
      use: {
        sass: null,
        stylus: null,
        less: { javascriptEnabled: true },
      },
    }),
    babel({
      extensions: ['.jsx', '.js', '.tsx'],
      exclude: 'node_modules/**',
      presets: ['@babel/preset-env', '@babel/preset-react'],
    }),
    commonjs(),
  ],
};

const ENTRIES = packages.map((pkg) => ({
  input: `./packages/${pkg}/src/index.js`,
  output: {
    file: `./packages/${pkg}/dist/index.js`,
    exports: 'default',
    format: 'cjs',
  },
  ...COMMON,
}));

export default ENTRIES;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 錯誤:元素類型無效:預期為字符串(對於內置組件)或類/函數(對於復合組件)但得到:ReactJS 中的對象 錯誤:元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:對象 NextJS:錯誤 - 元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object × 錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object 元素類型無效:應為字符串(對於內置組件)或類/函數(對於復合組件)但得到:object。abcd Material UI - 元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:object 不變違規:元素類型無效:預期為字符串(內置組件)或類/函數(復合組件),但得到:對象 錯誤 - 錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:未定義 錯誤:元素類型無效:需要一個字符串(對於內置組件)或一個類/函數(對於復合組件)但得到:未定義 渲染錯誤元素類型無效:預期為字符串(對於內置組件)或類/函數(對於復合組件)但得到:未定義
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM