Skip to content

Commit 5f0a992

Browse files
Support async globalScripts functions
fixes #3392 STENCIL-467
1 parent f5d4e98 commit 5f0a992

File tree

16 files changed

+170
-10
lines changed

16 files changed

+170
-10
lines changed

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Using `npm link` is beneficial to the development cycle in that consecutive buil
7878
"compilerOptions": {
7979
"baseUrl": ".",
8080
"paths": {
81-
"@stencil/core/internal": ["node_modules/@stencil/core/internal"],
82-
"@stencil/core/internal/*": ["node_modules/@stencil/core/internal/*"]
81+
"@stencil/core/internal": ["./node_modules/@stencil/core/internal"],
82+
"@stencil/core/internal/*": ["./node_modules/@stencil/core/internal/*"]
8383
}
8484
}
8585
}

src/compiler/bundle/app-data-plugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,11 @@ const appendGlobalScripts = (globalScripts: GlobalScript[], s: MagicString) => {
183183
});
184184

185185
s.append(`export const globalScripts = () => {\n`);
186+
s.append(` return Promise.all([`);
186187
globalScripts.forEach((globalScript) => {
187-
s.append(` ${globalScript.defaultName}();\n`);
188+
s.append(` ${globalScript.defaultName}(),\n`);
188189
});
190+
s.append(` ]);\n`);
189191
s.append(`};\n`);
190192
} else {
191193
s.append(`export const globalScripts = () => {};\n`);

src/compiler/output-targets/dist-lazy/lazy-output.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ const getLazyEntry = (isBrowser: boolean): string => {
180180
if (isBrowser) {
181181
s.append(`import { patchBrowser } from '${STENCIL_INTERNAL_CLIENT_PATCH_BROWSER_ID}';\n`);
182182
s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`);
183-
s.append(`patchBrowser().then(options => {\n`);
184-
s.append(` globalScripts();\n`);
183+
s.append(`patchBrowser().then(async (options) => {\n`);
184+
s.append(` await globalScripts();\n`);
185185
s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`);
186186
s.append(`});\n`);
187187
} else {
188188
s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`);
189-
s.append(`export const defineCustomElements = (win, options) => {\n`);
189+
s.append(`export const defineCustomElements = async (win, options) => {\n`);
190190
s.append(` if (typeof window === 'undefined') return undefined;\n`);
191-
s.append(` globalScripts();\n`);
191+
s.append(` await globalScripts();\n`);
192192
s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`);
193193
s.append(`};\n`);
194194
}

src/testing/puppeteer/puppeteer.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* hello world!
3+
*/

test/karma/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
"scripts": {
88
"clean": "rm -rf test-output tmp-compiled-tests",
99
"start": "node ../../bin/stencil build --dev --watch --serve --es5",
10-
"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",
10+
"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",
1111
"build.app": "npm run compile.test-app && node ../../bin/stencil build --debug --es5",
1212
"compile.test-app": "node ../../node_modules/typescript/lib/tsc -p tsconfig.json",
1313
"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",
1414
"build.invisible-prehydration.false": "node ../../bin/stencil build --config test-invisible-prehydration/stencil.invisiblePrehydrationFalse.config.ts --debug",
1515
"build.prerender": "node ../../bin/stencil build --config test-prerender/stencil.config-prerender.ts --prerender --debug && node test-prerender/no-script-build.js",
1616
"build.sibling": "node ../../bin/stencil build --config test-sibling/stencil.config.ts --debug",
17+
"build.globalScripts": "node ../../bin/stencil build --config test-global-script/stencil.config.ts --debug",
1718
"karma": "karma start karma.config.js",
1819
"karma.prod": "npm run build.all && npm run karma",
1920
"karma.webpack": "webpack-cli --config test-app/esm-webpack/webpack.config.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf8">
3+
<script src="./build/testglobalscript.esm.js" type="module"></script>
4+
<script src="./build/testglobalscript.js" nomodule></script>
5+
6+
<test-cmp></test-cmp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { setupDomTests } from '../util';
2+
3+
describe('global script', () => {
4+
const env = setupDomTests(document);
5+
6+
afterEach(() => {
7+
env?.tearDownDom();
8+
});
9+
10+
it('supports async execution', async () => {
11+
await env?.setupDom('/global-script/index.html');
12+
13+
const cmp = document.querySelector('test-cmp');
14+
expect(cmp).not.toBeNull();
15+
expect(typeof cmp?.textContent).toBe('string');
16+
17+
const renderedDelay = parseInt(cmp?.textContent?.slice('I am rendered after '.length)!);
18+
expect(renderedDelay).toBeGreaterThanOrEqual(1000);
19+
});
20+
});

test/karma/test-app/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export declare namespace SomeTypes {
1717
/**
1818
* Utilities for creating a test bed to execute HTML rendering tests against
1919
*/
20-
type DomTestUtilities = {
20+
export type DomTestUtilities = {
2121
/**
2222
* Create and render the HTML at the provided url
2323
* @param url a location on disk of a file containing the HTML to load

test/karma/test-global-script/package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "global-script",
3+
"main": "dist/index.cjs.js",
4+
"module": "dist/index.js",
5+
"collection": "dist/collection/collection-manifest.json",
6+
"types": "dist/types/components.d.ts",
7+
"volta": {
8+
"extends": "../package.json"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/**
4+
* This is an autogenerated file created by the Stencil compiler.
5+
* It contains typing information for all components that exist in this project.
6+
*/
7+
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
8+
export namespace Components {
9+
interface TestCmp {
10+
}
11+
}
12+
declare global {
13+
interface HTMLTestCmpElement extends Components.TestCmp, HTMLStencilElement {
14+
}
15+
var HTMLTestCmpElement: {
16+
prototype: HTMLTestCmpElement;
17+
new (): HTMLTestCmpElement;
18+
};
19+
interface HTMLElementTagNameMap {
20+
"test-cmp": HTMLTestCmpElement;
21+
}
22+
}
23+
declare namespace LocalJSX {
24+
interface TestCmp {
25+
}
26+
interface IntrinsicElements {
27+
"test-cmp": TestCmp;
28+
}
29+
}
30+
export { LocalJSX as JSX };
31+
declare module "@stencil/core" {
32+
export namespace JSX {
33+
interface IntrinsicElements {
34+
"test-cmp": LocalJSX.TestCmp & JSXBase.HTMLAttributes<HTMLTestCmpElement>;
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare global {
2+
interface Window {
3+
__testStart: number;
4+
}
5+
}
6+
7+
export default async function () {
8+
window.__testStart = Date.now();
9+
return new Promise((resolve) => setTimeout(() => resolve('done!'), 1000));
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Component, h } from '@stencil/core';
2+
3+
@Component({
4+
tag: 'test-cmp',
5+
scoped: true,
6+
})
7+
export class SiblingRoot {
8+
render() {
9+
return <div>I am rendered after {Date.now() - window.__testStart}</div>;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Config } from '../../../dist/declarations';
2+
const { WWW_OUT_DIR } = require('../constants');
3+
4+
export const config: Config = {
5+
namespace: 'TestGlobalScript',
6+
tsconfig: 'tsconfig.json',
7+
outputTargets: [
8+
{
9+
type: 'www',
10+
dir: `../${WWW_OUT_DIR}`,
11+
},
12+
],
13+
globalScript: 'src/global.ts',
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"alwaysStrict": true,
4+
"allowSyntheticDefaultImports": true,
5+
"allowUnreachableCode": true,
6+
"declaration": false,
7+
"resolveJsonModule": true,
8+
"experimentalDecorators": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"jsx": "react",
11+
"jsxFactory": "h",
12+
"lib": [
13+
"dom",
14+
"es2017"
15+
],
16+
"module": "esnext",
17+
"moduleResolution": "node",
18+
"noImplicitAny": false,
19+
"noImplicitReturns": false,
20+
"noUnusedLocals": false,
21+
"noUnusedParameters": false,
22+
"pretty": true,
23+
"target": "es2017",
24+
"useUnknownInCatchVariables": true,
25+
"baseUrl": ".",
26+
"paths": {
27+
"@stencil/core": ["../../../internal"],
28+
"@stencil/core/internal": ["../../../internal"],
29+
"@stencil/core/testing": ["../../../testing"]
30+
}
31+
},
32+
"include": [
33+
"src"
34+
]
35+
}

test/karma/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
],
4242
"exclude": [
4343
"test-prerender",
44-
"test-sibling"
44+
"test-sibling",
45+
"test-global-script"
4546
]
4647
}

0 commit comments

Comments
 (0)