React和Webpack模块解析失败:/testimonials.js意外的令牌(6:4)

时间:2017-11-20 22:29:27

标签: javascript reactjs webpack jsx babel

有人可以帮我解决当我尝试通过webpack运行React时遇到的这个错误吗?以下是我得到的错误。

控制台错误:

ERROR in ./testimonials.js
Module parse fai,led: src/js/testimonials.js Unexpected token (6:4)
You may need an appropriate loader to handle this file type.
| const HelloWorld = () => {
    |   return (
        |     <div className='hello-world'>
        |       <h1>Hello World</h1>
        |       <p>Welcome to my world</p>

这是webpack.config.js文件

'use strict'

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

module.exports = {
  context: __dirname + "/src/js",
  devtool: 'source-map',
  entry: {
    'global': './global/app.js',
    'legal': './legal.js',
    'blogagg': './blog-agg.js',
    'newsagg': './news-agg.js',
    'events' : './events.js',
    'updates': './product-updates.js',
    'signup': './signup.js',
    'contact': './contact.js',
    'testimonialsjs': './testimonials.js'
  },
  output: {
    path: __dirname + "/_site/js",
    filename: "[name].min.js"
  },
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/,
        query: {
          cacheDirectory: true,
          presets: ['react', 'es2015']
        }
      }
    ],
    rules: [
      {
        test: /\.json$/,
        use: 'json-loader'
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new webpack.optimize.UglifyJsPlugin({
      parallel: true
    })
  ]
}

以下是testimonials.js文件

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

const HelloWorld = () => {
  return (
    <div className='hello-world'>
      <h1>Hello World</h1>
      <p>Welcome to my world</p>
    </div>
  )
}

ReactDOM.render(<HelloWorld />, document.getElementById('app'))

这是testimonials.html文件。注意:我将Jekyll与Liquid模板一起使用:此页面通过asset_namespace读取js文件。我的react id =“app”div组件在此文件中。

---
layout: base-layout
title: Testimonials
has_js_app: true
asset_namespace: testimonialsjs
has_breadcrumbs: false
title: Testimonials
---

<main class="testimonials">
  <div class="blog section">
    <div class="grid-container">
      <div id='app'></div>
      <h2>5 Star Service and Support</h2>
      <div class="grid-x grid-margin-y align-center card-grid">
        {% for testimonials in page.testimonials %}
        <div class="cell testimonial-card" data-number="{{forloop.index}}">
          <p>{{testimonials.testimonial_text}}</p>
          <p class="author-name">{{testimonials.author_name}}</p>
        </div>
        {% endfor %}
      </div>
    </div>
  </div>
</main>

以下是我的package.json文件中的依赖项

"devDependencies": {
    "a11y": "^0.5.0",
    "autoprefixer": "^6.7.7",
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-eslint": "^6.1.2",
    "babel-loader": "^6.4.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "browser-sync": "^2.18.13",
    "css-loader": "^0.28.7",
    "html-loader": "^0.5.1",
    "json-loader": "^0.5.4",
    "node-sass": "^4.6.1",
    "parallelshell": "^3.0.1",
    "path": "^0.12.7",
    "postcss": "^6.0.2",
    "postcss-cli": "^4.1.0",
    "postcss-flexbugs-fixes": "^3.0.0",
    "psi": "^3.0.0",
    "style-loader": "^0.19.0",
    "webpack": "^2.7.0",
    "webpack-dev-server": "^2.9.4",
    "webpack-stream": "^3.2.0"
  },
  "dependencies": {
    "babel-polyfill": "^6.23.0",
    "babel-preset-stage-3": "^6.24.1",
    "foundation-sites": "6.4.3",
    "jquery": "3.1.1",
    "marketo-rest-api": "^0.2.0",
    "motion-ui": "^1.2.3",
    "node": "^9.2.0",
    "pug": "^2.0.0-rc.3",
    "pug-cli": "^1.0.0-alpha6",
    "react": "^16.1.1",
    "react-dom": "^16.1.1",
    "script-loader": "^0.7.0"
  }

1 个答案:

答案 0 :(得分:1)

根据webpack v2更新webpack.config.js。有关详细信息,请参阅Migrating Versions

  • module.loaders现在是module.rules
  • 在引用加载器
  • 时,不再可以省略-loader扩展名
  • Loader配置是通过选项

更新的webpack.config.js如下:

'use strict'

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

module.exports = {
  context: __dirname + "/src/js",
  devtool: 'source-map',
  entry: {
    'global': './global/app.js',
    'legal': './legal.js',
    'blogagg': './blog-agg.js',
    'newsagg': './news-agg.js',
    'events' : './events.js',
    'updates': './product-updates.js',
    'signup': './signup.js',
    'contact': './contact.js',
    'testimonialsjs': './testimonials.js'
  },
  output: {
    path: __dirname + "/_site/js",
    filename: "[name].min.js"
  },
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  module: {
    rules: [
      {
        test: /\.json$/,
        use: 'json-loader'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['react', 'es2015']
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new webpack.optimize.UglifyJsPlugin({
      parallel: true
    })
  ]
}