代码之家  ›  专栏  ›  技术社区  ›  spz1

React和网页包模块解析失败:/Conditionals。js意外令牌(6:4)

  •  2
  • spz1  · 技术社区  · 8 年前

    有人能帮我解决这个错误,我一直得到当我试图运行反应通过网页?下面是我得到的错误。

    控制台错误:

    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>
    

    这是网页包。配置。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
        })
      ]
    }
    

    这是推荐信。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'))
    

    这是推荐信。html文件。注意:我在这个网站上使用了Jekyll和Liquid模板 :此页面通过asset_名称空间读取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>
    

    这是我的包中的依赖项。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 回复  |  直到 8 年前
        1
  •  1
  •   Maluen    8 年前

    更新您的 webpack.config.js 根据网页v2。看见 Migrating Versions 了解更多信息。

    • module.loaders 现在是 module.rules
    • 在引用加载器时,不可能再省略-loader扩展
    • 装载机配置通过选项完成

    更新的 网页包。配置。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
        })
      ]
    }
    
    推荐文章