【问题标题】:Webpack jsx module loader fails when include given包含给定时Webpack jsx模块加载器失败
【发布时间】:2018-07-11 01:56:27
【问题描述】:

我将我的问题归结为最低限度的测试。我有一个 App.js 文件,其中包含一些 jsx,将被编译为 ./bundle.js:

// App.js
module.exports = () => <div/>

我的 webpack 配置看起来很标准

module.exports = {
    entry: './App.js',
    output: {
        filename: './bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                //include: ['./includes'],  // <- Problem line
                query: {
                    plugins: [
                        ['transform-react-jsx']
                    ]
                }
            }
        ]
    }
}

只有在删除带有include 的行时才能正确编译。如果我启用该行,则会出现错误

Module parse failed: ./App.js Unexpected token (1:23)
You may need an appropriate loader to handle this file type.
| module.exports = () => <div/>

我不知道为什么当我为加载程序指定包含时它会失败,即使 include: [] 也会失败并出现同样的错误。

有人有想法吗?

【问题讨论】:

    标签: webpack babeljs jsx babel-loader


    【解决方案1】:

    include 就像test

    • include: [] 表示“不包含任何内容”`
    • include: ["./include"] 表示“只包含include 目录中的文件”

    如果这些将排除./App.js

    【讨论】:

    • 谢谢,这绝对有道理。但是,include 会是什么样子?即使使用include: ['.', './App.js', './includes'],,我也会遇到同样的错误
    • 那是因为App.js__dirname 中。你为什么要首先添加这个?
    • 我希望相对路径也能正常工作。我正在努力制作一个通用的仅限客户端的 Web 应用程序,并正在尝试以一种干净的方式注入仅限客户端的代码。
    猜你喜欢
    • 2018-02-13
    • 2018-12-20
    • 2016-04-09
    • 2019-11-03
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    相关资源
    最近更新 更多