ReactJS在其他应用程序中加载返回模块解析index.js中的ERROR失败

时间:2018-05-27 04:21:38

标签: javascript reactjs webpack

我正在使用一个非常新的JS开发环境(here for details)并根据this documentation React很容易实现到其他JS框架中。

在我的index.js文件中,它位于我的目录的根目录下,我有以下代码:

[HttpPost]
public async Task<IActionResult> Send([Bind("File")] MyViewModel myVM)
{
   if (myVM.File?.Length > 0)
   {
            byte[] fileBytes;
            using (var fileStream = myVM.File.OpenReadStream())
            using (var ms = new MemoryStream())
            {
                fileStream.CopyTo(ms);
                fileBytes = ms.ToArray();
            }

            var fileName = Path.GetFileName(myVM.File.FileName);
            var fileMimeType = myVM.File.ContentType;
            var fileContent = fileBytes;

            //You have all the file attributes and content

   }
}

当我去运行import React, { Component } from "react"; import ReactDOM from "react-dom"; function Button() { return <button id="btn">Say Hello</button>; } ReactDOM.render( <Button />, document.getElementById('container'), function() { $('#btn').click(function() { alert('Hello!'); }); } ); 时,我收到以下错误:

   ./index.js中的错误模块解析失败:意外的令牌(8:9)你   可能需要一个合适的加载器来处理这种文件类型。 | |功能   Button(){|回来说你好; | } |

这是我的 package.json 文件:

fly server

和我的webpack.config:

{
  "name": "fly-example",
  "version": "1.0.0",
  "description": "fly example app",
  "main": "index.js",
  "dependencies": {
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^0.28.11",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "style-loader": "^0.21.0",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.0.14",
    "webpack-dev-server": "^3.1.3"
  },
  "scripts": {
    "test": "mocha"
  },
  "repository": {
    "type": "git",
    "url": "none"
  },
  "keywords": [
    "fly",
    "app"
  ],

.babelrc文件:

const path = require("path");
const webpack = require("webpack");
const bundlePath = path.resolve(__dirname, "dist/");

module.exports = {
  entry: "./index.js",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        options: { presets: ['env'] }
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      }
    ]
  },
  resolve: { extensions: ['*', '.js', '.jsx'] },
  output: {
    publicPath: bundlePath,
    filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname,'public'),
    port: 3000,
    publicPath: "http://localhost:3000/dist"
  },
  plugins: [ new webpack.HotModuleReplacementPlugin() ]
};

React仍然相当新,但我喜欢在其中构建应用程序,但我真的想学习如何将它集成到其他JS框架中,以便我可以更广泛地使用它。

package.json文件或webpack.config中是否缺少某些内容?我已经做了相当多的环顾四周,但没有找到很多。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试在babel-loader下指定use.loader。见下文:

const path = require("path");
const webpack = require("webpack");
const bundlePath = path.resolve(__dirname, "dist/");

module.exports = {
  entry: "./index.js",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        use: {
            loader: 'babel-loader',
        }
        options: { presets: ['env'] }
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      }
    ]
  },
  resolve: { extensions: ['*', '.js', '.jsx'] },
  output: {
    publicPath: bundlePath,
    filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname,'public'),
    port: 3000,
    publicPath: "http://localhost:3000/dist"
  },
  plugins: [ new webpack.HotModuleReplacementPlugin() ]
};

我建议不要自己编写webpack配置,而是建议使用create-react-app,这样更简单。还要确保导入jquery。

尝试让我知道这是否有效!