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

fix: Solve issue where worker thread didn't have access to fetch #3012

Merged
merged 4 commits into from
Aug 20, 2021

Conversation

splitinfinities
Copy link
Contributor

@splitinfinities splitinfinities commented Aug 20, 2021

Closes #3011

The worker thread's in-memory system did not have the correct reference to fetch.

How to test:

  1. Check this branch out
  2. Build the compiler with npm run build
  3. Visit the /test/browser-compile directory.
  4. Run npm i && npm start
  5. This app will display an app that runs the compiler in the browser. It lets you run the compiler with different settings.

Originally, before this, the transpile results pane displayed empty and there were multiple errors in dev tools.

@splitinfinities splitinfinities requested a review from a team August 20, 2021 16:31
@@ -505,7 +505,7 @@ export const createSystem = (c?: { logger?: Logger }) => {
return results;
};

const fetch = global.fetch;
const fetch = self?.fetch ?? global?.fetch ?? window?.fetch;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Truthfully, this can be executed in multiple spaces. So what I'm trying to do here is worker, then node, then window. I'm open to swapping the order here too.

Copy link
Contributor

Choose a reason for hiding this comment

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

I couple questions/maybe asks here -

  • How do self and window differ here?
  • Can we add a comment here to explain how/why we chose this order?
  • In what circumstances do we use the fetch on self, global, & window?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

self is the global namespace object used within a web worker.

window is the browser's global namespace object (I reorganized this to check the reference on that second)

global is Node's global namespace object. https://nodejs.org/api/globals.html#globals_global

I did switch these to do worker => browser => node

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I think that's what I'm looking for in the the ask for a comment. ATM, the comment is kind of repeating the code:

  // Reference fetch from a worker thread, then a browser thread, then a node thread.
  const fetch =
    typeof self !== 'undefined'
      ? self?.fetch
      : typeof window !== 'undefined'
      ? window?.fetch
      : typeof global !== 'undefined'
      ? global?.fetch
      : undefined;

which doesn't do "future Will and Ryan" much good. Ideally we explain why we choose this order (why is window over global, and self over both of them?) and potentially the context they're used in (what you've got in the previous comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

@@ -57,7 +57,6 @@ export class AppRoot {
}

async compile() {
console.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When running this app, the console gets cleared after the worker loads, so the traspile problems weren't immediately obvious when I opened dev tools.

PS: We should figure out how to automate this with Karma.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see now. It was clearing debugging statements in this component itself

And yes! Can you please put an item on the refinement sheet to discuss next week?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -70,7 +70,7 @@ export const cssTemplatePlugin = {
async load(id: string) {
let code = styleImports.get(id.split('?')[0]);
if (code) {
const results = await stencil.compile(code, {
const results = await stencil.transpile(code, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

compile doesn't exist on the Stencil compiler API.

Copy link
Contributor

Choose a reason for hiding this comment

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

Was this being masked by #3012 (comment) clearing the console?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This failed when running the start command in this directory, because compile didn't exist. It was a TypeScript error that was accurate, I'm assuming because it's been a while since this browser-compile project was started up.

The comment you reference actually covered up errors in the worker, which if it wasn't here, would have immediately told me that how we get the fetch reference for the in-memory system was erroring.

@rwaskiewicz rwaskiewicz merged commit 925d4e9 into master Aug 20, 2021
@rwaskiewicz rwaskiewicz deleted the investigate-compiler-api branch August 20, 2021 20:53
rwaskiewicz pushed a commit that referenced this pull request Aug 27, 2021
…fetch (#3012)

add a more flexible way of assigning `fetch` based on the environment

STENCIL-84: Investigate Non-Transpiled Stencil Code in Web API
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.

Stencil web API not transpiling correctly in 2.7.0
2 participants