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(compiler): support async globalScripts functions #5158

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/bundle/app-data-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ const appendGlobalScripts = (globalScripts: GlobalScript[], s: MagicString) => {
});

s.append(`export const globalScripts = () => {\n`);
s.append(` return Promise.all([\n`);
globalScripts.forEach((globalScript) => {
s.append(` ${globalScript.defaultName}();\n`);
s.append(` ${globalScript.defaultName}(),\n`);
});
s.append(` ]);\n`);
s.append(`};\n`);
} else {
s.append(`export const globalScripts = () => {};\n`);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/output-targets/dist-lazy/lazy-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ const getLazyEntry = (isBrowser: boolean): string => {
if (isBrowser) {
s.append(`import { patchBrowser } from '${STENCIL_INTERNAL_CLIENT_PATCH_BROWSER_ID}';\n`);
s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`);
s.append(`patchBrowser().then(options => {\n`);
s.append(` globalScripts();\n`);
s.append(`patchBrowser().then(async (options) => {\n`);
s.append(` await globalScripts();\n`);
s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`);
s.append(`});\n`);
} else {
s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`);
s.append(`export const defineCustomElements = (win, options) => {\n`);
s.append(`export const defineCustomElements = async (win, options) => {\n`);
s.append(` if (typeof window === 'undefined') return undefined;\n`);
s.append(` globalScripts();\n`);
s.append(` await globalScripts();\n`);
s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`);
s.append(`};\n`);
}
Expand Down
3 changes: 2 additions & 1 deletion test/karma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"scripts": {
"clean": "rm -rf test-output tmp-compiled-tests",
"start": "node ../../bin/stencil build --dev --watch --serve --es5",
"build.all": "npm run clean && npm run build.sibling && npm run build.invisible-prehydration.false && npm run build.app && npm run build.custom-elements && npm run karma.webpack && npm run build.prerender",
"build.all": "npm run clean && npm run build.sibling && npm run build.globalScripts && npm run build.invisible-prehydration.false && npm run build.app && npm run build.custom-elements && npm run karma.webpack && npm run build.prerender",
"build.app": "npm run compile.test-app && node ../../bin/stencil build --debug --es5",
"compile.test-app": "node ../../node_modules/typescript/lib/tsc -p tsconfig.json",
"build.custom-elements": "webpack-cli --config test-app/custom-elements-output-webpack/webpack.config.js && webpack-cli --config test-app/custom-elements-output-tag-class-different/webpack.config.js && webpack-cli --config test-app/custom-elements-delegates-focus/webpack.config.js",
"build.invisible-prehydration.false": "node ../../bin/stencil build --config test-invisible-prehydration/stencil.invisiblePrehydrationFalse.config.ts --debug",
"build.prerender": "node ../../bin/stencil build --config test-prerender/stencil.config-prerender.ts --prerender --debug && node test-prerender/no-script-build.js",
"build.sibling": "node ../../bin/stencil build --config test-sibling/stencil.config.ts --debug",
"build.globalScripts": "node ../../bin/stencil build --config test-global-script/stencil.config.ts --debug",
"karma": "karma start karma.config.js",
"karma.prod": "npm run build.all && npm run karma",
"karma.webpack": "webpack-cli --config test-app/esm-webpack/webpack.config.js",
Expand Down
6 changes: 6 additions & 0 deletions test/karma/test-app/global-script/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<meta charset="utf8">
<script src="./build/testglobalscript.esm.js" type="module"></script>
<script src="./build/testglobalscript.js" nomodule></script>

<test-cmp></test-cmp>
20 changes: 20 additions & 0 deletions test/karma/test-app/global-script/karma.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { setupDomTests } from '../util';

describe('global script', () => {
const env = setupDomTests(document);

afterEach(() => {
env?.tearDownDom();
});

it('supports async execution', async () => {
await env?.setupDom('/global-script/index.html');

const cmp = document.querySelector('test-cmp');
expect(cmp).not.toBeNull();
expect(typeof cmp?.textContent).toBe('string');

const renderedDelay = parseInt(cmp?.textContent?.slice('I am rendered after '.length));
expect(renderedDelay).toBeGreaterThanOrEqual(1000);
});
});
10 changes: 10 additions & 0 deletions test/karma/test-global-script/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/karma/test-global-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "global-script",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"collection": "dist/collection/collection-manifest.json",
"types": "dist/types/components.d.ts",
"volta": {
"extends": "../package.json"
}
}
37 changes: 37 additions & 0 deletions test/karma/test-global-script/src/components.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable */
/* tslint:disable */
/**
* This is an autogenerated file created by the Stencil compiler.
* It contains typing information for all components that exist in this project.
*/
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
export namespace Components {
interface TestCmp {
}
}
declare global {
interface HTMLTestCmpElement extends Components.TestCmp, HTMLStencilElement {
}
var HTMLTestCmpElement: {
prototype: HTMLTestCmpElement;
new (): HTMLTestCmpElement;
};
interface HTMLElementTagNameMap {
"test-cmp": HTMLTestCmpElement;
}
}
declare namespace LocalJSX {
interface TestCmp {
}
interface IntrinsicElements {
"test-cmp": TestCmp;
}
}
export { LocalJSX as JSX };
declare module "@stencil/core" {
export namespace JSX {
interface IntrinsicElements {
"test-cmp": LocalJSX.TestCmp & JSXBase.HTMLAttributes<HTMLTestCmpElement>;
}
}
}
10 changes: 10 additions & 0 deletions test/karma/test-global-script/src/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare global {
interface Window {
__testStart: number;
}
}

export default async function () {
window.__testStart = Date.now();
return new Promise((resolve) => setTimeout(() => resolve('done!'), 1000));
}
11 changes: 11 additions & 0 deletions test/karma/test-global-script/src/test-cmp/test-cmp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component, h } from '@stencil/core';

@Component({
tag: 'test-cmp',
scoped: true,
})
export class SiblingRoot {
render() {
return <div>I am rendered after {Date.now() - window.__testStart}</div>;
}
}
14 changes: 14 additions & 0 deletions test/karma/test-global-script/stencil.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Config } from '../../../dist/declarations';
const { WWW_OUT_DIR } = require('../constants');

export const config: Config = {
namespace: 'TestGlobalScript',
tsconfig: 'tsconfig.json',
outputTargets: [
{
type: 'www',
dir: `../${WWW_OUT_DIR}`,
},
],
globalScript: 'src/global.ts',
};
35 changes: 35 additions & 0 deletions test/karma/test-global-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"compilerOptions": {
"alwaysStrict": true,
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": true,
"declaration": false,
"resolveJsonModule": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"jsxFactory": "h",
"lib": [
"dom",
"es2017"
],
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": false,
"noImplicitReturns": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"pretty": true,
"target": "es2017",
"useUnknownInCatchVariables": true,
"baseUrl": ".",
"paths": {
"@stencil/core": ["../../../internal"],
"@stencil/core/internal": ["../../../internal"],
"@stencil/core/testing": ["../../../testing"]
}
},
"include": [
"src"
]
}
3 changes: 2 additions & 1 deletion test/karma/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
],
"exclude": [
"test-prerender",
"test-sibling"
"test-sibling",
"test-global-script"
]
}
Loading