web-dev-qa-db-fra.com

Erreur de roulement: «Default» n'est pas exportée par Node_Modules / React / Index.js

J'ai un projet où j'utilise Webpack et je veux passer à Rollup.js, mais j'ai des problèmes concernant le plugin @ rollup/plugin-commonjs.

Mon rollup.conf.js

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import nodePolyfills from 'rollup-plugin-node-polyfills';

const config = {
    input: 'site/templates/scripts/master.js',
    output: [
        {
            file: 'site/templates/scripts/master.min.js',
            format: 'cjs'
        }
    ],
    plugins: [
        nodePolyfills(),
        resolve({
            browser: true
        }),
        commonjs({
            include: /node_modules/,
            namedExports: {
                'react': ["useState", "useEffect"],
                '@apollo/client': ['ApolloProvider', 'ApolloClient', 'HttpLink', 'InMemoryCache', 'useQuery', 'gql'],
                'styled-components': [ 'styled', 'css', 'ThemeProvider' ]
            }
        }),
        babel({
            babelrc: true,
            exclude: 'node_modules/**'
        }),
        terser()
    ]
};

export default config;

L'erreur que je reçois et ne sait pas résoudre

site/templates/scripts/master.js → site/templates/scripts/master.min.js...
[!] Error: 'default' is not exported by node_modules/react/index.js, imported by site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js (3:7)
1: 'use strict';
2: 
3: import React from "react";
          ^
4: 
5: const FredhopperProduct = ({
Error: 'default' is not exported by node_modules/react/index.js, imported by site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js
    at error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:10152:30)
    at Module.error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14487:16)
    at handleMissingExport (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14388:28)
    at Module.traceVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14871:24)
    at ModuleScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:13448:39)
    at FunctionScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8661:38)
    at ChildScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8661:38)
    at MemberExpression.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:11285:49)
    at CallExpression$1.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8746:23)
    at CallExpression$1.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:11473:15)

Le https://rollupjs.org/guide/fr/#error-name-is-not-exported-by-module Page ne vous aide pas vraiment, car j'ai tous nommé des exportations que j'utilise dans ma configuration.

7
tiefenb

Enfin j'ai trouvé ce qui n'allait pas. J'avais besoin du plugin @rollup/plugin-replace Pour remplacer process.env.NODE_ENV

Voici le code de travail

Cela nécessitait également des sports supplémentaires.

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import replace from '@rollup/plugin-replace';

import React from 'react';
import ReactIs from 'react-is';
import ReactDOM from 'react-dom';

const config = {
    input: 'site/templates/scripts/master.js',
    output: [
        {
            file: 'site/templates/scripts/master.min.js',
            format: 'cjs'
        }
    ],
    plugins: [
        replace({
            "process.env.NODE_ENV": JSON.stringify("development")
        }),
        nodePolyfills(),
        resolve({
            browser: true
        }),
        commonjs({
            include: /node_modules/,
            namedExports: {
                'react-is': Object.keys(ReactIs),
                'react': Object.keys(React),
                'react-dom': Object.keys(ReactDOM),
                '@apollo/client': ['ApolloProvider', 'ApolloClient', 'HttpLink', 'InMemoryCache', 'useQuery', 'gql'],
                'styled-components': [ 'styled', 'css', 'ThemeProvider' ]
            }
        }),
        babel({
            babelrc: true,
            exclude: 'node_modules/**'
        }),
        terser()
    ]
};

export default config;
4
tiefenb