Skip to content

Commit

Permalink
fix(compiler): solve issue where worker thread didn't have access to …
Browse files Browse the repository at this point in the history
…fetch (#3012)

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

STENCIL-84: Investigate Non-Transpiled Stencil Code in Web API
  • Loading branch information
splitinfinities committed Aug 20, 2021
1 parent 2dd9fba commit 925d4e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/compiler/sys/stencil-sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,22 @@ export const createSystem = (c?: { logger?: Logger }) => {
return results;
};

const fetch = global.fetch;
/**
* `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
*
* loading in this order should allow workers, which are most common, then browser,
* then Node to grab the reference to fetch correctly.
*/
const fetch =
typeof self !== 'undefined'
? self?.fetch
: typeof window !== 'undefined'
? window?.fetch
: typeof global !== 'undefined'
? global?.fetch
: undefined;

const writeFile = async (p: string, data: string) => writeFileSync(p, data);

Expand Down
1 change: 0 additions & 1 deletion test/browser-compile/src/components/app-root/app-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class AppRoot {
}

async compile() {
console.clear();
console.log(`compile: stencil v${stencil.version}, typescript v${stencil.versions.typescript}`);

const opts: StencilTypes.TranspileOptions = {
Expand Down
2 changes: 1 addition & 1 deletion test/browser-compile/src/utils/css-template-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
file: id,
});
return results.code;
Expand Down

0 comments on commit 925d4e9

Please sign in to comment.