官术网_书友最值得收藏!

Debugging in the browser

We can also use our code directly in the browser. There may be times when we want to see the effects of a change, without a build step. In such cases, we can try something like this:

$ npm install --save babel-core
$ npm install --save systemjs

These will give us access to a browser-based dependency manager and cross-compiler; that is, we can use unbundled source code in an example HTML file:

<!DOCTYPE html>
<html>
    <head>
        <script src="/node_modules/babel-core/browser.js"></script>
        <script src="/node_modules/systemjs/dist/system.js"></script>
    </head>
    <body>
        <p class="react"></p>
        <script>
            System.config({
                "transpiler": "babel",
                "map": {
                    "react": "/examples/react/react",
                    "react-dom": "/examples/react/react-dom",
                    "page": "/src/page"
                },
                "defaultJSExtensions": true
            });

            System.import("main");
        </script>
    </body>
</html>

This uses the same unprocessed main.js file as before, but we no longer need to rebuild it after each change to the source code. The System is a reference to the SystemJS library we just installed through NPM. It takes care of the import statements, loading those dependencies with Ajax requests.

You may notice the references to react and react-dom. We import these in main.js, but where do they come from? Browserify fetches them out of the node_modules folder. When we skip the Browserify step, we need to let SystemJS know where to find them.

The easiest place to find these files is at https://facebook.github.io/react. Click on the download button, extract the archive, and copy the JS files in the build folder to where they are referenced in the HTML page.

The ReactJS website is a great place to download ReactJS, and find documentation about how you can use it:

主站蜘蛛池模板: 都昌县| 富源县| 云安县| 元氏县| 宾阳县| 平南县| 昌平区| 镇江市| 芦溪县| 丰台区| 宜君县| 鹤岗市| 南陵县| 阜南县| 靖边县| 宁阳县| 屏东市| 漳州市| 林芝县| 马关县| 商都县| 南皮县| 甘谷县| 谷城县| 南召县| 九江市| 新昌县| 缙云县| 博乐市| 久治县| 富阳市| 徐州市| 平利县| 小金县| 乐至县| 读书| 华亭县| 田东县| 嘉峪关市| 清原| 临江市|