0

我正在尝试设置react-datepicker 页面上提供的代码。

我的构建工具链由 npm 和 browserify 组成。

我有以下 package.json

{
  "name": "datepicker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "browserify -o public/bundle.js -v -d main.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babelify": "^7.0.0",
    "browserify": "^12.0.1",
    "browserify-css": "^0.14.0",
    "react": "16.5",
    "react-datepicker": "^2.0.0",
    "react-dom": "^16.5",
    "reactify": "^1.1.1"
  },
  "browserify-css": {
    "autoInject": true,
    "minify": true,
    "rootDir": ".",
    "output": "backend/public/bundle.css"
  },
  "browserify": {
    "transform": [
      "babelify",
      "browserify-css"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2"
  }
}

我的 main.js 组成如下:

  import React from "react";
  import ReactDOM from "react-dom"
  import DatePicker from "react-datepicker";


// CSS Modules, react-datepicker-cssmodules.css
 import 'react-datepicker/dist/react-datepicker-cssmodules.css';

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      startDate: new Date()
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(date) {
    this.setState({
      startDate: date
    });
  }

  render() {
    return (
      <DatePicker
        selected={this.state.startDate}
        onChange={this.handleChange}
      />
    );
  }
}

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

然后我跑

npm run start

我收到了这条信息

语法错误:/home/mylogin/test_react_go/datepicker/main.js:意外令牌(26:6)

我花了一些时间来理解这个问题,但还没有找到任何解决方案。如果有人更习惯于现代 js 工具可以帮助我,我会很高兴。

4

2 回答 2

1

您将 reactify 添加为依赖项,但未将其放在 browserify 转换列表中。

看看这个设置,这样你就可以比较并做出更好的设置。

于 2019-01-06T19:05:56.363 回答
1

这是我的解决方案(可行),如果开发人员来自后端语言,请浏览到这个 StackOverflow 问题:) 你可以在下面找到一个 package.json,它能够在两个文件 bundle.js 中生成反应项目 & bundle.css 来自 ES6 代码源。

{
  "name": "datepicker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "browserify -o 'public/bundle.js' -v -d main.js -p [ parcelify -o public/bundle.css ] "
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "16.5",
    "react-datepicker": "^2.0.0",
    "react-dom": "^16.5"
  },
  "browserify": {
    "transform": [
      [
        "babelify",
        {
          "presets": [
            "@babel/preset-env",
            "@babel/preset-react"
          ]
        }
      ]
    ]
  },
  "style": [
    "./node_modules/react-datepicker/dist/react-datepicker-cssmodules.css"
  ],
  "devDependencies": {
    "parcelify": "^2.2.0",
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babelify": "^10.0.0",
    "babel-plugin-react-css-modules": "^5.0.0",
    "browserify": "^12.0.1"
  }
}

我愿意发表评论或改进。

于 2019-01-07T22:09:54.647 回答