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(angular, react, vue): use defineCustomElement import to improve treeshaking #208

Merged
merged 18 commits into from
Dec 6, 2021
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
8 changes: 5 additions & 3 deletions packages/angular-output-target/angular-component-lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export const defineCustomElement = (tagName: string, customElement: any) => {
}

// tslint:disable-next-line: only-arrow-functions
export function ProxyCmp(opts: { tagName: string, customElement?: any, inputs?: any; methods?: any }) {
export function ProxyCmp(opts: { defineCustomElementFn?: () => void, inputs?: any; methods?: any }) {
const decorator = function (cls: any) {
const { tagName, customElement, inputs, methods } = opts;
const { defineCustomElementFn, inputs, methods } = opts;

defineCustomElement(tagName, customElement);
if (defineCustomElementFn !== undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another const function called defineCustomElements defined above, so that's why I called this defineCustomElementFn.

defineCustomElementFn();
}

if (inputs) {
proxyInputs(cls, inputs);
Expand Down
52 changes: 50 additions & 2 deletions packages/angular-output-target/package-lock.json

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

5 changes: 4 additions & 1 deletion packages/angular-output-target/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stencil/angular-output-target",
"version": "0.3.0",
"version": "0.4.0-2",
"description": "Angular output target for @stencil/core components.",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
Expand Down Expand Up @@ -37,6 +37,9 @@
"@angular/core": "8.2.14",
"@angular/forms": "8.2.14"
},
"peerDependencies": {
"@stencil/core": "^2.9.0"
},
"jest": {
"transform": {
"^.+\\.(js|ts|tsx)$": "<rootDir>/test/jest.preprocessor.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ function getProxyCmp(tagName: string, includeCustomElement: boolean, inputs: str
const hasMethods = methods.length > 0;

const proxMeta: string[] = [
`tagName: \'${tagName}\'`,
`customElement: ${includeCustomElement ? dashToPascalCase(tagName) + 'Cmp' : 'undefined'}`
`defineCustomElementFn: ${includeCustomElement ? 'define' + dashToPascalCase(tagName) : 'undefined'}`
];

if (hasInputs) proxMeta.push(`inputs: ['${inputs.join(`', '`)}']`);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-output-target/src/output-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import { ProxyCmp, proxyOutputs } from './angular-component-lib/utils';\n`;
const cmpImports = components.map(component => {
const pascalImport = dashToPascalCase(component.tagName);

return `import { ${pascalImport} as ${pascalImport}Cmp } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
'components'
}/${component.tagName}.js';`;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('createComponentDefinition', () => {
methods: [],
events: [],
}, true);
expect(output[0]).toEqual(`export const MyComponent = /*@__PURE__*/createReactComponent<JSX.MyComponent, HTMLMyComponentElement>('my-component', undefined, undefined, MyComponentCmp);`);
expect(output[0]).toEqual(`export const MyComponent = /*@__PURE__*/createReactComponent<JSX.MyComponent, HTMLMyComponentElement>('my-component', undefined, undefined, defineMyComponent);`);
});

it('should create a React component without custom element support', () => {
Expand Down
33 changes: 7 additions & 26 deletions packages/react-output-target/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/react-output-target/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stencil/react-output-target",
"version": "0.1.0",
"version": "0.2.0-0",
"description": "React output target for @stencil/core components.",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"url": "https://github.com/ionic-team/stencil-ds-output-targets/issues"
},
"peerDependencies": {
"@stencil/core": ">=1.8.0"
"@stencil/core": "^2.9.0"
},
"devDependencies": {
"@types/react": "^16.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
attachProps,
createForwardRef,
dashToPascalCase,
defineCustomElement,
isCoveredByReact,
mergeRefs,
} from './utils';
Expand All @@ -30,9 +29,11 @@ export const createReactComponent = <
originalProps: StencilReactInternalProps<ElementType>,
propsToPass: any,
) => ExpandedPropsTypes,
customElement?: any,
defineCustomElement?: () => void,
) => {
defineCustomElement(tagName, customElement);
if (defineCustomElement !== undefined) {
defineCustomElement();
}

const displayName = dashToPascalCase(tagName);
const ReactComponent = class extends React.Component<StencilReactInternalProps<ElementType>> {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-output-target/src/output-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { createReactComponent } from './react-component-lib';\n`;
const cmpImports = components.map(component => {
const pascalImport = dashToPascalCase(component.tagName);

return `import { ${pascalImport} as ${pascalImport}Cmp } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
'components'
}/${component.tagName}.js';`;
});
Expand Down Expand Up @@ -117,7 +117,7 @@ export function createComponentDefinition(cmpMeta: ComponentCompilerMeta, includ
let template = `export const ${tagNameAsPascal} = /*@__PURE__*/createReactComponent<${IMPORT_TYPES}.${tagNameAsPascal}, HTML${tagNameAsPascal}Element>('${cmpMeta.tagName}'`;

if (includeCustomElement) {
template += `, undefined, undefined, ${tagNameAsPascal}Cmp`;
template += `, undefined, undefined, define${tagNameAsPascal}`;
}

template += `);`;
Expand Down
33 changes: 31 additions & 2 deletions packages/svelte-output-target/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/svelte-output-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"url": "https://github.com/ionic-team/stencil-ds-output-targets/issues"
},
"peerDependencies": {
"@stencil/core": ">=1.8.0 || ^2.0.0"
"@stencil/core": "^2.9.0"
},
"devDependencies": {
"estree-walker": "^2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const MyComponent = /*@__PURE__*/ defineContainer<Components.MyComponent>
events: [],
});
expect(output).toEqual(`
export const MyComponent = /*@__PURE__*/ defineContainer<Components.MyComponent>('my-component', MyComponentCmp);
export const MyComponent = /*@__PURE__*/ defineContainer<Components.MyComponent>('my-component', defineMyComponent);
`
});

Expand Down
6 changes: 3 additions & 3 deletions packages/vue-output-target/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/vue-output-target/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stencil/vue-output-target",
"version": "0.5.1",
"version": "0.6.0-0",
"description": "Vue output target for @stencil/core components.",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"url": "https://github.com/ionic-team/stencil-ds-output-targets/issues"
},
"peerDependencies": {
"@stencil/core": ">=1.8.0 || ^2.0.0"
"@stencil/core": "^2.9.0"
},
"jest": {
"transform": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-output-target/src/generate-vue-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const createComponentDefinition = (
includeCustomElement: boolean = false
) => (cmpMeta: Pick<ComponentCompilerMeta, 'properties' | 'tagName' | 'methods' | 'events'>) => {
const tagNameAsPascal = dashToPascalCase(cmpMeta.tagName);
const importAs = (includeCustomElement) ? tagNameAsPascal + 'Cmp' : 'undefined';
const importAs = (includeCustomElement) ? 'define' + tagNameAsPascal : 'undefined';

let props: string[] = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/vue-output-target/src/output-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import { defineContainer } from './vue-component-lib/utils';\n`;
const cmpImports = components.map(component => {
const pascalImport = dashToPascalCase(component.tagName);

return `import { ${pascalImport} as ${pascalImport}Cmp } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
return `import { defineCustomElement as define${pascalImport} } from '${normalizePath(outputTarget.componentCorePackage!)}/${outputTarget.customElementsDir ||
'components'
}/${component.tagName}.js';`;
});
Expand Down
10 changes: 3 additions & 7 deletions packages/vue-output-target/vue-component-lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getElementClasses = (ref: Ref<HTMLElement | undefined>, componentClasses:
*/
export const defineContainer = <Props>(
name: string,
customElement: any,
defineCustomElement: any,
componentProps: string[] = [],
modelProp?: string,
modelUpdateEvent?: string,
Expand All @@ -63,12 +63,8 @@ export const defineContainer = <Props>(
* They refer to whatever properties are set on an instance of a component.
*/

if (
customElement !== undefined &&
typeof customElements !== 'undefined' &&
!customElements.get(name)
) {
customElements.define(name, customElement);
if (defineCustomElement !== undefined) {
defineCustomElement();
}

const Container = defineComponent<Props & InputProps>((props: any, { attrs, slots, emit }) => {
Expand Down