2016-07-20 40 views
1

在運行webpack以編譯jsx語法時出現分析錯誤。如果有人能指出我的錯誤,將不勝感激。我看到一個類似的問題Webpack, React, JSX, Babel - Unexpected token <,但解決方案建議這對我無效。簡單的Webpack + React + ES6 + babel示例不起作用。意外的令牌錯誤

這是我的配置文件的方式是這樣的:

的package.json

{ 
    "name": "dropdowns", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "dependencies": { 
    "react": "^15.2.1", 
    "react-dom": "^15.2.1", 
    "babel-core": "^6.11.4", 
    "babel-loader": "^6.2.4", 
    "babel-preset-es2015": "^6.9.0", 
    "babel-preset-react": "^6.11.1" 
    }, 
    "devDependencies": { 
    "webpack": "^1.13.1", 
    "webpack-dev-server": "^1.14.1" 
    }, 
    "author": "", 
    "license": "ISC" 
} 

我webpack.config.js文件

module.exports = { 
    context: __dirname + "/app", 
    entry: "./main.js", 

    output: { 
     filename: "bundle.js", 
     path: __dirname + "/dist" 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.js$/, 
       exclude: /node_modules/, 
       loader: 'babel-loader', 
       query: { 
        presets: ['es2015', 'react'] 
       } 
      } 
     ] 
    } 


}; 

在本地app文件夾我有主.js和IdMappingOptions.js如下:

// in IdMappingOptions.js 
import React from 'react'; 
class IdMappingOptions extends React.Component { 
    render() { 
     return <span>Hello!</span> 
    } 
} 
export default IdMappingOptions; 
// in main.js 
    import React from 'react'; 
import { render } from 'react-dom'; 
import IdMappingOptions from './IdMappingOptions'; 

render(
    <IdMappingOptions/>, document.body 
); 

運行node_modules /的.bin /的WebPack時,我得到了下面的錯誤跟蹤:

Hash: 396f0bfb9d565b6f60f0 
Version: webpack 1.13.1 
Time: 37ms 
    [0] ./main.js 0 bytes [built] [failed] 
ERROR in ./main.js 
Module parse failed: /scratch/parallel/repository/dropdowns/app/main.js Unexpected token (6:4) 
You may need an appropriate loader to handle this file type. 
SyntaxError: Unexpected token (6:4) 
at Parser.pp.raise (/scratch/parallel/repository/dropdowns/node_module/acorn/dist/acorn.js:923:13) 

編輯:每評論 如下固定的測試圖案,並在webpack.config.js添加巴貝爾核心。這是我的directory structure

+0

你可以分享你的應用程序文件夾結構? – Vikramaditya

回答

3

您的測試似乎錯誤:

test: "/.js$" 

試試這個:

test: /\.js$/ 
相關問題