9

I'm experimenting with using Rollup as a React toolset. I've got a working prototype when I can use lazy loading in react. I'm just working with just ES6 Modules, I'll integrate SystemJS for older browsers later. My question is regarding the rollup setup. It seems crazy to treat react and react-dom as internal, then have to supply a manualChunk and split them out again.

If I treat them as external and not chunk them I get the following error:

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

Is there any way to treat react and react-dom as external and reference them from a 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'],
};

Here's the react component setup, moduleA.js is lazy loaded on click:

// 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>
sansSpoon
  • 2,115
  • 2
  • 24
  • 43

0 Answers0