使用React供应商拆分汇总

时间:2018-11-21 04:16:59

标签: javascript reactjs rollup

我正在尝试使用汇总作为React工具集。当我可以在反应中使用延迟加载时,我有一个可以正常工作的原型。我仅使用ES6模块,稍后将集成SystemJS以用于较旧的浏览器。我的问题是关于汇总设置。将reactreact-dom视为内部,然后必须提供manualChunk并再次将它们分开,这似乎很疯狂。

如果我将它们视为外部而不是分块处理,则会出现以下错误:

Uncaught (in promise) TypeError: Failed to resolve module specifier "react-dom". Relative references must start with either "/", "./", or "../".

是否有任何方法可以将reactreact-dom视为外部对象并从CDN引用它们?

import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import replace from "rollup-plugin-replace";

export default {
    input: ['src/index.js'],
    output: {
        format: 'esm',
        dir: 'build',
    },

    experimentalCodeSplitting: true,

    // Is there any way to avoid this?
    manualChunks: {
        'react': ['./node_modules/react/index.js'],
        'reactDOM': ['./node_modules/react-dom/index.js'],
    },
    plugins: [
        json(),
        commonjs({
            include: 'node_modules/**',
            sourceMap: false,
            namedExports: {
                './node_modules/react/index.js': ['Component','Children','PropTypes','createElement','cloneElement', 'createFactory'],
                './node_modules/react-dom/index.js': ['render'],
            },
        }),
        nodeResolve(),
        babel({
            exclude: 'node_modules/**',
        }),
        replace({
            'process.env.NODE_ENV': JSON.stringify('development'),
        })
    ],
    // If react is treated as external, how can it be loaded into this ESM module from a UMD?
    // e.g. "https://unpkg.com/react@16/umd/react.development.js"
    // external: ['react','react-dom'],
};

这是react组件的设置,moduleA.js在单击时被延迟加载:

// src/moduleA.js
const moduleA = 'Hello';
export default { moduleA };

// src/App.js
import React, { Component } from 'react';
import { version } from '../package.json';

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        import('./moduleA')
            .then((module) => {
                console.log(module.default);
            })
            .catch(err => {
                // Handle failure
            });
    };

    render() {
        return (
            <React.Fragment>
                <div className="App">
                    <h1>Hello, World!</h1>
                    <p>This is version: { version }</p>
                </div>
                <button onClick={this.handleClick}>Load</button>
            </React.Fragment>
        );
    }
}

// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));


//html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>#</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <main id="root"></main>
<!-- How can these be referenced in the below module?
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
-->
    <script type="module">
        import('/index.js');
    </script>
</body>
</html>

0 个答案:

没有答案