Skip to content

Commit e7fa9c8

Browse files
committed
feat(output): includeGlobalScripts option for custom elements
1 parent 8686b4c commit e7fa9c8

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/compiler/output-targets/dist-custom-elements-bundle/index.ts

+25-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { removeCollectionImports } from '../../transformers/remove-collection-im
1111
import { STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID, STENCIL_APP_GLOBALS_ID } from '../../bundle/entry-alias-ids';
1212
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
1313

14-
export const outputCustomElementsBundle = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
14+
export const outputCustomElementsBundle = async (
15+
config: d.Config,
16+
compilerCtx: d.CompilerCtx,
17+
buildCtx: d.BuildCtx,
18+
) => {
1519
if (!config.buildDist) {
1620
return;
1721
}
@@ -28,7 +32,12 @@ export const outputCustomElementsBundle = async (config: d.Config, compilerCtx:
2832
timespan.finish(`generate custom elements bundle finished`);
2933
};
3034

31-
const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCustomElementsBundle) => {
35+
const bundleCustomElements = async (
36+
config: d.Config,
37+
compilerCtx: d.CompilerCtx,
38+
buildCtx: d.BuildCtx,
39+
outputTarget: d.OutputTargetDistCustomElementsBundle,
40+
) => {
3241
try {
3342
const bundleOpts: BundleOptions = {
3443
id: 'customElementsBundle',
@@ -41,7 +50,7 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
4150
index: '\0core',
4251
},
4352
loader: {
44-
'\0core': generateEntryPoint(buildCtx),
53+
'\0core': generateEntryPoint(outputTarget, buildCtx),
4554
},
4655
inlineDynamicImports: outputTarget.inlineDynamicImports,
4756
preserveEntrySignatures: 'allow-extension',
@@ -69,7 +78,9 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
6978
if (!hasError(optimizeResults.diagnostics) && typeof optimizeResults.output === 'string') {
7079
code = optimizeResults.output;
7180
}
72-
await compilerCtx.fs.writeFile(join(outputTarget.dir, bundle.fileName), code, { outputTargetType: outputTarget.type });
81+
await compilerCtx.fs.writeFile(join(outputTarget.dir, bundle.fileName), code, {
82+
outputTargetType: outputTarget.type,
83+
});
7384
}
7485
});
7586
await Promise.all(files);
@@ -79,7 +90,7 @@ const bundleCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx
7990
}
8091
};
8192

82-
const generateEntryPoint = (buildCtx: d.BuildCtx) => {
93+
const generateEntryPoint = (outputTarget: d.OutputTargetDistCustomElementsBundle, buildCtx: d.BuildCtx) => {
8394
const imp: string[] = [];
8495
const exp: string[] = [];
8596
const exportNames: string[] = [];
@@ -88,10 +99,12 @@ const generateEntryPoint = (buildCtx: d.BuildCtx) => {
8899
`import { proxyCustomElement } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
89100
`export { setAssetPath } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
90101
`export * from '${USER_INDEX_ENTRY_ID}';`,
91-
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';`,
92-
`globalScripts();`,
93102
);
94103

104+
if (outputTarget.includeGlobalScripts !== false) {
105+
imp.push(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';`, `globalScripts();`);
106+
}
107+
95108
buildCtx.components.forEach(cmp => {
96109
const exportName = dashToPascalCase(cmp.tagName);
97110
const importName = cmp.componentClassName;
@@ -133,5 +146,9 @@ const getCustomElementBundleCustomTransformer = (config: d.Config, compilerCtx:
133146
style: 'static',
134147
styleImportData: 'queryparams',
135148
};
136-
return [updateStencilCoreImports(transformOpts.coreImportPath), nativeComponentTransform(compilerCtx, transformOpts), removeCollectionImports(compilerCtx)];
149+
return [
150+
updateStencilCoreImports(transformOpts.coreImportPath),
151+
nativeComponentTransform(compilerCtx, transformOpts),
152+
removeCollectionImports(compilerCtx),
153+
];
137154
};

src/declarations/stencil-public-compiler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,7 @@ export interface OutputTargetDistCustomElementsBundle extends OutputTargetBaseNe
18711871
externalRuntime?: boolean;
18721872
copy?: CopyTask[];
18731873
inlineDynamicImports?: boolean;
1874+
includeGlobalScripts?: boolean;
18741875
}
18751876

18761877
export interface OutputTargetBase {

0 commit comments

Comments
 (0)