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

Add ability to skip polyfills and registration. #62

Merged
merged 1 commit into from
Jun 26, 2020
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
@@ -0,0 +1,86 @@
import { ComponentCompilerMeta } from '@stencil/core/internal';
import { generateProxies } from '../src/output-react';
import { PackageJSON, OutputTargetReact } from '../src/types';

describe('generateProxies', () => {
const components: ComponentCompilerMeta[] = [];
const pkgData: PackageJSON = {
types: 'dist/types/index.d.ts',
};
const rootDir: string = '';
beforeEach(() => {});

it('should include both polyfills and definCustomElements when both are true in the outputTarget', () => {
const outputTarget: OutputTargetReact = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-react/src/proxies.ts',
includePolyfills: true,
includeDefineCustomElements: true,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated react proxies */
import { createReactComponent } from './react-component-lib';

import { JSX } from 'component-library';

import { applyPolyfills, defineCustomElements } from 'component-library/loader';

applyPolyfills().then(() => defineCustomElements());

`,
);
});

it('should include only defineCustomElements when includePolyfills is false in the outputTarget', () => {
const outputTarget: OutputTargetReact = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-react/src/proxies.ts',
includePolyfills: false,
includeDefineCustomElements: true,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated react proxies */
import { createReactComponent } from './react-component-lib';

import { JSX } from 'component-library';

import { defineCustomElements } from 'component-library/loader';

defineCustomElements();

`,
);
});

it('should not include defineCustomElements or applyPolyfills if both are false in the outputTarget', () => {
const outputTarget: OutputTargetReact = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-react/src/proxies.ts',
includePolyfills: false,
includeDefineCustomElements: false,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated react proxies */
import { createReactComponent } from './react-component-lib';

import { JSX } from 'component-library';




`,
);
});
});
3 changes: 1 addition & 2 deletions packages/react-output-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"rollup": "rollup -c",
"version": "npm run build",
"release": "np",
"test": "jest --passWithNoTests",
"test.watch": "jest --watch"
"test": "../../node_modules/.bin/jest"
},
"repository": {
"type": "git",
Expand Down
35 changes: 21 additions & 14 deletions packages/react-output-target/src/output-react.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { OutputTargetReact } from './types';
import { OutputTargetReact, PackageJSON } from './types';
import { dashToPascalCase, normalizePath, readPackageJson, relativeImport, sortBy } from './utils';
import { CompilerCtx, ComponentCompilerMeta, Config } from '@stencil/core/internal';

Expand All @@ -11,24 +11,25 @@ export async function reactProxyOutput(
) {
const filteredComponents = getFilteredComponents(outputTarget.excludeComponents, components);
const rootDir = config.rootDir as string;
const pkgData = await readPackageJson(rootDir);

await generateProxies(compilerCtx, filteredComponents, outputTarget, rootDir);
const finalText = generateProxies(filteredComponents, pkgData, outputTarget, rootDir);
await compilerCtx.fs.writeFile(outputTarget.proxiesFile, finalText);
await copyResources(config, outputTarget);
}

function getFilteredComponents(excludeComponents: string[] = [], cmps: ComponentCompilerMeta[]) {
return sortBy(cmps, cmp => cmp.tagName).filter(
c => !excludeComponents.includes(c.tagName) && !c.internal,
return sortBy(cmps, (cmp) => cmp.tagName).filter(
(c) => !excludeComponents.includes(c.tagName) && !c.internal,
);
}

async function generateProxies(
compilerCtx: CompilerCtx,
export function generateProxies(
components: ComponentCompilerMeta[],
pkgData: PackageJSON,
outputTarget: OutputTargetReact,
rootDir: string,
) {
const pkgData = await readPackageJson(rootDir);
const distTypesDir = path.dirname(pkgData.types);
const dtsFilePath = path.join(rootDir, distTypesDir, GENERATED_DTS);
const componentsTypeFile = relativeImport(outputTarget.proxiesFile, dtsFilePath, '.d.ts');
Expand All @@ -42,14 +43,22 @@ import { createReactComponent } from './react-component-lib';\n`;
? `import { ${IMPORT_TYPES} } from '${normalizePath(componentsTypeFile)}';\n`
: `import { ${IMPORT_TYPES} } from '${normalizePath(outputTarget.componentCorePackage)}';\n`;

const sourceImports = `import { ${REGISTER_CUSTOM_ELEMENTS}, ${APPLY_POLYFILLS} } from '${normalizePath(
const pathToCorePackageLoader = normalizePath(
path.join(
outputTarget.componentCorePackage || '',
outputTarget.loaderDir || DEFAULT_LOADER_DIR,
),
)}';\n`;

const registerCustomElements = `${APPLY_POLYFILLS}().then(() => ${REGISTER_CUSTOM_ELEMENTS}());`;
);
let sourceImports = '';
let registerCustomElements = '';

if (outputTarget.includePolyfills && outputTarget.includeDefineCustomElements) {
sourceImports = `import { ${APPLY_POLYFILLS}, ${REGISTER_CUSTOM_ELEMENTS} } from '${pathToCorePackageLoader}';\n`;
registerCustomElements = `${APPLY_POLYFILLS}().then(() => ${REGISTER_CUSTOM_ELEMENTS}());`;
} else if (!outputTarget.includePolyfills && outputTarget.includeDefineCustomElements) {
sourceImports = `import { ${REGISTER_CUSTOM_ELEMENTS} } from '${pathToCorePackageLoader}';\n`;
registerCustomElements = `${REGISTER_CUSTOM_ELEMENTS}();`;
}

const final: string[] = [
imports,
Expand All @@ -59,9 +68,7 @@ import { createReactComponent } from './react-component-lib';\n`;
components.map(createComponentDefinition).join('\n'),
];

const finalText = final.join('\n') + '\n';

return compilerCtx.fs.writeFile(outputTarget.proxiesFile, finalText);
return final.join('\n') + '\n';
}

function createComponentDefinition(cmpMeta: ComponentCompilerMeta) {
Expand Down
7 changes: 4 additions & 3 deletions packages/react-output-target/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export const reactOutputTarget = (outputTarget: OutputTargetReact): OutputTarget
await reactProxyOutput(compilerCtx, outputTarget, buildCtx.components, config);

timespan.finish(`generate react finished`);
}
},
});


function normalizeOutputTarget(config: Config, outputTarget: any) {
const results: OutputTargetReact = {
...outputTarget,
excludeComponents: outputTarget.excludeComponents || []
excludeComponents: outputTarget.excludeComponents || [],
includePolyfills: outputTarget.includePolyfills ?? true,
includeDefineCustomElements: outputTarget.includeDefineCustomElements ?? true,
};

if (config.rootDir == null) {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-output-target/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export interface OutputTargetReact {
proxiesFile: string;
excludeComponents?: string[];
loaderDir?: string;
includePolyfills?: boolean;
includeDefineCustomElements?: boolean;
}

export interface PackageJSON {
Expand Down
107 changes: 107 additions & 0 deletions packages/vue-output-target/__tests__/generate-vue-compontes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { ComponentCompilerMeta } from '@stencil/core/internal';
import { generateProxies } from '../src/output-vue';
import { PackageJSON, OutputTargetVue } from '../src/types';

describe('generateProxies', () => {
const components: ComponentCompilerMeta[] = [];
const pkgData: PackageJSON = {
types: 'dist/types/index.d.ts',
};
const rootDir: string = '';
beforeEach(() => {});

it('should include both polyfills and definCustomElements when both are true in the outputTarget', () => {
const outputTarget: OutputTargetVue = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-vue/src/proxies.ts',
includePolyfills: true,
includeDefineCustomElements: true,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated vue proxies */
import Vue, { PropOptions } from 'vue';
import { createCommonRender, createCommonMethod } from './vue-component-lib/utils';

import { Components } from 'component-library';

import { applyPolyfills, defineCustomElements } from 'component-library/loader';

applyPolyfills().then(() => defineCustomElements());

const customElementTags: string[] = [

];
Vue.config.ignoredElements = [...Vue.config.ignoredElements, ...customElementTags];


`,
);
});

it('should include only defineCustomElements when includePolyfills is false in the outputTarget', () => {
const outputTarget: OutputTargetVue = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-vue/src/proxies.ts',
includePolyfills: false,
includeDefineCustomElements: true,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated vue proxies */
import Vue, { PropOptions } from 'vue';
import { createCommonRender, createCommonMethod } from './vue-component-lib/utils';

import { Components } from 'component-library';

import { defineCustomElements } from 'component-library/loader';

defineCustomElements();

const customElementTags: string[] = [

];
Vue.config.ignoredElements = [...Vue.config.ignoredElements, ...customElementTags];


`,
);
});

it('should not include defineCustomElements or applyPolyfills if both are false in the outputTarget', () => {
const outputTarget: OutputTargetVue = {
componentCorePackage: 'component-library',
proxiesFile: '../component-library-vue/src/proxies.ts',
includePolyfills: false,
includeDefineCustomElements: false,
};

const finalText = generateProxies(components, pkgData, outputTarget, rootDir);
expect(finalText).toEqual(
`/* eslint-disable */
/* tslint:disable */
/* auto-generated vue proxies */
import Vue, { PropOptions } from 'vue';
import { createCommonRender, createCommonMethod } from './vue-component-lib/utils';

import { Components } from 'component-library';




const customElementTags: string[] = [

];
Vue.config.ignoredElements = [...Vue.config.ignoredElements, ...customElementTags];


`,
);
});
});
3 changes: 1 addition & 2 deletions packages/vue-output-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"rollup": "rollup -c",
"version": "npm run build",
"release": "np",
"test": "jest --passWithNoTests",
"test.watch": "jest --watch"
"test": "../../node_modules/.bin/jest"
},
"repository": {
"type": "git",
Expand Down
30 changes: 19 additions & 11 deletions packages/vue-output-target/src/output-vue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { OutputTargetVue } from './types';
import { OutputTargetVue, PackageJSON } from './types';
import { CompilerCtx, ComponentCompilerMeta, Config } from '@stencil/core/internal';
import { createComponentDefinition } from './generate-vue-component';
import { normalizePath, readPackageJson, relativeImport, sortBy } from './utils';
Expand All @@ -12,8 +12,10 @@ export async function vueProxyOutput(
) {
const filteredComponents = getFilteredComponents(outputTarget.excludeComponents, components);
const rootDir = config.rootDir as string;
const pkgData = await readPackageJson(rootDir);

await generateProxies(compilerCtx, filteredComponents, outputTarget, rootDir);
const finalText = generateProxies(filteredComponents, pkgData, outputTarget, rootDir);
await compilerCtx.fs.writeFile(outputTarget.proxiesFile, finalText);
await copyResources(config, outputTarget);
}

Expand All @@ -23,13 +25,12 @@ function getFilteredComponents(excludeComponents: string[] = [], cmps: Component
);
}

async function generateProxies(
compilerCtx: CompilerCtx,
export function generateProxies(
components: ComponentCompilerMeta[],
pkgData: PackageJSON,
outputTarget: OutputTargetVue,
rootDir: string,
) {
const pkgData = await readPackageJson(rootDir);
const distTypesDir = path.dirname(pkgData.types);
const dtsFilePath = path.join(rootDir, distTypesDir, GENERATED_DTS);
const componentsTypeFile = relativeImport(outputTarget.proxiesFile, dtsFilePath, '.d.ts');
Expand All @@ -44,14 +45,22 @@ import { createCommonRender, createCommonMethod } from './vue-component-lib/util
? `import { ${IMPORT_TYPES} } from '${normalizePath(componentsTypeFile)}';\n`
: `import { ${IMPORT_TYPES} } from '${normalizePath(outputTarget.componentCorePackage)}';\n`;

const sourceImports = `import { ${REGISTER_CUSTOM_ELEMENTS}, ${APPLY_POLYFILLS} } from '${normalizePath(
const pathToCorePackageLoader = normalizePath(
path.join(
outputTarget.componentCorePackage || '',
outputTarget.loaderDir || DEFAULT_LOADER_DIR,
),
)}';\n`;
);
let sourceImports = '';
let registerCustomElements = '';

const registerCustomElements = `${APPLY_POLYFILLS}().then(() => ${REGISTER_CUSTOM_ELEMENTS}());`;
if (outputTarget.includePolyfills && outputTarget.includeDefineCustomElements) {
sourceImports = `import { ${APPLY_POLYFILLS}, ${REGISTER_CUSTOM_ELEMENTS} } from '${pathToCorePackageLoader}';\n`;
registerCustomElements = `${APPLY_POLYFILLS}().then(() => ${REGISTER_CUSTOM_ELEMENTS}());`;
} else if (!outputTarget.includePolyfills && outputTarget.includeDefineCustomElements) {
sourceImports = `import { ${REGISTER_CUSTOM_ELEMENTS} } from '${pathToCorePackageLoader}';\n`;
registerCustomElements = `${REGISTER_CUSTOM_ELEMENTS}();`;
}

const final: string[] = [
imports,
Expand All @@ -64,10 +73,9 @@ import { createCommonRender, createCommonMethod } from './vue-component-lib/util
.join('\n'),
];

const finalText = final.join('\n') + '\n';

return compilerCtx.fs.writeFile(outputTarget.proxiesFile, finalText);
return final.join('\n') + '\n';
}

function createIgnoredElementsString(components: ComponentCompilerMeta[]) {
const ignoredElements = components.map((component) => ` '${component.tagName}',`).join('\n');

Expand Down
Loading