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): sourcemap generation without ext runtime #4570

Merged
merged 2 commits into from
Jul 13, 2023
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { DIST_CUSTOM_ELEMENTS } from '@utils';
import ts from 'typescript';

import type * as d from '../../../declarations';
import { addOutputTargetCoreRuntimeApi, RUNTIME_APIS } from '../core-runtime-apis';
import { addCreateEvents } from '../create-event';
import { retrieveTsModifiers } from '../transform-utils';

Expand Down Expand Up @@ -35,11 +33,7 @@ export const updateNativeConstructor = (
// a constructor may not have a body (e.g. in the case of constructor overloads)
const cstrBodyStatements: ts.NodeArray<ts.Statement> = cstrMethod.body?.statements ?? ts.factory.createNodeArray();

let statements: ts.Statement[] = [
...nativeInit(moduleFile, cmp),
...addCreateEvents(moduleFile, cmp),
...cstrBodyStatements,
];
let statements: ts.Statement[] = [...nativeInit(cmp), ...addCreateEvents(moduleFile, cmp), ...cstrBodyStatements];

const hasSuper = cstrBodyStatements.some((s) => s.kind === ts.SyntaxKind.SuperKeyword);
if (!hasSuper) {
Expand All @@ -56,7 +50,7 @@ export const updateNativeConstructor = (
// create a constructor()
const statements: ts.Statement[] = [
createNativeConstructorSuper(),
...nativeInit(moduleFile, cmp),
...nativeInit(cmp),
...addCreateEvents(moduleFile, cmp),
];

Expand All @@ -67,14 +61,13 @@ export const updateNativeConstructor = (

/**
* Generates a series of expression statements used to help initialize a Stencil component
* @param moduleFile the Stencil module that will be instantiated
* @param cmp the component's metadata
* @returns the generated expression statements
*/
const nativeInit = (moduleFile: d.Module, cmp: d.ComponentCompilerMeta): ReadonlyArray<ts.ExpressionStatement> => {
const nativeInit = (cmp: d.ComponentCompilerMeta): ReadonlyArray<ts.ExpressionStatement> => {
const initStatements = [nativeRegisterHostStatement()];
if (cmp.encapsulation === 'shadow') {
initStatements.push(nativeAttachShadowStatement(moduleFile));
initStatements.push(nativeAttachShadowStatement());
}
return initStatements;
};
Expand All @@ -97,11 +90,9 @@ const nativeRegisterHostStatement = (): ts.ExpressionStatement => {

/**
* Generates an expression statement for attaching a shadow DOM tree to an element.
* @param moduleFile the Stencil module that will use the generated expression statement
* @returns the generated expression statement
*/
const nativeAttachShadowStatement = (moduleFile: d.Module): ts.ExpressionStatement => {
addOutputTargetCoreRuntimeApi(moduleFile, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
const nativeAttachShadowStatement = (): ts.ExpressionStatement => {
// Create an expression statement, `this.__attachShadow();`
return ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/transformers/core-runtime-apis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type * as d from '../../declarations';

export const ATTACH_SHADOW = '__stencil_attachShadow';
export const CREATE_EVENT = '__stencil_createEvent';
export const DEFINE_CUSTOM_ELEMENT = '__stencil_defineCustomElement';
export const GET_ELEMENT = '__stencil_getElement';
Expand All @@ -12,7 +11,6 @@ export const REGISTER_HOST = '__stencil_registerHost';
export const H = '__stencil_h';

export const RUNTIME_APIS = {
attachShadow: `attachShadow as ${ATTACH_SHADOW}`,
createEvent: `createEvent as ${CREATE_EVENT}`,
defineCustomElement: `defineCustomElement as ${DEFINE_CUSTOM_ELEMENT}`,
getElement: `getElement as ${GET_ELEMENT}`,
Expand Down
28 changes: 14 additions & 14 deletions src/compiler/transformers/test/core-runtime-apis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ describe('addCoreRuntimeApi()', () => {
expect(mockModule.coreRuntimeApis).toBeDefined();
expect(mockModule.coreRuntimeApis).toHaveLength(0);

addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);

addCoreRuntimeApi(mockModule, RUNTIME_APIS.createEvent);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow, RUNTIME_APIS.createEvent]);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host, RUNTIME_APIS.createEvent]);
});

it("does not allow duplicate entries in a module's coreRuntimeApis", () => {
expect(mockModule.coreRuntimeApis).toBeDefined();
expect(mockModule.coreRuntimeApis).toHaveLength(0);

addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);

// attempt to add the api again, doing so shall not create a duplicate entry
addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);
});
});

Expand All @@ -57,25 +57,25 @@ describe('addOutputTargetCoreRuntimeApi()', () => {
expect(mockModule.outputTargetCoreRuntimeApis).toBeDefined();
expect(Object.entries(mockModule.outputTargetCoreRuntimeApis)).toHaveLength(0);

addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow] });
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.Host);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host] });

addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.createEvent);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({
[DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow, RUNTIME_APIS.createEvent],
[DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host, RUNTIME_APIS.createEvent],
});
});

it("does not allow duplicate entries in a module's outputTargetCoreRuntimeApis", () => {
expect(mockModule.outputTargetCoreRuntimeApis).toBeDefined();
expect(Object.entries(mockModule.outputTargetCoreRuntimeApis)).toHaveLength(0);

addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow] });
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.Host);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host] });

// attempt to add the api again, doing so shall not create a duplicate entry
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow] });
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.Host);
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host] });
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/test/native-constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('nativeComponentTransform', () => {
const transpiledModule = transpileModule(code, null, compilerCtx, [], [transformer]);

expect(transpiledModule.outputText).toContain(
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement, attachShadow as __stencil_attachShadow } from "@stencil/core";`
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement } from "@stencil/core";`
);
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
});
Expand All @@ -63,7 +63,7 @@ describe('nativeComponentTransform', () => {
const transpiledModule = transpileModule(code, null, compilerCtx, [], [transformer]);

expect(transpiledModule.outputText).toContain(
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement, attachShadow as __stencil_attachShadow } from "@stencil/core";`
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement } from "@stencil/core";`
);
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ describe('custom-elements-delegates-focus', () => {
const elm: Element = app.querySelector('custom-elements-delegates-focus');

expect(elm.shadowRoot).toBeDefined();
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object
expect((elm.shadowRoot as any).delegatesFocus).toBe(true);
expect(elm.shadowRoot.delegatesFocus).toBe(true);
});

it('does not set delegatesFocus when shadow is set to "true"', async () => {
Expand All @@ -25,7 +24,6 @@ describe('custom-elements-delegates-focus', () => {
const elm: Element = app.querySelector('custom-elements-no-delegates-focus');

expect(elm.shadowRoot).toBeDefined();
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object
expect((elm.shadowRoot as any).delegatesFocus).toBe(false);
expect(elm.shadowRoot.delegatesFocus).toBe(false);
});
});