【问题标题】:Unable to import a jsx module in my react-application with webpack 4 and babel 7无法使用 webpack 4 和 babel 7 在我的 react-application 中导入 jsx 模块
【发布时间】:2019-06-06 06:15:00
【问题描述】:

在浏览了与该主题相关的几乎所有答案后,我没有运气,因此必须创建一个新问题。

问题陈述:

我有一个库(模块),我想在我的 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-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'],
        },
    },
};

我不确定出了什么问题,任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript reactjs webpack babeljs


    【解决方案1】:

    这不是一个有效的语法。

    export * from './button';
    

    您需要列出您导出的内容。

    export {button} 
    

    export default button
    

    【讨论】:

    • 好吧,我不认为它会一直到那里,问题是它告诉导出那里的语法无效。
    猜你喜欢
    • 2019-10-30
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2016-06-15
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多