Skip to content

Commit 5d730ad

Browse files
committed
fix(esm): expose defineCustomElement()
1 parent d48b8f3 commit 5d730ad

1 file changed

Lines changed: 45 additions & 26 deletions

File tree

src/compiler/distribution/dist-esm.ts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { copyEsmCorePolyfills } from '../app/app-polyfills';
33
import { getComponentsEsmBuildPath, getComponentsEsmFileName, getCoreEsmFileName, getDefineCustomElementsPath, getDistEsmComponentsDir, getDistEsmDir, getDistEsmIndexPath, getLoaderEsmPath } from '../app/app-file-naming';
44
import { formatBrowserLoaderComponent } from '../../util/data-serialize';
55
import { normalizePath, pathJoin } from '../util';
6+
import { dashToPascalCase } from '../../util/helpers';
67

78

89
export async function generateEsmIndexes(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist) {
@@ -62,66 +63,70 @@ export async function generateEsmHosts(config: d.Config, compilerCtx: d.Compiler
6263
return;
6364
}
6465

66+
const esmImports: EsmImport[] = Object.keys(cmpRegistry).sort().map(tagName => {
67+
const cmpMeta = cmpRegistry[tagName];
68+
const data = JSON.stringify(formatBrowserLoaderComponent(cmpMeta));
69+
return {
70+
name: dashToPascalCase(tagName),
71+
data,
72+
};
73+
});
74+
6575
const hosts = [
66-
generateEsmLoader(config, compilerCtx, outputTarget),
76+
generateEsmLoader(config, compilerCtx, outputTarget, esmImports),
6777

68-
generateEsmHost(config, compilerCtx, cmpRegistry, outputTarget, 'es2017'),
78+
generateEsmHost(config, compilerCtx, outputTarget, 'es2017', esmImports),
6979
];
7080
if (config.buildEs5) {
7181
hosts.push(
72-
generateEsmHost(config, compilerCtx, cmpRegistry, outputTarget, 'es5')
82+
generateEsmHost(config, compilerCtx, outputTarget, 'es5', esmImports)
7383
);
7484
}
7585

7686
await Promise.all(hosts);
7787
}
7888

79-
export async function generateEsmHost(config: d.Config, compilerCtx: d.CompilerCtx, cmpRegistry: d.ComponentRegistry, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget) {
89+
export async function generateEsmHost(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget, esmImports: EsmImport[]) {
8090
await Promise.all([
81-
generateEsm(config, compilerCtx, cmpRegistry, outputTarget, sourceTarget),
82-
generateDefineCustomElements(config, compilerCtx, cmpRegistry, outputTarget, sourceTarget),
91+
generateEsm(config, compilerCtx, outputTarget, sourceTarget, esmImports),
92+
generateDefineCustomElements(config, compilerCtx, outputTarget, sourceTarget, esmImports),
8393
]);
8494
}
8595

86-
async function generateDefineCustomElements(config: d.Config, compilerCtx: d.CompilerCtx, cmpRegistry: d.ComponentRegistry, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget) {
87-
const componentClassList = Object.keys(cmpRegistry).map(tagName => {
88-
const cmpMeta: d.ComponentMeta = cmpRegistry[tagName];
89-
return cmpMeta.componentClass;
90-
}).sort();
91-
96+
async function generateDefineCustomElements(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget, esmImports: EsmImport[]) {
9297
const componentsFileName = getComponentsEsmFileName(config);
98+
const register = esmImports.map(c => `c.${c.name}`).join(',\n ');
9399
const c = `
94100
// ${config.namespace}: Custom Elements Define Library, ES Module/${sourceTarget} Target
95101
96102
import { defineCustomElement } from './${getCoreEsmFileName(config)}';
97-
import {\n ${componentClassList.join(',\n ')}\n} from './${componentsFileName}';
103+
import * as c from './${componentsFileName}';
104+
105+
export * from './${componentsFileName}';
106+
export { defineCustomElement };
98107
99108
export function defineCustomElements(win, opts) {
100-
return defineCustomElement(win, [\n ${componentClassList.join(',\n ')}\n ], opts);
109+
return defineCustomElement(win, [\n ${register}\n ], opts);
101110
}
102111
`;
103112

104113
const defineFilePath = getDefineCustomElementsPath(config, outputTarget, sourceTarget);
105-
106114
await compilerCtx.fs.writeFile(defineFilePath, c);
107115
}
108116

109-
async function generateEsm(config: d.Config, compilerCtx: d.CompilerCtx, cmpRegistry: d.ComponentRegistry, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget) {
117+
async function generateEsm(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetDist, sourceTarget: d.SourceTarget, esmImports: EsmImport[]) {
110118
const VAR = sourceTarget === 'es5' ? 'var' : 'const';
111-
const c = await Promise.all(Object.keys(cmpRegistry).sort().map(async tagName => {
112-
const cmpMeta = cmpRegistry[tagName];
113-
const data = JSON.stringify(formatBrowserLoaderComponent(cmpMeta));
114-
return `export ${VAR} ${cmpMeta.componentClass} = ${data};`;
115-
}));
119+
const indexContent = [
120+
`// ${config.namespace}: Host Data, ES Module/${sourceTarget} Target`,
116121

117-
c.unshift(`// ${config.namespace}: Host Data, ES Module/${sourceTarget} Target`);
122+
...esmImports.map(({name, data}) => `export ${VAR} ${name} = ${data};`)
123+
].join('\n');
118124

119125
const componentsEsmFilePath = getComponentsEsmBuildPath(config, outputTarget, sourceTarget);
120-
121-
await compilerCtx.fs.writeFile(componentsEsmFilePath, c.join('\n'));
126+
await compilerCtx.fs.writeFile(componentsEsmFilePath, indexContent);
122127
}
123128

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

136141
const indexPath = config.buildEs5 ? es5EntryPoint : es2017EntryPoint;
137-
const indexDtsContent = `export declare function defineCustomElements(win: any): Promise<void>;`;
142+
const indexDtsContent = generateIndexDts(esmImports);
138143
const indexContent = `export * from '${normalizePath(config.sys.path.relative(loaderPath, indexPath))}';`;
139144
const indexES2017Content = `export * from '${normalizePath(config.sys.path.relative(loaderPath, es2017EntryPoint))}';`;
140145

@@ -156,3 +161,17 @@ async function patchCollection(config: d.Config, compilerCtx: d.CompilerCtx, out
156161
const collectionInterfacePath = pathJoin(config, outputTarget.collectionDir, 'interface.js');
157162
await compilerCtx.fs.writeFile(collectionInterfacePath, '');
158163
}
164+
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');
172+
}
173+
174+
interface EsmImport {
175+
name: string;
176+
data: string;
177+
}

0 commit comments

Comments
 (0)