Skip to content

Commit

Permalink
feat(output): includeGlobalScripts option for custom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Dec 16, 2020
1 parent 8686b4c commit e7fa9c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/compiler/output-targets/dist-custom-elements-bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { removeCollectionImports } from '../../transformers/remove-collection-im
import { STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID, STENCIL_APP_GLOBALS_ID } from '../../bundle/entry-alias-ids';
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';

export const outputCustomElementsBundle = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
export const outputCustomElementsBundle = async (
config: d.Config,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
) => {
if (!config.buildDist) {
return;
}
Expand All @@ -28,7 +32,12 @@ export const outputCustomElementsBundle = async (config: d.Config, compilerCtx:
timespan.finish(`generate custom elements bundle finished`);
};

const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCustomElementsBundle) => {
const bundleCustomElements = async (
config: d.Config,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
outputTarget: d.OutputTargetDistCustomElementsBundle,
) => {
try {
const bundleOpts: BundleOptions = {
id: 'customElementsBundle',
Expand All @@ -41,7 +50,7 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
index: '\0core',
},
loader: {
'\0core': generateEntryPoint(buildCtx),
'\0core': generateEntryPoint(outputTarget, buildCtx),
},
inlineDynamicImports: outputTarget.inlineDynamicImports,
preserveEntrySignatures: 'allow-extension',
Expand Down Expand Up @@ -69,7 +78,9 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
if (!hasError(optimizeResults.diagnostics) && typeof optimizeResults.output === 'string') {
code = optimizeResults.output;
}
await compilerCtx.fs.writeFile(join(outputTarget.dir, bundle.fileName), code, { outputTargetType: outputTarget.type });
await compilerCtx.fs.writeFile(join(outputTarget.dir, bundle.fileName), code, {
outputTargetType: outputTarget.type,
});
}
});
await Promise.all(files);
Expand All @@ -79,7 +90,7 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
}
};

const generateEntryPoint = (buildCtx: d.BuildCtx) => {
const generateEntryPoint = (outputTarget: d.OutputTargetDistCustomElementsBundle, buildCtx: d.BuildCtx) => {
const imp: string[] = [];
const exp: string[] = [];
const exportNames: string[] = [];
Expand All @@ -88,10 +99,12 @@ const generateEntryPoint = (buildCtx: d.BuildCtx) => {
`import { proxyCustomElement } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
`export { setAssetPath } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
`export * from '${USER_INDEX_ENTRY_ID}';`,
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';`,
`globalScripts();`,
);

if (outputTarget.includeGlobalScripts !== false) {
imp.push(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';`, `globalScripts();`);
}

buildCtx.components.forEach(cmp => {
const exportName = dashToPascalCase(cmp.tagName);
const importName = cmp.componentClassName;
Expand Down Expand Up @@ -133,5 +146,9 @@ const getCustomElementBundleCustomTransformer = (config: d.Config, compilerCtx:
style: 'static',
styleImportData: 'queryparams',
};
return [updateStencilCoreImports(transformOpts.coreImportPath), nativeComponentTransform(compilerCtx, transformOpts), removeCollectionImports(compilerCtx)];
return [
updateStencilCoreImports(transformOpts.coreImportPath),
nativeComponentTransform(compilerCtx, transformOpts),
removeCollectionImports(compilerCtx),
];
};
1 change: 1 addition & 0 deletions src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@ export interface OutputTargetDistCustomElementsBundle extends OutputTargetBaseNe
externalRuntime?: boolean;
copy?: CopyTask[];
inlineDynamicImports?: boolean;
includeGlobalScripts?: boolean;
}

export interface OutputTargetBase {
Expand Down

0 comments on commit e7fa9c8

Please sign in to comment.