reactjs – 如何使用jQuery与webpack?

前端之家收集整理的这篇文章主要介绍了reactjs – 如何使用jQuery与webpack?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 webpack开发一个React组件。这里是一个简单的版本:
  1. 'use strict';
  2.  
  3. require('./MyComponent.less');
  4.  
  5. var React = require('react');
  6.  
  7. var MyComponent = React.createClass({
  8. render() {
  9. return (
  10. <div className="my-component">
  11. Hello World
  12. </div>
  13. );
  14. }
  15. });
  16.  
  17. module.exports = MyComponent;

现在,我想使用jest测试这个组件。这里是我的package.json的相关位:

  1. "scripts": {
  2. "test": "jest"
  3. },"jest": {
  4. "rootDir": ".","testDirectoryName": "tests","scriptPreprocessor": "<rootDir>/node_modules/babel-jest","unmockedModulePathPatterns": [
  5. "react"
  6. ]
  7. }

当运行npm测试时,我得到以下错误

SyntaxError: /Users/mishamoroshko/react-component/src/tests/MyComponent.js: /Users/mishamoroshko/react-component/src/MyComponent.js: /Users/mishamoroshko/react-component/src/MyComponent.less: Unexpected token ILLEGAL

看起来像webpack需要处理require(‘./ MyComponent.less’)before jest可以运行测试。

我不知道我是否需要使用像jest-webpack.如果是,有没有办法指定多个scriptPreprocessors? (注意,我已经使用babel-jest)

解决方法

我结束了以下黑客:
  1. // package.json
  2.  
  3. "jest": {
  4. "scriptPreprocessor": "<rootDir>/jest-script-preprocessor",...
  5. }
  6.  
  7.  
  8. // jest-script-preprocessor.js
  9. var babelJest = require("babel-jest");
  10.  
  11. module.exports = {
  12. process: function(src,filename) {
  13. return babelJest.process(src,filename)
  14. .replace(/^require.*\.less.*;$/gm,'');
  15. }
  16. };

但是,我仍然想知道什么是这个问题的正确解决方案。

猜你在找的jQuery相关文章