简体   繁体   English

Gulp和Webpack编译React

[英]Gulp & Webpack compiling React

I am strugling with setting up basic compilation of my React project using Gulp & Webpack 我正在努力使用Gulp和Webpack设置我的React项目的基本编译

Here is my Gulpfile: 这是我的Gulpfile:

function styles() {
  return gulp.src('./src/less/styles.less')
    .pipe(less())
    .pipe(gulp.dest('./dist'))
    .pipe(connect.reload());
}

function copyAssets() {
  return gulp.src('./assets/**')
    .pipe(gulpCopy('./dist/'))
    .pipe(connect.reload());
}


function watchAssets() {
  gulp.watch('assets/**/*', copyAssets);
  gulp.watch('src/less/**/*.less', styles);
}

function webpackBuildgulp() {
  return gulp.src('src/js/index.js')
    .pipe(webpack(webpackConfig))
    .pipe(gulp.dest('dist/'));
}

gulp.task('default', gulp.parallel(styles, copyAssets, webpackBuildgulp, server, watchAssets));

Here is my Webpack config: 这是我的Webpack配置:

module.exports = () => {
  const webpackConfig = {
    entry: './src/js/index.js',
    module: {
      rules: [{
        test: /\.jsx$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'stage-0', 'react'],
          },
        },
      }],
    },
    output: {
      path: path.resolve(__dirname, '../dist'),
      filename: 'app.js',
      publicPath: 'http://localhost:1337/dist/',
    },
    resolve: {
      modules: ['../src/js', 'node_modules'],
      extensions: ['', '.js', 'jsx'],
    },
    plugins: [
      new webpack.ProvidePlugin({
        jQuery: 'jquery',
        $: 'jquery',
      }),
    ],
  };
  return webpackConfig;
};

Here is package.json deps tree: 这是package.json deps树:

"devDependencies": {
  "babel-cli": "^6.24.1",
  "babel-core": "^6.24.0",
  "babel-loader": "^7.0.0",
  "babel-plugin-transform-es2015-modules-commonjs": "^6.7.7",
  "babel-plugin-transform-object-rest-spread": "^6.8.0",
  "babel-plugin-transform-react-jsx": "^6.7.5",
  "babel-preset-airbnb": "^1.0.1",
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-react": "^6.5.0",
  "babelify": "^7.3.0",
  "chalk": "^1.1.3",
  "connect-modrewrite": "^0.9.0",
  "envify": "^3.4.1",
  "enzyme": "^2.4.1",
  "eslint": "^3.11.1",
  "eslint-config-airbnb": "^13.0.0",
  "eslint-plugin-import": "^2.2.0",
  "eslint-plugin-jsx-a11y": "^2.2.3",
  "eslint-plugin-react": "^6.8.0",
  "gulp": "github:gulpjs/gulp#4.0",
  "gulp-callback": "0.0.3",
  "gulp-connect": "^5.0.0",
  "gulp-imagemin": "^3.0.3",
  "gulp-less": "^3.1.0",
  "gulp-uglify": "^2.0.0",
  "gulp-watch": "^4.3.9",
  "gutil": "^1.6.4",
  "husky": "^0.11.6",
  "isparta": "^4.0.0",
  "jasmine-core": "^2.4.1",
  "jsdom": "^8.4.1",
  "karma": "^0.13.19",
  "karma-babel-preprocessor": "^6.0.1",
  "karma-browserify": "^5.0.0",
  "karma-chrome-launcher": "^0.2.2",
  "karma-coverage": "^1.1.0",
  "karma-jasmine": "^0.3.6",
  "karma-nyan-reporter": "^0.2.4",
  "karma-phantomjs-launcher": "^1.0.0",
  "karma-sinon": "^1.0.4",
  "less": "^2.6.1",
  "less-plugin-autoprefix": "^1.5.1",
  "less-plugin-clean-css": "^1.5.1",
  "phantomjs-polyfill-object-assign": "0.0.2",
  "phantomjs-prebuilt": "^2.1.4",
  "react-addons": "^0.9.1-deprecated",
  "react-addons-shallow-compare": "15.4.0",
  "react-addons-test-utils": "15.4.0",
  "react-asset-loader": "0.0.1",
  "react-images": "^0.5.1",
  "react-test-renderer": "15.4.0",
  "require-dir": "^0.3.0",
  "sinon": "^1.17.4",
  "vinyl-buffer": "^1.0.0",
  "vinyl-source-stream": "^1.1.0",
  "watchify": "^3.7.0",
  "webpack-dev-middleware": "^1.10.0",
  "webpack-stream": "^3.2.0",
  "when": "^3.7.7"
},
"dependencies": {
  "animate.css": "^3.5.1",
  "axios": "^0.12.0",
  "classnames": "^2.2.5",
  "es6-promise": "^3.2.1",
  "fullcalendar": "3.3.1",
  "fullcalendar-scheduler": "1.5.0",
  "intl": "^1.2.4",
  "jquery": "2.1.4",
  "jquery-timepicker": "^1.3.3",
  "lodash": "^4.13.1",
  "moment": "2.18.1",
  "moment-range": "^3.0.2",
  "qtip2": "^3.0.3",
  "rc-calendar": "^7.6.5",
  "rc-time-picker": "^2.2.2",
  "react": "15.4.0",
  "react-animated-number": "^0.4.2",
  "react-countup": "^1.3.0",
  "react-datepicker": "^0.41.1",
  "react-dom": "15.4.0",
  "react-dropzone": "^3.5.3",
  "react-geosuggest": "^1.24.1",
  "react-google-maps": "^4.11.0",
  "react-google-tag-manager": "^1.1.2",
  "react-helmet": "^3.1.0",
  "react-imageloader": "^2.1.0",
  "react-intl": "^2.1.3",
  "react-maskedinput": "^3.2.4",
  "react-redux": "^5.0.0",
  "react-redux-toastr": "^3.8.5",
  "react-router": "^2.5.2",
  "react-scrollbar": "^0.4.1",
  "react-sortable": "1.2.0",
  "react-sparklines": "^1.6.0",
  "react-tabs": "^0.7.0",
  "react-typist": "^1.1.0",
  "redux": "^3.5.2",
  "redux-form": "^6.3.2",
  "redux-logger": "^2.6.1",
  "redux-mock-store": "^1.1.2",
  "redux-thunk": "^2.1.0",
  "riek": "^1.0.4",
  "slick-carousel": "^1.6.0",
  "spid-sdk-js": "^2.1.7",
  "vivus": "^0.3.1"
},

However when I trie to compile my project typing gulp I get the following error: 但是,当我尝试编译输入gulp项目时,出现以下错误:

Module parse failed: /src/js/index.js Unexpected token (42:2)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (42:2)

Here is the index.js file: 这是index.js文件:

import ReactDOM from 'react-dom';
import React from 'react';
import { Provider } from 'react-redux';
import { Router, browserHistory } from 'react-router';
import { IntlProvider } from 'react-intl';
import ReduxToastr from 'react-redux-toastr';

import createStore from './store';
import { SIGNIN_USER, getCurrentUser } from './actions/auth';
import { getSalonsBasicInfo } from './actions/landingPage';
import { stuff } from './middleware/stuff';
import { checkSalon } from './actions/salon';


import routes from './routes';

import translations from './translations/en';

if (!window.Intl) {
  require('intl');
}

const store = createStore();
const access_token = localStorage.getItem('access_token');
const refresh_token = localStorage.getItem('refresh_token');

store.dispatch({
  type: INIT,
});

if (access_token || refresh_token) {
  store.dispatch({
    type: SIGNIN_USER,
  });
  store.dispatch(getCurrentUser());
  store.dispatch(checkSalon());
}

localStorage.setItem('debug', false);

ReactDOM.render(
  <Provider store={store}>
    <div>
      <ReduxToastr
        timeOut={4000}
        newestOnTop={false}
        position="top-center"
      />
      <IntlProvider locale={'en-US'} messages={translations}>
        <Router history={browserHistory} routes={routes} />
      </IntlProvider>
    </div>
  </Provider>,
  document.getElementById('app'),
);

Someway, somehow, Babel fails to understand React code and doesnt compile it. 不知何故,Babel无法理解React代码并且不对其进行编译。

However when I run Babel manually: 但是,当我手动运行Babel时:

babel src/js/index.js --presets es2015,react

It works perfectly. 它运作完美。

What am I doing wrong in my Webpack configuration ? Webpack配置中我在做什么错?

Your babel-loader is only testing for .jsx files and you have saved your index as a .js file. 您的babel-loader仅测试.jsx文件,并且已将索引另存为.js文件。 Change test to test: /\\.(js|jsx)$/, and see if that helps. 更改测试以test: /\\.(js|jsx)$/,看看是否有帮助。

    module: {
      rules: [{
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'stage-0', 'react'],
          },
        },
      }],
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM