无法在Gulp项目中导入React Router Dom

时间:2017-06-08 06:06:38

标签: javascript reactjs gulp react-router-dom

这是我第一次体验到这一点。我尝试在react-route-dom中导入index.jsx。但是当我尝试导入它时,我遇到了以下错误消息:

  

'react-router-dom'由projectMyAccount.Site \ project.MyAccount.Site.Desktop \ scripts \ js \ desktop \ bundle \ app.js导入,但无法解析 - 将其视为外部依赖项   在options.globals中没有为外部模块'react-router-dom'提供名称 - 猜测'reactRouterDom'

指数Jsx:

import Index from '../pages/MyAccount/Index';
import Support  from '../pages/Support/Index';
import {BrowserRouter as Router, Route,Link} from 'react-router-dom';

class Pager extends React.Component {

    constructor(props){
        super(props);
    }

    render(){
        <Router>
     <div>
         <ul>
             <li><Link to="/test1">Home</Link></li>
             <li><Link to="/about-us">About Use</Link></li>
         </ul>

         <hr/>

         <Route path="/hesabim" component={Index}/>
         <Route path="/about" component={Support}/>
     </div>
 </Router>
    }
};

主要任务:

gulp.task('es6', function () {
  return gulp.src(filePath + '/scripts/js/**/*.js')
        .pipe(babel())
        .pipe(gulp.dest(filePath + '/scripts/js'));
});

gulp.task('react', function () {
    return gulp.src(filePath + '/scripts/jsx/**/*.jsx')
        .pipe(react({harmony: false, es6module: true}))
        .pipe(gulp.dest(filePath + '/scripts/js/'));
});

gulp.task('create-app', function () {
  return gulp.src([
      filePath + '/Scripts/js/desktop/pages/Index.js'
    ])
    .pipe(concatJs("hepsiburada.js"))
    .pipe(gulp.dest(filePath + '/scripts/js/desktop/bundle'))
    .pipe(rollup({
      allowRealFiles: true,
      "format": "iife",
      "plugins": [
        require("rollup-plugin-babel")({
          "presets": [["es2015", { "modules": false }]],
          "plugins": ["external-helpers"]
        })
      ],
      entry: filePath + '/scripts/js/desktop/bundle/app.js'
    }))
    .pipe(gulp.dest(filePath + '/build/scripts/'));
});

有什么问题?我是否必须为gulp导入另一个包?我不清楚。我可以将它与unpkg包一起使用,如下所示:

import Index from '../pages/MyAccount/Index';
import Support  from '../pages/Support/Index';
class Pager extends React.Component {

    constructor(props){
        super(props);
    }

    render(){

        const Router = ReactRouterDOM.BrowserRouter;
        const Link = ReactRouterDOM.Link;
        const Route = ReactRouterDOM.Route;

        return (<Router>
     <div>
         <ul>
             <li><Link to="/test1">Home</Link></li>
             <li><Link to="/about-us">About Use</Link></li>
         </ul>

         <hr/>

         <Route path="/hesabim" component={Index}/>
         <Route path="/about" component={Support}/>
     </div>
 </Router>);
    }

};

但我认为长期发展并不好。如何从节点模块实现它?

1 个答案:

答案 0 :(得分:1)

看起来您可能需要将rollup-plugin-node-resolve添加到您的gulp中。请在此处查看可能的答案:gulp rollup not working external resource import