【问题标题】:Change script type to "text/babel" using requireJS使用 requireJS 将脚本类型更改为“text/babel”
【发布时间】:2016-02-06 17:24:00
【问题描述】:

我正在使用 requireJS 加载 React 组件,但我收到错误“Uncaught SyntaxError: Unexpected token docs 和 explained in this question 的说明设置 scriptType,但我无法让它工作或找到一个很好的例子来说明如何使这个工作。

requireConfig.js:

requirejs.config({
    baseUrl: 'scripts/',
    paths:
    {
        jquery: 'jquery-1.9.0',
        react: 'libs/build/react',
        reactdom: 'libs/build/react-dom',
        browser: '//cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min',
        inputWindow: 'inputWindow/inputWindow'
    },
    scriptType: {
        'inputWindow': "text/babel"
     }
});

define(function (require) {
    var InputWindow = require('inputWindow');

    InputWindow.initialize();


});

inputWindow.js:

define(function(require){

    var React = require('react');
    var ReactDOM = require('reactdom');

    var InputWindow = React.createClass({

        render: function(){
            return(<div>
                {this.props.message}
            </div>)     
        }

    });

    function initialize(){
        ReactDOM.render(<InputWindow message="Hello World!"/>, document.getElementById('inputWindowDiv'))
    }

    return {
        initialize: initialize,
    }
})

当我使用该部分配置 requireConfig.js 时

scriptType:{
    'inputWindow':'text/babel'
}

然后将文件 inputWindow.js 加载到带有标签的 index.html 中

type="[Object Object]"

直到 requireJS 超时。

screen capture of inputWindow.js loaded with type=[Object Object]

【问题讨论】:

  • 对不起,我现在没有答案,但我想知道,requirejs 是必需的吗?我认为有更简单的方法来加载和转换 jsx / react 组件。
  • 这不是必需的,但它是我熟悉的工具。我想知道这个问题的答案,即使最终的最佳答案是使用不同的工具。

标签: reactjs requirejs


【解决方案1】:

代替

scriptType: {
    'inputWindow': "text/babel"
}

试试

scriptType: 'text/babel'

它应该工作。现在你正在尝试对一个对象进行字符串化,所以难怪它不起作用。 ;)

【讨论】:

  • 行得通!我不确定的一件事是为什么它只更改了 inputWindow 文件的类型。 requirejs 仍然会超时加载文件,但我认为这与其他一些错误有关。该文件在超时之前以“text/babel”类型加载。
  • 可能是因为你想使用github.com/mikach/requirejs-babel 而不是你奇怪的配置。 :) 记得在 r.js 中使用 stubModules 进行编译!以下是在另一个插件中实现相同功能的方法:npmjs.com/package/requirejs-babel-plugin
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多