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

Refactor i18n #1405

Merged
merged 17 commits into from
Oct 7, 2021
Merged

Refactor i18n #1405

merged 17 commits into from
Oct 7, 2021

Conversation

hgiesel
Copy link
Contributor

@hgiesel hgiesel commented Oct 3, 2021

Moving to ts_project has broken types for me on both VS Code and Neovim. This is an error message:

[ts 2607] [E] Element does not support attributes because type definitions
⇒ are missing for this Svelte Component or element cannot be used as such.

Underlying error:
JSX element class does not support attributes because it does not have a
⇒ '$prop_def' property.
─────────────────────────────────
[ts 2786] [E] Type definitions are missing for this Svelte Component. It
⇒ needs a class definition with at least the property '$prop_def' which
⇒ should contain a map of input property definitions.
Example:
class ComponentName { $prop_def: { propertyName: string; } }
If you are using Svelte 3.31+, use SvelteComponentTyped:
import type { SvelteComponentTyped } from "svelte";
class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}

Underlying error:
'ButtonGroup' cannot be used as a JSX component.
Its instance type 'ButtonGroup__SvelteComponent_' is not a valid JSX
⇒ element.
Property '$prop_def' is missing in type 'ButtonGroup__SvelteComponent_'
⇒ but required in type 'ElementClass'.

This PR fixes types for me. Obviously we should find a solution that fixes it for everybody :)

To fix the typings for svelte files, I had to move the rootDirs parameter inside the sub projects.
To fix the typings for tr and backend_proto I had to reinstate the symlinks you originally had added.

I also used the opportunity to rewrite i18n quite a bit. The old i18n_helpers is now the main i18n file. As the generated files are more difficult to handle, I thought it would make more sense, if the other subprojects depended on the one actual file, not on generated files. I also removed the singleton class and replaced it with ordinary functions.

- typings do not work for any ts or svelte files
- if we set the 'rootDirs' in ts/tsconfig.json to '../bazel-bin/ts' and then inherit
  them from e.g. editor, the root will be changed to '../../bazel-bin/ts',
  however editor needs look in '../../bazel-bin/ts/editor' instead.
- This way, we can restrict the awkwardness of importing files outside
  the ts directory within lib
@dae
Copy link
Member

dae commented Oct 3, 2021

Strange, the typings are working fine for me. I have the latest "Svelte for VS Code" installed ( v105.4.1 ) and start it by opening the root Anki folder after building with 'bazel build //...' to ensure all the Svelte files have been generated. Might be worth trying a 'bazel clean' to clear out old files that are in the wrong location, and making sure you're opening the base folder rather than just a single subfolder? I don't mind the rootDir change if it doesn't break anything, but would like to understand what's wrong first.

The reason for the current i18n layout is because rexporting symbols breaks tree shaking:

dae@dip:Work/code/dtop% bazel build -c opt ts/graphs && du -hs bazel-bin/ts/graphs/graphs.js
304K	bazel-bin/ts/graphs/graphs.js
dae@dip:Work/code/dtop% gh pr checkout 1405
dae@dip:Work/code/dtop% bazel build -c opt ts/graphs && du -hs bazel-bin/ts/graphs/graphs.js
408K	bazel-bin/ts/graphs/graphs.js

I agree the current layout is a bit awkward, and no objections from me on a change if you can preserve the tree shaking aspect.

This partially reverts commit e1d4292.

It seems like this might not be necessary after all.
However some other change made on this branch seems to have fixed
the .svelte.d.ts imports
There was a circular import i18n.ts <-> i18n-translate.ts
* This restores tree shaking
* es2018-2020 have wide support on all modern browsers including
@hgiesel hgiesel marked this pull request as draft October 4, 2021 01:15
@hgiesel hgiesel marked this pull request as ready for review October 4, 2021 01:32
@hgiesel hgiesel force-pushed the fix-svelte-imports branch from a0ab15f to 72256f0 Compare October 4, 2021 01:34
@hgiesel
Copy link
Contributor Author

hgiesel commented Oct 4, 2021

Here the author of esbuild himself says it:

Tree shaking is supported with import * as x but not when x is then re-exported and re-imported.

I've done several bazel clean and git clean but to no avail, I can't get typing for tr. In return tree-shaking works again. I still refactored i18n quite a lot. tr is now outside of the i18n directory.

I also changed tsconfig's lib and module to es2020.
es2018, es2019, and es2020 all seem to have enough support.

Copy link
Member

@dae dae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor looks good; some comments below.

I'm still puzzled by this symlink requirement though. Will test on another machine and get back to you.

ts/tsconfig.json Outdated
"dom.iterable"
],
"module": "es2020",
"lib": ["es2020", "dom", "dom.iterable"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this safe? The current minimum Qt target is 5.14, which is Chromium 77 - which does not support things like the nullish coalescing operator. Neither does iOS 12 / early iOS 13.

Does this change enable something important that we were missing, or was it just about making things neater?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case no, I'll revert it. I only needed to add support for .fromEntries.
Is there some reference list of platforms Anki aims to support. I basically went through the lists on icanuse and looked out for:

  • support for Chromium 83
  • support for iOS 14/15
    in which case we could go with es2020.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it's Chromium 77/iOS 12

  • iOS 12 support will likely be dropped soon, but I try to support a few versions back, so I'd like to retain 13 support unless there's a compelling reason for dropping it early
  • The current Windows and Mac builds use Qt 5.14 due to bugs in 5.15 (5.15 is only used when running from Bazel). Ideally we would drop 5.x support once the 6.2 branch is polished, but in past Qt updates, newer versions have broken things for some users. We'll know more once 6.2 has been polished a bit and has a packaged build available, but I wouldn't be surprised if we end up needing to bring back alternate versions based on 5.x for users that have trouble with the 6.2 build.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it might be worth setting up eslint to warn us when we use something outside those versions - feel free to look into that if you're interested :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#982 tracks the 5.15 issues

@dae
Copy link
Member

dae commented Oct 6, 2021

Ok:

  • installed VS code on other machine

  • installed svelte add-on

  • checked out latest main branch from this repo

  • opened it in VS Code

  • went to CongratsPage.svelte. Was prompted to install TS tools; did so and restarted. Was told extension modified, restarted VS code again.

  • I can see the "../lib/i18n" line underlined in red because the sources haven't been built yet.

  • I go to lib/proto.ts and see ./backend_proto is also underlined red.

  • I go to editor/index.ts and see that the i18n and components imports are highlighted red

  • I go to a terminal and run bazel build ... in the checkout folder

  • I restart vs code

  • I see that the lib/i18n and backend_proto references are now working, but I'm getting errors in DeckOptionsPage about dynamic svelte component.

  • I dug around to find out why my other machine wasn't showing the DeckOptionsPage errors, and it turns out I had a ~/node_modules folder that was affecting things - when I renamed that and restarted VS Code, my main machine now shows the same error with SveltedTypedComponent. Is it possible your i18n/proto issues are due to a node_modules folder?

Any ideas about the DynamicSvelteComponent issues in DeckOptionsPage?

@dae
Copy link
Member

dae commented Oct 6, 2021

I suspect this is caused by our node_modules being in ts/ instead of the root folder. With a symlink all files stay in ts, but when we reference ../../bazel-bin it presumably hunts further up the tree, and results in it not being able to find libraries like 'svelte', breaking the definitions. Will look into moving the location tomorrow and see if it helps

@hgiesel
Copy link
Contributor Author

hgiesel commented Oct 6, 2021

I think you might be right. According to this StackOverflow answer:

When you set multiple rootDirs, tsc will find the parent directory that is common to all of them and treat that as the rootDir. This is why you're getting the outDir structure that you're getting.

So this means that our inferred rootDir is the repo directory, not ts/

I added them to fix to have completion for tr, however this would have
also have meant to abandon the tree shaking.
As we want to have tree shaking, it's also not necessary to have the
symlinks anymore
@hgiesel hgiesel changed the title Fix svelte imports Fix svelte imports + Refactor i18n Oct 6, 2021
ts/tsconfig.json Outdated
@@ -33,7 +34,6 @@
// "rootDir": "..",
"rootDir": ".",
"rootDirs": [
".",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, everything in this PR everything except removing this line is just refactoring i18n into a directory.
Removing this line is what fixes autocompletion for Svelte components for me.

Copy link
Member

@dae dae Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that didn't work for me - I tried the latest code in this PR, and the generated i18n files can't be found by vscode on my machine.

Moving node_modules to the root folder and setting rootDir to .. seems to solve things - could you give #1408 a try? If that works, we can just drop the rootDirs change from this PR and it should be good to merge in.

dae added a commit that referenced this pull request Oct 7, 2021
Recommend removing ts/node_modules folder before attempting to
build after this update.

This moves ts/node_modules into the root of the project to work around
#1405 (comment)
 
Also fixes the sass errors shown when running scripts/svelte-check
dae added a commit that referenced this pull request Oct 7, 2021
Recommend removing ts/node_modules folder before attempting to
build after this update.

This moves ts/node_modules into the root of the project to work around
#1405 (comment)

Also fixes the sass errors shown when running scripts/svelte-check
@hgiesel hgiesel changed the title Fix svelte imports + Refactor i18n ~Fix svelte imports~ + Refactor i18n Oct 7, 2021
@hgiesel hgiesel changed the title ~Fix svelte imports~ + Refactor i18n Fix svelte imports + Refactor i18n Oct 7, 2021
@dae
Copy link
Member

dae commented Oct 7, 2021

Want to squash and rebase this over main so we get a nice clean commit? :-)

@hgiesel
Copy link
Contributor Author

hgiesel commented Oct 7, 2021

Sounds good :)

@dae dae changed the title Fix svelte imports + Refactor i18n Refactor i18n Oct 7, 2021
@dae dae merged commit dec0fbe into ankitects:main Oct 7, 2021
@dae
Copy link
Member

dae commented Oct 7, 2021

Sorry, that wasn't clear. I'd meant to suggest you do that (so we can test it over main the main branch before merging in - if I squashed it with the git interface it would presumably show up as authored by me instead). But I didn't want to hold this up and we've tested it already, so I just used GitHub's 'squash and merge' this time

@hgiesel
Copy link
Contributor Author

hgiesel commented Oct 7, 2021

I did get you, I was just about to push the squashed commit :) I'll also try to be more clear next time.

@dae
Copy link
Member

dae commented Oct 7, 2021

All good :-)

RumovZ added a commit to RumovZ/anki that referenced this pull request Oct 12, 2021
dae pushed a commit that referenced this pull request Oct 14, 2021
* Only collect card stats on the backend ...

... instead of rendering an HTML string using askama.

* Add ts page Card Info

* Update test for new `col.card_stats()`

* Remove obsolete CardStats code

* Use new ts page in `CardInfoDialog`

* Align start and end instead of left and right

Curiously, `text-align: start` does not work for `th` tags if assigned
via classes.

* Adopt ts refactorings after rebase

#1405 and #1409

* Clean up `ts/card-info/BUILD.bazel`

* Port card info logic from Rust to TS

* Move repeated field to the top

#1414 (comment)

* Convert pseudo classes to interfaces

* CardInfoPage -> CardInfo

* Make revlog in card info optional

* Add legacy support for old card stats

* Check for undefined instead of falsy

* Make Revlog separate component

* drop askama dependency (dae)

* Fix nightmode for legacy card stats
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