-
Notifications
You must be signed in to change notification settings - Fork 556
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
chore(typescript): migrating to typescript #1248
Conversation
chore: formatting-only changes for JS/MD files and pkg json
chore: bump husky, change precommit hook, eslint scripts
Version Packages (prerelease)
V3 fixes port
Co-authored-by: Ioannis Chrysostomakis <ic768@protonmail.com>
Forward port of bugfix and grammar fix in v3
Version Packages (prerelease)
feat: add preprocessors
chore: remove es6 helpers and use actual ES6
Fix tests & slight API changes
Version Packages (prerelease)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* fix: support optional props border tokens * fix: handle timingFunction and fontFamily props in transforms
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* feat: allow not throwing on broken refs * chore: add integration test broken refs + correct refs * fix: revert throwOnBrokenReferences, respect silent verbosity
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* feat: recursively expand nested composite tokens * fix: expand object type check, maintain ref if possible * fix: handle multi-value object tokens like shadows --------- Co-authored-by: Abel van Beek <abel.van.beek@tromsfylke.no> Co-authored-by: jorenbroekema <joren.broekema@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@@ -4,7 +4,7 @@ import { dirname } from 'path-unified'; | |||
import { fs } from 'style-dictionary/fs'; | |||
import { chaiWtrSnapshot } from '../snapshot-plugin/chai-wtr-snapshot.js'; | |||
import { fixDate } from './__helpers.js'; | |||
import { writeZIP } from '../lib/utils/convertToDTCG.js'; | |||
import { writeZIP } from '../dist/esm/utils/convertToDTCG.mjs'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually I would like to move the tests to TS as well and import source files rather than dist files, but we can save that for a separate PR
@@ -13,7 +13,7 @@ | |||
import { expect } from 'chai'; | |||
import StyleDictionary from 'style-dictionary'; | |||
import { fs } from 'style-dictionary/fs'; | |||
import { resolve } from '../lib/resolve.js'; | |||
import { resolve } from '../dist/esm/resolve.mjs'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the test scripts to run a build first, so these files should exist
package.json
Outdated
}, | ||
"./utils": "./lib/utils/index.js", | ||
"./types": "./types/index.d.ts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types are now just exported out of index
const ctor = /** @type {typeof Register} */ (this.constructor); | ||
return deepmerge(ctor.hooks, this._hooks ?? {}); | ||
get hooks(): Required<Hooks> { | ||
return deepmerge((this.constructor as any).hooks, this._hooks ?? {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this issue is a nuisance but I don't see how typecasting to any
is better than typecasting to typeof Register
which is actually the proper cast and what it should be implicitly typed as if that TS issue were solved.
export { default as minifyDictionary } from './minifyDictionary.js'; | ||
export { default as setSwiftFileProperties } from './setSwiftFileProperties.js'; | ||
export { default as setComposeObjectProperties } from './setComposeObjectProperties.js'; | ||
export { default as createPropertyFormatter } from './createPropertyFormatter'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future: export named functions and don't have to do export { default as _ }
This feels like improper usage or a bug or something, can't we fix this alone?
I would prefer to only publish ESM. CJS is a legacy format and has a lot of drawbacks:
There's more... but generally speaking CJS vs ESM is causing fragmentation in the JS ecosystem and people need to start pushing stuff towards ESM-only for it to get better, worst case scenario folks can always dynamically import ESM inside CJS files so it's not like we're alienating CJS users. With regards to moving to TS files: I think this PR demonstrates this nicely in the way the tests are ran now, we're currently running them from the dist folder which means in order to run your tests you have to recompile the TS files, which significantly worsens the TDD flow.. JSDocs annotations are the drawback, they're subjectively inferior DX while authoring, but Basically, every time in the last 2 years that I've switched over a project from JS files w JSDocs annotations for type safety to TS files, I've thoroughly regretted the move, the most recent one being the sd-transforms package. Here's some reading material on this topic that I find helpful when explaining this perspective: If the only issue is that TSC didn't properly check our .d.ts files for issues, I would much prefer to just fix that issue without going down this route. |
"main": "./dist/index.js", | ||
"module": "./dist/esm/index.mjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are legacy fields and should be avoided. Package entrypoints has widespread support across all modern JS tooling and locks down public from private APIs which helps with reliability and prevents issues where people will complain that you broke some private API that they were relying upon
I put a comment that this would actually not be how the tests should run, but I just didn't have time to port the test files to TS as well. If we port the test files to TS then we would not have to import dist files to test them, we would just import the source TS files.
I'm curious what troubles you have had, my experience in the past as been the exact opposite. For ESM v CJS, I totally agree CJS has many issues, but at the same time it is not that hard to support with something like Rollup. In other projects I don't even think about ESM v CJS v TS anymore because the build step takes care of that pretty easily, and the build step takes less than a second. One issue I have with the current setup is that our I'm open to not migrating to typescript completely, my main concern is to not treat our typescript customers as second-class consumers anymore (which to my fault they have been so far). The main concerns I have:
One example friction point, which could be solved with the current architecture maybe?, is actually navigating to the types in VSCode. The first clip is the current way with JSDoc + declarations: And this is with the PR: |
It's a long list and I don't remember everything from when I last ran into issues but here's a subset:
You'd be surprised about the amount of ESM v CJS interoperability issues that exist which bundlers do not take care of, at least not correctly or without configuring it to your need. Fortunately I haven't ran into them all too much for style-dictionary in particular (because the issues are somewhat niche), but when you do, it is an absolute nightmare to figure out why the CJS version is misbehaving while the ESM one works fine or vice versa. The amount of configuration here gives a rough idea: https://www.npmjs.com/package/@rollup/plugin-commonjs#options . And there isn't really a downside to forcing people to use ESM when you can still import ESM packages inside CJS modules just fine. Lastly, third party module tampering being an inherent security flaw of CJS modules really brings it over the line for me (although less of a concern in this package, since it's not crypto/security related).
Yeah that's generally the tactic, run build, npm link, then
Fully agree, the type issue you found with the local .d.ts files + skipLibCheck was a bit of a surprise to me too, but fixable (see my PR).
|
Issue #, if available:
Description of changes:
Initially found a bug with the
DesignTokens
type in types/DesignToken.d.ts when working on the docs:TypeScript declaration files are not really type-safe because the TypeScript compiler isn't really being run on them. So I went down a rabbit hole of just migrating everything to TypeScript so that we don't inadvertently ship TS issues.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.