-
Notifications
You must be signed in to change notification settings - Fork 665
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
[WIP] Enable web extension for Foam #1290
Conversation
Update: The extension is running quite well in the online environment at first sight. To be sure - and for regression, I'd like to run the integration tests for web as well. This is where the pain begins. Last time I got stuck in a long loop to get Mocha working, so this time I went for There seems to be some ways to get commonjs working in vscode, but I am wondering if we want all these changes just to get testing for web working. For now, I feel |
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.
Thanks for giving it a second stab! I am happy to see that the code is basically ready for the transition, except for the readFileSync
which I still think needs a solution (have you checked if the toString
works?)
Unfortunately I am not super versed in mocha nor vitest, so can't help much on that front. The commonjs requirement is a bit of a limitation, and I agree that is probably better to play within that unless a solid solution can be found otherwise.
@@ -194,7 +193,7 @@ function contentExtractor( | |||
parser: ResourceParser, | |||
workspace: FoamWorkspace | |||
): string { | |||
let noteText = readFileSync(note.uri.toFsPath()).toString(); | |||
let noteText = vsWorkspace.fs.readFile(toVsCodeUri(note.uri)).toString(); |
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.
I think vsWorkspace.fs.readFile
returns a promise, this was to me the only open question as to how to transition in the code
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.
Hi, is this still open? If yes can we get around by writing a new markdown-it
plugin to handle the async operation?
Their documentation has some guidelines. Though I don't follow the wording aptly but I can give it a quick stab by forking markdownItRegex
. WDYT?
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.
This is still open, you can find all the info and continue the conversation here: #1295
I've been working on getting the tests to work with Mocha. Although it seemed I was making progress I ran into a big problem. Our assertion library is jest's My suggestion is to enable the brwoser extension and only add a test suite that checks if Foam can be activated in the web browser. A pattern that I have seen at more VSCode extensions. This way we at least confirm the webpack bundle is okay and works. Specific checks for working functionality in the browser would not exist. Only perhaps if it is a recurring event in which we would write a specific regression test. @riccardoferretti what do you think? |
Thanks @pderaaij for the investigation.
Mmmm.. can you share a couple of VS Code extensions that use that approach?
I am not against migrating over time to another library that would be more web friendly. Basically all new and edited tests would use the new library until we reach a point of switch over. |
Annoyingly enough I can't find these examples anymore in my history and a quick GitHub search leaves me blank as well. I have researched so many repo's I might lost track how often I have seen this pattern. I do agree it feels quite brittle. We could first rewire the testing framework. Or we just publish the web extension and rewrite the tests based on incoming reports. But that's not the greatest experience. In any case, it will require some work. |
Ok, this is something to think about, I am still debating if it's "good enough" (it might be). I am open to your suggestion about using the legacy framework (jest) for the regular testing, and only have some smoke tests for the web extension. |
Ahoy there! I come with few skills but willing to contribute help to try to merge this if I can provide any. I also know several other people who are interested in getting Foam to work on VS Code web :) What's left to be done? Could the extension be merged as-is without interfering with code quality/stability for regular (non-browser-based) VS Code users, maybe with a warning/alpha disclaimer? |
@riccardoferretti this would be enough to make the web version work. However, note-embedding will not work correctly. I might be able to get it working via: microsoft/vscode#174080 (comment). Additionally, I have not been able to get a simple end-2-end test working. My idea was to simply test if the extension was loaded. But I can't get Nevertheless, we can publish the extension for web with these comments and start to collect the feedback. |
It took some time, but I got an end-2-end test working. It is a simple check that asserts if the foam extension is loaded. @riccardoferretti I've added an extra CI step to build and verify the web version is working. If you see this differently, let me know. My goal would be to get this PR merged. The next step is to attempt to change the markdown preview. |
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.
Thanks @pderaaij I have left a few comments, let me know what you think
mainFields: ['browser', 'module', 'main'], | ||
extensions: ['.ts', '.js'], // support ts-files and js-files | ||
alias: { | ||
// Overwrite wikilink embedding as it will not work in the current setup on web |
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.
2 comments:
- I wonder if we can handle it from within
wikilink-embed.ts
so that it's more explicit (I don't love webpack nor its black magic ;) ) - if approach n.1 is not good, I would actually have
/src/web/features/preview/wikilink-embed.ts
->src/features/preview/wikilink-embed-for-web-extension.ts
(and add a comment on both files to explain what's happening)
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.
First won't be possible. As the file contains an import of fs
it will try to pack that. By overwriting the file on compile time, I am preventing these issues.
@@ -7,7 +7,8 @@ | |||
"lib": ["ES2019", "es2020.string", "DOM"], | |||
"sourceMap": true, | |||
"strict": false, | |||
"downlevelIteration": true | |||
"downlevelIteration": true, | |||
"skipLibCheck": true |
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.
what's the goal of this setting?
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.
Preventing tsc
to check the types within the project. I've added this to prevent the collisions between mocha
and jest
. We use mocha
for the e2e tests and jest
for the vscode extension. They are incompatible with each other, but for our use case that's not a problem. However, tsc
scans the entire node_modules
folder and detects the collisions.
workspace: FoamWorkspace, | ||
parser: ResourceParser | ||
) => { | ||
return md; |
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.
I would actually return something along the lines of:
return `<div class="foam-no-embeds-warning">Embeds are not supported in web extension: ${wikilink}</div>`;
foam-no-embeds-warning
could be along the lines of foam-cyclic-link-warning
This is gonna save us from a bunch of bug reports about the missing feature ;)
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.
vscode.window.showInformationMessage('Start all tests.'); | ||
|
||
test('Foam extension is loaded', () => { | ||
assert.ok(vscode.extensions.getExtension('foam.foam-vscode')); |
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.
I like the idea of doing just a quick smoke check here, making sure that:
- the extension is present
- the API is exposed
- a few commands are available
I think that should be enough to get started
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.
I am stuck on expanding the tests. See microsoft/vscode-test-web#130.
Once this is solved, I'll update the PR.
@@ -1,6 +1,5 @@ | |||
import { isEmpty } from 'lodash'; | |||
import { asAbsoluteUri, URI } from '../core/model/uri'; | |||
import { TextEncoder } from 'util'; |
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.
doesn't remove these import affect the native extension?
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.
Not required anymore, as it is default part from node > 16.
Hi @pderaaij I was trying to test this locally, but I am getting the following error in the browser Sequence of steps:
Also, the Can you confirm the issues? |
Yes, the Buffer one I found out as well. It is locally in my stage, but yet to commit. Will do so. It is a webpack config issue by the way. Easily resolved. |
Got the tests working: Functionality should be working now. Let me know what you think @riccardoferretti |
First of all, amazing work @pderaaij! This is not a simple change! There is one thing that I am really hesitating on, and that's mocha. I normally wouldn't like to have 2 "competing" libraries, in this specific case the fact that it's for testing and the extra magic on the TS front makes me even more wary. So, I am looking at the code and am trying to understand where jest falls short. Thanks! |
As far as I understand the problem is that Jest is an unit testing library. It is designed to run within the nodejs environment, but not the browser. The test suite runs in the browser. The vscode-web-test extension starts a local VS code web environment in which Foam is loaded. After activation the test suite runs in the browser. For this we need a browser library. `` mocha`. Is the suggested library by the vscode docs. I know it isn't ideal, but it is the pattern I saw in many repositories. |
I see what you mean. |
@riccardoferretti Quite sure it was working, but can't understand how it could be working. I worked on a concept to make the graph working again. Could you verify if this works for your? run |
Thanks @pderaaij - I think things are really coming together and I was dealing with the odd sensation that there is something that rubs me the wrong way. After much thinking I think I have come to the source of the issue: I feel too many bits are impacted, and between webpack and mocha, and the extra TS settings, I am very hesitant. More info on the PR itself, please let me know what you think, and thanks again for making this possible! |
See #1290 for context. Major thanks to @pderaaij that did all the hard work here. * using js-sha1 instead of node's crypto to compute sha1 * Using esbuild to bundle native and web extension (WIP) * Added message regarding unsupported embeds in web extension * support for graph webview in web extension
I am making a fresh attempt on getting Foam to work on the browser. I'll close #1127 later on, but am keeping it for reference. So far, so good. But long way to go.