Skip to content

Commit ea289f3

Browse files
committed
feat(loader): add exclude option
1 parent 918573b commit ea289f3

2 files changed

Lines changed: 19 additions & 24 deletions

File tree

src/client/core-esm.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ export { h } from '../renderer/vdom/h';
1414

1515

1616
export function defineCustomElement(win: Window, cmpData: d.ComponentHostData | d.ComponentHostData[], opts: CustomElementsDefineOptions = {}) {
17-
const cmpDataArray = (Array.isArray(cmpData) ? cmpData : [cmpData]) as d.ComponentHostData[];
17+
let cmpDataArray = (Array.isArray(cmpData) ? cmpData : [cmpData]) as d.ComponentHostData[];
1818
const doc = win.document;
1919
const hydratedCssClass = opts.hydratedCssClass || '__APP__HYDRATED__CSS__PLACEHOLDER__';
2020

21+
const exclude = opts['exclude'];
22+
if (exclude) {
23+
cmpDataArray = cmpDataArray.filter(c => exclude.indexOf(c[0]) === -1);
24+
}
2125
const styleCmps = cmpDataArray.map(c => c[0]);
2226
if (styleCmps.length > 0) {
2327
// auto hide components until they been fully hydrated
@@ -57,7 +61,7 @@ export function defineCustomElement(win: Window, cmpData: d.ComponentHostData |
5761

5862
function defineComponents() {
5963
// polyfills have been applied if need be
60-
(cmpData as d.ComponentHostData[]).forEach(c => {
64+
(cmpDataArray as d.ComponentHostData[]).forEach(c => {
6165
let HostElementConstructor: any;
6266

6367
if (isNative(win.customElements.define)) {
@@ -129,4 +133,5 @@ export interface CustomElementsDefineOptions {
129133
hydratedCssClass?: string;
130134
namespace?: string;
131135
resourcesUrl?: string;
136+
exclude?: string[];
132137
}

src/compiler/distribution/dist-esm.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ export async function generateEsmHosts(config: d.Config, compilerCtx: d.Compiler
6565

6666
const esmImports: EsmImport[] = Object.keys(cmpRegistry).sort().map(tagName => {
6767
const cmpMeta = cmpRegistry[tagName];
68-
const data = JSON.stringify(formatBrowserLoaderComponent(cmpMeta));
68+
const data = formatBrowserLoaderComponent(cmpMeta);
6969
return {
7070
name: dashToPascalCase(tagName),
7171
data,
7272
};
7373
});
7474

7575
const hosts = [
76-
generateEsmLoader(config, compilerCtx, outputTarget, esmImports),
76+
generateEsmLoader(config, compilerCtx, outputTarget),
7777

7878
generateEsmHost(config, compilerCtx, outputTarget, 'es2017', esmImports),
7979
];
@@ -89,24 +89,20 @@ export async function generateEsmHosts(config: d.Config, compilerCtx: d.Compiler
8989
export async function generateEsmHost(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget, esmImports: EsmImport[]) {
9090
await Promise.all([
9191
generateEsm(config, compilerCtx, outputTarget, sourceTarget, esmImports),
92-
generateDefineCustomElements(config, compilerCtx, outputTarget, sourceTarget, esmImports),
92+
generateDefineCustomElements(config, compilerCtx, outputTarget, sourceTarget),
9393
]);
9494
}
9595

96-
async function generateDefineCustomElements(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget, esmImports: EsmImport[]) {
96+
async function generateDefineCustomElements(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget) {
9797
const componentsFileName = getComponentsEsmFileName(config);
98-
const register = esmImports.map(c => `c.${c.name}`).join(',\n ');
9998
const c = `
10099
// ${config.namespace}: Custom Elements Define Library, ES Module/${sourceTarget} Target
101100
102101
import { defineCustomElement } from './${getCoreEsmFileName(config)}';
103-
import * as c from './${componentsFileName}';
104-
105-
export * from './${componentsFileName}';
106-
export { defineCustomElement };
102+
import { COMPONENTS } from './${componentsFileName}';
107103
108104
export function defineCustomElements(win, opts) {
109-
return defineCustomElement(win, [\n ${register}\n ], opts);
105+
return defineCustomElement(win, COMPONENTS, opts);
110106
}
111107
`;
112108

@@ -118,15 +114,14 @@ async function generateEsm(config: d.Config, compilerCtx: d.CompilerCtx, outputT
118114
const VAR = sourceTarget === 'es5' ? 'var' : 'const';
119115
const indexContent = [
120116
`// ${config.namespace}: Host Data, ES Module/${sourceTarget} Target`,
121-
122-
...esmImports.map(({name, data}) => `export ${VAR} ${name} = ${data};`)
117+
`export ${VAR} COMPONENTS = ${JSON.stringify(esmImports.map(({data}) => data))}`
123118
].join('\n');
124119

125120
const componentsEsmFilePath = getComponentsEsmBuildPath(config, outputTarget, sourceTarget);
126121
await compilerCtx.fs.writeFile(componentsEsmFilePath, indexContent);
127122
}
128123

129-
async function generateEsmLoader(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, esmImports: EsmImport[]) {
124+
async function generateEsmLoader(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist) {
130125
const loaderPath = getLoaderEsmPath(config, outputTarget);
131126
const es5EntryPoint = getDefineCustomElementsPath(config, outputTarget, 'es5');
132127
const es2017EntryPoint = getDefineCustomElementsPath(config, outputTarget, 'es2017');
@@ -139,7 +134,7 @@ async function generateEsmLoader(config: d.Config, compilerCtx: d.CompilerCtx, o
139134
}, null, 2);
140135

141136
const indexPath = config.buildEs5 ? es5EntryPoint : es2017EntryPoint;
142-
const indexDtsContent = generateIndexDts(esmImports);
137+
const indexDtsContent = generateIndexDts();
143138
const indexContent = `export * from '${normalizePath(config.sys.path.relative(loaderPath, indexPath))}';`;
144139
const indexES2017Content = `export * from '${normalizePath(config.sys.path.relative(loaderPath, es2017EntryPoint))}';`;
145140

@@ -162,16 +157,11 @@ async function patchCollection(config: d.Config, compilerCtx: d.CompilerCtx, out
162157
await compilerCtx.fs.writeFile(collectionInterfacePath, '');
163158
}
164159

165-
function generateIndexDts(esmImports: EsmImport[]) {
166-
return [
167-
'export declare function defineCustomElements(win: Window, opts?: any): Promise<void>;',
168-
'export declare function defineCustomElement(win: Window, elements: any[], opts?: any): Promise<void>;',
169-
170-
...esmImports.map(i => `export declare const ${i.name}: any;`)
171-
].join('\n');
160+
function generateIndexDts() {
161+
return 'export declare function defineCustomElements(win: any, opts?: any): Promise<void>;';
172162
}
173163

174164
interface EsmImport {
175165
name: string;
176-
data: string;
166+
data: any;
177167
}

0 commit comments

Comments
 (0)