Skip to content

fix: default package exports#15869

Closed
userquin wants to merge 4 commits into
tailwindlabs:mainfrom
userquin:fix-default-cjs-exports
Closed

fix: default package exports#15869
userquin wants to merge 4 commits into
tailwindlabs:mainfrom
userquin:fix-default-cjs-exports

Conversation

@userquin

@userquin userquin commented Jan 25, 2025

Copy link
Copy Markdown

This PR includes the following changes to the packages/tailwindcss package (check Context):

  • fixes the default packages exports for node10 and node16 cjs types
  • export types in plugin.ts module, cannot be used with Typescript 5.7
  • add "type": "commonjs" to package.json
  • add ./lib/util/flattenColorPalette.js variant to package.json exports (publishConfig)

Context
The current version at npm registry has all types broken for all packages exports. The problem seems to be with tsup since it is not applying the .cts hack, all d.ts files using export { X as default };, check https://arethetypeswrong.github.io/?p=tailwindcss%404.0.0 .

With this PR (for context check this PR unjs/unbuild#475 and this md file https://github.com/unjs/unbuild/blob/a011a7831e2004688bf44716f5e9c9377d08ebec/src/builders/rollup/plugins/cjs.md):

image

This PR includes a new script module that runs after tsup finish building the dist, extracting the default export and applying some replacements in the files including the default export correclty: export = X; and remaining named exports in the default export statement.

About exporting the types in the plugin.ts module, when using TypeScript 5.7 there is an error. Using the following code with any module resolution

import plugin from 'tailwindcss/plugin'

const exportedPlugin = plugin.withOptions(() => {
	return ({ matchComponents }) => {
		matchComponents({
			test: (content: string) => {
				return {
					color: content,
				};
			},
		});
	};
});

export default exportedPlugin;

and building it via tsc -b, there is an error about required annotation:

image

Using a local tgz from this PR, TypeScript is fine when building the module:

image

You can generate the local tgz using this PR in the following repository (use packages/plugin): https://github.com/iconify/iconify-tailwind

@userquin userquin requested a review from a team as a code owner January 25, 2025 21:28
Comment thread packages/tailwindcss/fix-default-cjs-exports.mjs Outdated
@userquin

Copy link
Copy Markdown
Author

We should add "type": "commonjs" to the package.json.

Comment thread packages/tailwindcss/package.json
@userquin

Copy link
Copy Markdown
Author

We should add "type": "commonjs" to the package.json.

@userquin userquin changed the title fix(tailwind): default package exports fix(tailwindcss): default package exports Jan 25, 2025
@userquin

userquin commented Jan 27, 2025

Copy link
Copy Markdown
Author

Looks like @tailwindcss/node package also broken, this is the index.d.ts content (index.d.mts with same imports)

Downloaded tgz file from npm registry: https://registry.npmjs.org/@tailwindcss/node/-/node-4.0.0.tgz

image

@userquin userquin marked this pull request as draft January 27, 2025 15:00
@userquin userquin changed the title fix(tailwindcss): default package exports fix: default package exports Jan 27, 2025
@philipp-spiess

Copy link
Copy Markdown
Contributor

@userquin Hey! I wonder if you're still working at this since the PR is open.

@thecrypticace and me have been thinking about this problem as well and our most recent thinking is that we may need to hand-write all exported types. :/

@userquin

userquin commented Mar 5, 2025

Copy link
Copy Markdown
Author

@philipp-spiess I'm not working on the PR since the problem is about changing all the packages, you can remove the cjs types fix once next tsup version released (my PR merged egoist/tsup#1310 but not yet released)

I suggest you to add/use arethetypeswrong cli (attw) here; for example, check this PR at VueUse vueuse/vueuse#4648

@philipp-spiess

Copy link
Copy Markdown
Contributor

Thanks @userquin! This is good insight. If you're not actively working on this I suggest we close this PR for now but we still have the bug report open (#15844) and plan to get back to that very soon. Really appreciate your hard work here, this is going to be super helpful!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants