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

Broken ansi-styles produced by latest @rollup/plugin-commonjs #4882

Closed
coderaiser opened this issue Feb 27, 2023 · 6 comments · Fixed by rollup/plugins#1455
Closed

Broken ansi-styles produced by latest @rollup/plugin-commonjs #4882

coderaiser opened this issue Feb 27, 2023 · 6 comments · Fixed by rollup/plugins#1455

Comments

@coderaiser
Copy link

coderaiser commented Feb 27, 2023

Rollup Version

3.17.3

Operating System (or Browser)

darwin

Node Version (if applicable)

18

Link To Reproduction

To reproduce you can clone the repository: https://github.com/putoutjs/bundle, update @rollup/plugin-common to latest version, then npm install && npm run build && node bundle/putout.js will produce an error (there is no error in @rollup/plugin-common v22)

Expected Behaviour

I'm using chalk, and it has dependency ansi-styles, it works good with @rollup/plugin-commonjs@22, but I have issues using @rollup/plugin-commonjs@23 (same with v24) it converts the code to something like this (simplified a bit):

function requireAnsiStyles () {
	if (hasRequiredAnsiStyles) return ansiStylesExports;
	hasRequiredAnsiStyles = 1;
	(function (module) {
		function assembleStyles() {
			const styles = {
			};

			return styles;
		}

		// Make the export immutable
		Object.defineProperty(module, 'exports', {
			enumerable: true,
			get: assembleStyles
		});
} (ansiStyles));
	return ansiStylesExports;
}

So ansiStylesExports never assigned so it is an empty object.

In opposite @rollup/plugin-commonjs@22 produces next code (simplified):

function requireAnsiStyles () {
	if (hasRequiredAnsiStyles) return ansiStyles.exports;
	hasRequiredAnsiStyles = 1;
	(function (module) {
		function assembleStyles() {
			const codes = new Map();
			const styles = {

			return styles;
		}

		// Make the export immutable
		Object.defineProperty(module, 'exports', {
			enumerable: true,
			get: assembleStyles
		});
} (ansiStyles));
	return ansiStyles.exports;
}

Maybe I missing something in configuration, could you please help me with this?

I use chalk@4 because I need a Yarn Berry support and deduplication.

Actual Behaviour

ansy-styles produces in @rollup/plugin-commonjs@24 in the same way as it was in @rollup/plugin-commonjs@22.

Related to rollup/plugins#1446

@Versirity
Copy link

Versirity commented Mar 7, 2023

I have the same problem. Are there any temporary solutions?

@coderaiser
Copy link
Author

I created a file lib/chalk.js:

export default () => {};

And used alias:

alias({
    entries: [
        {find: 'chalk', replacement: './lib/chalk.js'},
    ]
});

Here is how it looks like: @putout/bundle.

@Versirity
Copy link

Versirity commented Mar 8, 2023

I created a file lib/chalk.js:

export default () => {};

And used alias:

alias({
    entries: [
        {find: 'chalk', replacement: './lib/chalk.js'},
    ]
});

Here is how it looks like: @putout/bundle.

OK.thanks .I will try it

@maecapozzi
Copy link

maecapozzi commented Mar 9, 2023

I'm having the same issue (although mine is showing up in a vite build that relies on @rollup/plugin-commonjs.

Does this workaround only work if chalk isn't used when jsondiffpatch is invoked?

@lukastaegert
Copy link
Member

I tried the reproduction but it did not seem to be broken for me. In any case from the described problems, rollup/plugins#1455 should fix this. I released a beta version for easier testing, could you please verify that the issue is resolved with @rollup/plugin-commonjs@24.1.0-0 ?

@lukastaegert
Copy link
Member

Ah, the last commits in your reproduction add the fix discussed above. If I go back to before that commit, I can see the error and can confirm that the new plugin version would fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants