无法在Webpack中ES6导入JSX模块

5
由于某些原因,我似乎无法让Webpack导入JSX文件中的模块。每次尝试运行Webpack时,都会出现以下消息:
ERROR in ./src/Example.jsx
Module parse failed: /path/to/project/src/Example.jsx Unexpected token (6:12)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (6:12)

事实上,./src/Example.jsx中并没有很多内容。实际上,它只包含以下内容:
import React from 'react';


export default class Example extends React.Component{
  render() {
    return (<h2>Hello world!!</h2>);
  }
};

Webpack在我的index.jsx文件中没有任何问题,但当我将它移动到自己的文件中时,Webpack突然不知道该怎么做了。我尝试使用babel-plugin-transform-react-jsx来解决这个问题,但似乎并没有解决我的问题。我需要做什么才能让Webpack正确地转换/解析JSX文件?
/* package.json */
{
  ...,
  "dependencies": {
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack": "1.13.1"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "eslint": "^3.1.1",
    "eslint-plugin-react": "^5.2.2",
    "jest-cli": "^13.2.3",
    "react-addons-test-utils": "^15.2.1",
    "webpack-dev-server": "^1.14.1"
  },
  "scripts": {
    "build": "webpack -p",
    "dev": "webpack-dev-server --port 9999",
    "start": "npm run build && python -m SimpleHTTPServer 9999",
    "test": "jest --verbose --coverage --config jest.config.json"
  }
}

/* webpack.config.js */

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /[^\.spec]+\.jsx$/,
        include: SOURCE_DIR,
        loader: 'babel'
      }
    ]
  }
};

/* .baberc */

{
  "presets": ["es2015", "react"]
}

/* src/index.jsx */

import React from 'react'
import ReactDOM from 'react-dom';

import Example from './Example'


ReactDOM.render(<Example />, document.getElementById('floor-plan'));
2个回答

12

我通过这几个修改让你的设置工作了。我将问题隔离到了你的测试属性。

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: SOURCE_DIR,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react']
        },
      }
    ]
  }
};

非常感谢!这很有帮助。 - Mathijs
configuration.resolve.extensions[0] should be an non-empty string. - Bằng

4

原因可能是在 index.jsx 文件中使用了 import Example from './Example',导致 webpack 将其视为 js 导入而非 jsx

请尝试修改成:

import Example from './Example.jsx'


3
虽然这样做是可行的,但明确提及文件扩展名仍然是不理想的。构建过程应该自动处理这些事情。 - vsync

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接