Webpack捆绑包未导出,无法导入

时间:2019-01-22 16:30:45

标签: javascript reactjs webpack

我使用Create-React-app创建了我的反应项目A 。然后,将它们与Webpack捆绑在一起,并保存在我的Git帐户中。 现在,我在不同的目录中创建另一个项目(称为项目B )。直接从git下载 Project A 。并尝试像这样使用它:

import React from 'react';
import ReactDOM from 'react-dom';
import { Main } from 'project-A/dist/main'

ReactDOM.render(<Main />, document.getElementById('root'));

我收到如下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

项目A 中的Webpack如下:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const nodeExternals = require("webpack-node-externals");

module.exports = [
    {
        /*Client Side*/
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    use: {
                        loader: "babel-loader"
                    }
                },
                {
                    test: /\.html$/,
                    use: {
                        loader: "html-loader",
                        options: { minimize: true }
                    }
                },
                {
                    test: /\.css$/,
                    use: [MiniCssExtractPlugin.loader,"css-loader"]
                }
            ]
        },
        plugins: [
            new HtmlWebPackPlugin({
                template: "./public/index.html",
                filename:"./index.html"
            }),
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename:"[id].css"
            })
        ]
    }
]

我通过github进行了研究,并尝试更改名称import,但仍然无法正常工作。

项目A的组件如下所示:

App.js:

  render() {
    return (
      <div>
        {this.renderCodeAuthCard()}
        {this.renderConfirmCard()}
        {this.renderVerifyCard()}
      </div>
    );
  }

export default App;

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

显然,webpack不会导出在Project A中创建的捆绑文件。由于导入会产生“ undefine”。

我正试图找到一种方法来导出我的webpack捆绑文件并在另一个项目中使用它。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

这是因为您没有从项目A index.js中导出任何内容。 npm安装的库从index.js导出功能。