由于某些原因,我似乎无法让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/exple.jsx中没有很多。事实上,这就是它所包含的全部内容:
import React from 'react';
export default class Example extends React.Component{
render() {
return (<h2>Hello world!!</h2>);
}
};Webpack在我的index.jsx文件中有类的时候没有任何问题,但是当我把它移到它自己的文件时,webpack突然想不出该怎么做了。我试图通过使用巴贝尔-插件-转换-反应-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'));发布于 2016-07-22 15:29:05
我让你的设置和这些修改一起工作。我将问题隔离到您的测试属性中。
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']
},
}
]
}
};发布于 2016-07-22 15:17:55
原因可能是import Example from './Example' in index.jsx
webpack将把这视为js进口,而不是jsx。
试一试
import Example from './Example.jsx'
https://stackoverflow.com/questions/38529554
复制相似问题