Skip to content

Commit d1be334

Browse files
authoredJul 13, 2023
fix(compiler): sourcemap generation without ext runtime (#4570)
* fix(compiler): sourcemap generation for non-external runtime this commit fixes a bug where projects using the `dist-custom-elements` output target with `externalRuntime: false` would receive the following error when building their project: ``` [ WARN ] Bundling Warning SOURCEMAP_ERROR Error when using sourcemap for reporting an error: Can't resolve original location of error. ``` the cause of this error was attempting to import a function, `attachShadow` that is no longer exported from the runtime bundle (as of #3117). to date, this has not had an effect on stencil (as the import gets treeshaken away). however, when trying to generate sourcemaps for a project using this configuration would cause a mismatch between what was expected to be in the produced output (the import statement) and what was really there (no import statement) * refactor(compiler): remove unused ATTACH_SHADOW API this commit removes the RUNTIME_APIS.ATTACH_SHADOW field that is no longer used in the codebase. it's only usage was in tests for adding runtime apis, and has been replaced with another field from `RUNTIME_APIS`
1 parent 01d7a9e commit d1be334

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed
 

‎src/compiler/transformers/component-native/native-constructor.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { DIST_CUSTOM_ELEMENTS } from '@utils';
21
import ts from 'typescript';
32

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

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

38-
let statements: ts.Statement[] = [
39-
...nativeInit(moduleFile, cmp),
40-
...addCreateEvents(moduleFile, cmp),
41-
...cstrBodyStatements,
42-
];
36+
let statements: ts.Statement[] = [...nativeInit(cmp), ...addCreateEvents(moduleFile, cmp), ...cstrBodyStatements];
4337

4438
const hasSuper = cstrBodyStatements.some((s) => s.kind === ts.SyntaxKind.SuperKeyword);
4539
if (!hasSuper) {
@@ -56,7 +50,7 @@ export const updateNativeConstructor = (
5650
// create a constructor()
5751
const statements: ts.Statement[] = [
5852
createNativeConstructorSuper(),
59-
...nativeInit(moduleFile, cmp),
53+
...nativeInit(cmp),
6054
...addCreateEvents(moduleFile, cmp),
6155
];
6256

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

6862
/**
6963
* Generates a series of expression statements used to help initialize a Stencil component
70-
* @param moduleFile the Stencil module that will be instantiated
7164
* @param cmp the component's metadata
7265
* @returns the generated expression statements
7366
*/
74-
const nativeInit = (moduleFile: d.Module, cmp: d.ComponentCompilerMeta): ReadonlyArray<ts.ExpressionStatement> => {
67+
const nativeInit = (cmp: d.ComponentCompilerMeta): ReadonlyArray<ts.ExpressionStatement> => {
7568
const initStatements = [nativeRegisterHostStatement()];
7669
if (cmp.encapsulation === 'shadow') {
77-
initStatements.push(nativeAttachShadowStatement(moduleFile));
70+
initStatements.push(nativeAttachShadowStatement());
7871
}
7972
return initStatements;
8073
};
@@ -97,11 +90,9 @@ const nativeRegisterHostStatement = (): ts.ExpressionStatement => {
9790

9891
/**
9992
* Generates an expression statement for attaching a shadow DOM tree to an element.
100-
* @param moduleFile the Stencil module that will use the generated expression statement
10193
* @returns the generated expression statement
10294
*/
103-
const nativeAttachShadowStatement = (moduleFile: d.Module): ts.ExpressionStatement => {
104-
addOutputTargetCoreRuntimeApi(moduleFile, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
95+
const nativeAttachShadowStatement = (): ts.ExpressionStatement => {
10596
// Create an expression statement, `this.__attachShadow();`
10697
return ts.factory.createExpressionStatement(
10798
ts.factory.createCallExpression(

‎src/compiler/transformers/core-runtime-apis.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type * as d from '../../declarations';
22

3-
export const ATTACH_SHADOW = '__stencil_attachShadow';
43
export const CREATE_EVENT = '__stencil_createEvent';
54
export const DEFINE_CUSTOM_ELEMENT = '__stencil_defineCustomElement';
65
export const GET_ELEMENT = '__stencil_getElement';
@@ -12,7 +11,6 @@ export const REGISTER_HOST = '__stencil_registerHost';
1211
export const H = '__stencil_h';
1312

1413
export const RUNTIME_APIS = {
15-
attachShadow: `attachShadow as ${ATTACH_SHADOW}`,
1614
createEvent: `createEvent as ${CREATE_EVENT}`,
1715
defineCustomElement: `defineCustomElement as ${DEFINE_CUSTOM_ELEMENT}`,
1816
getElement: `getElement as ${GET_ELEMENT}`,

‎src/compiler/transformers/test/core-runtime-apis.spec.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ describe('addCoreRuntimeApi()', () => {
2121
expect(mockModule.coreRuntimeApis).toBeDefined();
2222
expect(mockModule.coreRuntimeApis).toHaveLength(0);
2323

24-
addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
25-
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
24+
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
25+
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);
2626

2727
addCoreRuntimeApi(mockModule, RUNTIME_APIS.createEvent);
28-
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow, RUNTIME_APIS.createEvent]);
28+
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host, RUNTIME_APIS.createEvent]);
2929
});
3030

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

35-
addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
36-
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
35+
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
36+
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);
3737

3838
// attempt to add the api again, doing so shall not create a duplicate entry
39-
addCoreRuntimeApi(mockModule, RUNTIME_APIS.attachShadow);
40-
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.attachShadow]);
39+
addCoreRuntimeApi(mockModule, RUNTIME_APIS.Host);
40+
expect(mockModule.coreRuntimeApis).toEqual([RUNTIME_APIS.Host]);
4141
});
4242
});
4343

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

60-
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
61-
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow] });
60+
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.Host);
61+
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host] });
6262

6363
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.createEvent);
6464
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({
65-
[DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow, RUNTIME_APIS.createEvent],
65+
[DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host, RUNTIME_APIS.createEvent],
6666
});
6767
});
6868

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

73-
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.attachShadow);
74-
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.attachShadow] });
73+
addOutputTargetCoreRuntimeApi(mockModule, DIST_CUSTOM_ELEMENTS, RUNTIME_APIS.Host);
74+
expect(mockModule.outputTargetCoreRuntimeApis).toEqual({ [DIST_CUSTOM_ELEMENTS]: [RUNTIME_APIS.Host] });
7575

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

‎src/compiler/transformers/test/native-constructor.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('nativeComponentTransform', () => {
3838
const transpiledModule = transpileModule(code, null, compilerCtx, [], [transformer]);
3939

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

6565
expect(transpiledModule.outputText).toContain(
66-
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement, attachShadow as __stencil_attachShadow } from "@stencil/core";`
66+
`import { defineCustomElement as __stencil_defineCustomElement, HTMLElement } from "@stencil/core";`
6767
);
6868
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
6969
});

‎test/karma/test-app/custom-elements-delegates-focus/karma.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ describe('custom-elements-delegates-focus', () => {
1515
const elm: Element = app.querySelector('custom-elements-delegates-focus');
1616

1717
expect(elm.shadowRoot).toBeDefined();
18-
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object
19-
expect((elm.shadowRoot as any).delegatesFocus).toBe(true);
18+
expect(elm.shadowRoot.delegatesFocus).toBe(true);
2019
});
2120

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

2726
expect(elm.shadowRoot).toBeDefined();
28-
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object
29-
expect((elm.shadowRoot as any).delegatesFocus).toBe(false);
27+
expect(elm.shadowRoot.delegatesFocus).toBe(false);
3028
});
3129
});

0 commit comments

Comments
 (0)
Please sign in to comment.