Skip to content

If tsconfig.json is set to CommonJS mode, the declaration files cannot be read. #1303

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

Closed
3 tasks done
yuheiy opened this issue Feb 26, 2024 · 5 comments
Closed
3 tasks done

Comments

@yuheiy
Copy link

yuheiy commented Feb 26, 2024

Reproduction link

No response

Bug description

If tsconfig.json is set to CommonJS mode, the declaration files for almost modules cannot be read.

tsconfig.json:

{
  "compilerOptions": {
    "module": "CommonJS",
    "moduleResolution": "Node10",
    "strict": true
  }
}

test.ts:

スクリーンショット 2024-02-26 22 52 13

The cause is probably that the types is not set in package.json.

	"main": "dist/index.cjs",
	"module": "dist/index.mjs",
+	"types": "dist/index.d.ts",
	"exports": {
		".": {
			"import": {
				"types": "./dist/index.d.ts",
				"default": "./dist/index.mjs"
			},
			"require": {
				"default": "./dist/index.cjs"
			}
		}
	},

Actual Behavior

No response

Expected Behavior

No response

Can you reproduce it with npx @csstools/csstools-cli <plugin-name> minimal-example.css?

None

npx Output

No response

Extra config

No response

What plugin are you experiencing this issue on?

No response

Plugin version

What OS are you experiencing this on?

No response

Node Version

20

Validations

  • Follow our Code of Conduct
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Would you like to open a PR for this bug?

  • I'm willing to open a PR
@romainmenke
Copy link
Member

Hi @yuheiy,

This is not a bug, this is by design.
We do not provide types for commonjs, only for es modules.

The types we do have for es modules are only valid for the es modules source, not for the commonjs source.

See #1031 (comment) for more context.

@yuheiy
Copy link
Author

yuheiy commented Feb 26, 2024

Oh, thanks for your reply.

I found the following comment in that issue:

If however such a tool does exist, please let us know.
We are happy to change our setup to make this possible.

So, do you know about unbuild and tshy? What do you think about these?

@romainmenke
Copy link
Member

romainmenke commented Feb 26, 2024

Thank you for checking out some tools @yuheiy,

At first glance unbuild doesn't seem to solve the hard problems and only works for projects that happen to have compatible JavaScript that can be easily transpiled between commonjs and es modules (unjs/unbuild#374)

I tried tshy and it didn't work at all, I am sure it can work. It just doesn't work at all for me, for what I want it to do.
It also fails to solve the hard problems : https://github.com/isaacs/tshy?tab=readme-ov-file#handling-default-exports


At this time I've kinda embraced this issue. Typings are a secondary feature and es modules is the future. So from our perspective it makes more sense to focus on providing the best dev experience for es modules while also having a correct runtime for commonjs.

@yuheiy
Copy link
Author

yuheiy commented Feb 26, 2024

Thank you for your explanation. Now that I understand what the problem is, I will close the issue.

@yuheiy yuheiy closed this as completed Feb 26, 2024
@yuheiy
Copy link
Author

yuheiy commented Feb 27, 2024

Just so you know, I initially aimed to create a CommonJS (only) package using your module with tsc, specifically a PostCSS plugin. Given the issue, I've now decided to go with a bundler instead.

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

No branches or pull requests

2 participants