无法从库中导入反应组件

时间:2019-06-10 09:09:21

标签: reactjs webpack babeljs webpack-4 babel-loader

在经历了与该主题相关的几乎所有答案之后,我没有运气,因此不得不提出一个新问题。

问题陈述:

我有一个要导入到我的react应用程序内部的组件(模块)库,该库具有导出语法,例如(在另一个应用程序中工作):

export * from 'button'; // this returns jsx component

现在,在我的应用程序中,我将其用作:

import {button} from ./library; // this is the bundled module where button component exists. 

当我这样做时 Webpack 给我一个错误:

(function (exports, require, module, __filename, __dirname) { export * from './button';
                                                              ^^^^^^
SyntaxError: Unexpected token export

我正在将Webpack 4和babel 7用于以下配置:

webpack.config.js

//这是出口。

module.exports = {
    entry: {
        client: ['whatwg-fetch', './client/index.js', 'webpack-hot-middleware/client']
    },
    optimization: {
        minimizer: [new OptimizeCSSAssetsPlugin({})]
    },
    output: {
        path: path.resolve(__dirname, `dist/client/${pkg.version}`),
        filename: '[name].js',
        publicPath: `/myApp/${pkg.version}`
    },
    devtool: ifProduction('nosources-source-map', 'source-map'),
    resolve: {
        modules: [path.resolve('./client'), path.resolve('./node_modules')]
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.less$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
            },
            {
                test: /\.(ttf|eot|svg|woff|woff2?)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader?name=fonts/[name].[ext]'
            },
            {
                test: /\.(png|jpg|ico|svg|eot|ttf)$/,
                loader: 'url-loader'
            }
        ]
    },
    mode,
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new MiniCssExtractPlugin(),
        new BundleAnalyzerPlugin({
            analyzerMode: 'static',
            openAnalyzer: false,
            reportFilename: '../report.html'
        })
    ]
};

babel.config.js

//  prettier-ignore
module.exports = {
    presets: [
        '@babel/preset-env',
        '@babel/preset-react',
    ],
    plugins: [
        '@babel/plugin-transform-spread',
        'react-hot-loader/babel',
        'babel-plugin-syntax-export-extensions',
        '@babel/plugin-proposal-export-namespace-from',
        '@babel/plugin-syntax-export-namespace-from',
        '@babel/plugin-syntax-export-default-from',
        '@babel/plugin-syntax-dynamic-import',
        '@babel/plugin-proposal-class-properties',
        ['@babel/plugin-transform-runtime',
            {
                regenerator: true,
            },
        ],
    ],
    env: {
        development: {
            presets: [
                '@babel/preset-env', ['@babel/preset-react', { development: true } ],
            ],
        },
        test: {
            presets: [['@babel/preset-env'], '@babel/preset-react'],
        },
    },
};

我不确定发生了什么问题,我们将不胜感激。

谢谢。

0 个答案:

没有答案