English 中文(简体)
具有反应成分的微粒
原标题:vite build lib iife with react components

我正试图建立一个React部件图书馆。 目标是把他们称之为:

    <div id="testreact"></div>
    <div id="testreact2"></div>
    <script>
        MyLib.ReactDOM.createRoot(document.getElementById( testreact )).render(
            MyLib.ButtonShowdown(),
        )
        MyLib.ReactDOM.createRoot(document.getElementById( testreact2 )).render(
            MyLib.DrawerShowdown(),
        )
    </script>

ButtonShowdown工程是正确的。

提炼公司利用React.useState公司,但我收到的错误是“Cannot读作(翻新使用国)”在 con体内。

The build is done in Vite using this configuration:`

    import { defineConfig } from  vite 
    import { resolve } from  path ;
    import react from  @vitejs/plugin-react-swc 

    export default defineConfig({
    define: {
         process.env : process.env
    },
    plugins: [react()],
    build: {
        lib: {
            entry: resolve(__dirname,  页: 1 ),
            name:  MyLib ,
            fileName:  my-lib ,
            formats: [ iife ]
        }
    },
    })

页: 1

    import ReactDOM from  react-dom/client 
    import ButtonShowdown from "../src/ButtonShowdown";
    import DrawerShowdown from "../src/DrawerShowdown"

    export {
        ReactDOM,
        ButtonShowdown,
        DrawerShowdown,
    };

许多人似乎已经解决了这一问题,将React and ReactDOM排除在图书馆之外,但这也造成ButtonShowdown工作。

尽管我认为这可能是多余的,但我却为各组成部分提供了法典:

import React from  react 
    import { Button } from "@mui/material";
    import Base from "./Base";

    function ButtonShowdown(){
        return (
            <Base>
                <div>Showdown:</div>
                <Button variant="text">Text</Button>
                <Button variant="contained">Contained</Button>
                <Button variant="outlined">Outlined</Button>
            </Base>
        )
    }

    export default ButtonShowdown;



    import React from  react ;
    import Box from  @mui/material/Box ;
    import Drawer from  @mui/material/Drawer ;
    import Button from  @mui/material/Button ;
    import List from  @mui/material/List ;
    import Divider from  @mui/material/Divider ;
    import ListItem from  @mui/material/ListItem ;
    import ListItemButton from  @mui/material/ListItemButton ;
    import ListItemIcon from  @mui/material/ListItemIcon ;
    import ListItemText from  @mui/material/ListItemText ;
    import InboxIcon from  @mui/icons-material/MoveToInbox ;
    import MailIcon from  @mui/icons-material/Mail ;
    import Base from  ./Base ;

    function DrawerShowdown() {
    const [open, setOpen] = React.useState(false);

    const toggleDrawer = (newOpen) => () => {
        setOpen(newOpen);
    };

    const DrawerList = (
        <Box sx={{ width: 250 }} role="presentation" onClick={toggleDrawer(false)}>
        <List>
            {[ Inbox ,  Starred ,  Send email ,  Drafts ].map((text, index) => (
            <ListItem key={text} disablePadding>
                <ListItemButton>
                <ListItemIcon>
                    {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
                </ListItemIcon>
                <ListItemText primary={text} />
                </ListItemButton>
            </ListItem>
            ))}
        </List>
        <Divider />
        <List>
            {[ All mail ,  Trash ,  Spam ].map((text, index) => (
            <ListItem key={text} disablePadding>
                <ListItemButton>
                <ListItemIcon>
                    {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
                </ListItemIcon>
                <ListItemText primary={text} />
                </ListItemButton>
            </ListItem>
            ))}
        </List>
        </Box>
    );

    return (
        <Base>
        <Button onClick={toggleDrawer(true)}>Open drawer</Button>
        <Drawer open={open} onClose={toggleDrawer(false)}>
            {DrawerList}
        </Drawer>
        </Base>
    );
    }

    export default DrawerShowdown;


    import React from  react 
    import { ScopedCssBaseline, ThemeProvider, createTheme } from "@mui/material";
    import { red } from  @mui/material/colors ;


    function Base({children}){
        const baseTheme = createTheme(window.WpeaUI?.customTheme ?? {
            /*palette: {
                primary: red
            }*/
        });
        return (
            <React.StrictMode>
                <ScopedCssBaseline>
                    <ThemeProvider theme={baseTheme}>
                        {children}
                    </ThemeProvider>
                </ScopedCssBaseline>
            </React.StrictMode>
        )
    }

    export default Base;

只要我执行 n,所有东西都是正确的。 然而,在我尝试冲绳后,我就收到错误:“没有读作废(阅读使用国)的财产”。

问题回答

这也许不是答案,而是试图将你不想冲入你的图书馆的任何附属设施外部化,例如<代码>react和react-dom

build: {
    ...,
    rollupOptions: {
        external: [ react ,  react-dom ]
    },
}

另外,在您的<代码> 包装.json中,确保React and ReactDOM被列为同侪属地。





相关问题
Defining and calling function in one step

Is there a way in Javascript to define a function and immediately call it, in a way that allows it to be reused? I know you can do one-off anonymous functions: (function(i) { var product = i * ...

Self invoking functions javascript

I wrote a self invoking function in both firefox and chrome it it wouldn t invoke. I wrote something to the effect of (function () { alert("THE"); })(); do self invoking functions not work ...

A Javascript function

Please explain the following way of writing a function in javascript functions : (function (){ // some code })() I understand the fact that because of the trailing braces " () ", the function ...

Javascript self executing function "is not a function"

I have: var Init = (function() { my js goes here })(); And my js executes correctly when the page is loaded. I also have: $( form :checkbox ).change(function() { Init(); }); But firebug ...

What is this unknown JavaScript syntax?

Is this jQuery code (function(jQuery){ })(jQuery); equivalent to $(document).ready(function () { }); If yes, what are the differences between the two? If not, what does the first do? EDIT: ...

What is this practice called in JavaScript?

When you wrap your JavaScript code in a function like this: (function(){ var field = ...; function doSomthing(){... ... })(); I noticed that this fixes scoping problems for me on a lot of ...

热门标签