Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR not working with cjs modules #13977

Closed
7 tasks done
dmitryshelomanov opened this issue Jul 28, 2023 · 5 comments
Closed
7 tasks done

SSR not working with cjs modules #13977

dmitryshelomanov opened this issue Jul 28, 2023 · 5 comments

Comments

@dmitryshelomanov
Copy link

Describe the bug

I try to build ssr project, but cant to build production with cjs modules
And simple demo well works with hack ( ssr: {
noExternal:
mode === "production" ? ["react-copy-to-clipboard", "date-fns"] : [],
},), but crash with any error (filename.js not existed, but it already)

Reproduction

https://stackblitz.com/edit/github-vke2fz-67onp4?file=README.md

Steps to reproduce

yarn dev - failed
yarn run build
yarn run prod - failed (with noExternal its ok, but dev failed)

System Info

*

Used Package Manager

yarn

Logs

Validations

@stackblitz
Copy link

stackblitz bot commented Jul 28, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@dmitryshelomanov
Copy link
Author

image file not found, but exists ?

@benmccann
Copy link
Collaborator

This looks to me like it's probably a usage/packaging issue. That library distributes CJS, but doesn't expose named exports with module.exports.CopyToClipboard = ... and just has a single export.

Does it work better if you change import { CopyToClipboard } from 'react-copy-to-clipboard'; to

import * as ReactClipboard from 'react-copy-to-clipboard';
...
<ReactClipboard.CopyToClipboard ...

Or maybe

import ReactClipboard from 'react-copy-to-clipboard';
...
<ReactClipboard.CopyToClipboard ...

I'm not exactly sure how to run your reproduction and test it, but I expect one of those would fix it.

@hai-x
Copy link
Contributor

hai-x commented Jul 31, 2023

benmccann

I think you are right. CopyToClipboard expose default exports, instead of named exports.

When yarn dev, ebuild handle the esm & cjs interop problem.

// input
import { CopyToClipboard } from "react-copy-to-clipboard";
console.log(CopyToClipboard);
// output
var import_react_copy_to_clipboard = __toESM(require_lib(), 1);
console.log(import_react_copy_to_clipboard.CopyToClipboard);

When yarn build & prod, CopyToClipboard is imported absolutely from node_modules, instead of bundle

"use strict";
var _require = require('./Component'),
    CopyToClipboard = _require.CopyToClipboard;

CopyToClipboard.CopyToClipboard = CopyToClipboard;
module.exports = CopyToClipboard;

I just try solutions provided by @benmccann , and it works for me.

@benmccann
Copy link
Collaborator

Great! I'll go ahead and close this then

@github-actions github-actions bot locked and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants