Skip to content

Commit cd22da5

Browse files
fix(mock-doc): updated node.prepend to work even if there are no existing children (#6640)
* Updated node.prepend to work even if there are no existing children to insert the new child before * Updated addGlobalStyleToComponents option to default to 'client' and only attempt to add globalStyles when hydrating if the option is set to true * Formatted changes with prettier
1 parent 02f91b3 commit cd22da5

12 files changed

Lines changed: 45 additions & 25 deletions

src/compiler/bundle/app-data-plugin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { LoadResult, Plugin, ResolveIdResult, TransformResult } from 'rollu
55
import ts from 'typescript';
66

77
import type * as d from '../../declarations';
8+
import type { BundlePlatform } from './bundle-interface';
89
import { removeCollectionImports } from '../transformers/remove-collection-imports';
910
import { APP_DATA_CONDITIONAL, STENCIL_APP_DATA_ID, STENCIL_APP_GLOBALS_ID } from './entry-alias-ids';
1011

@@ -23,7 +24,7 @@ export const appDataPlugin = (
2324
compilerCtx: d.CompilerCtx,
2425
buildCtx: d.BuildCtx,
2526
buildConditionals: d.BuildConditionals,
26-
platform: 'client' | 'hydrate' | 'worker',
27+
platform: BundlePlatform,
2728
): Plugin => {
2829
if (!platform) {
2930
return {
@@ -62,7 +63,7 @@ export const appDataPlugin = (
6263
if (id === STENCIL_APP_GLOBALS_ID) {
6364
const s = new MagicString(``);
6465
appendGlobalScripts(globalScripts, s);
65-
await appendGlobalStyles(buildCtx, s);
66+
await appendGlobalStyles(buildCtx, s, platform);
6667
return s.toString();
6768
}
6869
if (id === STENCIL_APP_DATA_ID) {
@@ -215,12 +216,13 @@ const appendGlobalScripts = (globalScripts: GlobalScript[], s: MagicString) => {
215216
*
216217
* @param buildCtx the build context
217218
* @param s the MagicString to append the global styles onto
219+
* @param platform the platform that is being built
218220
*/
219-
const appendGlobalStyles = async (buildCtx: d.BuildCtx, s: MagicString) => {
220-
const globalStyles =
221-
buildCtx.config.globalStyle && buildCtx.config.extras.addGlobalStyleToComponents !== false
222-
? await buildCtx.stylesPromise
223-
: '';
221+
const appendGlobalStyles = async (buildCtx: d.BuildCtx, s: MagicString, platform: BundlePlatform) => {
222+
const { addGlobalStyleToComponents } = buildCtx.config.extras;
223+
const shouldIncludeGlobalStyles =
224+
addGlobalStyleToComponents === true || (addGlobalStyleToComponents === 'client' && platform === 'client');
225+
const globalStyles = buildCtx.config.globalStyle && shouldIncludeGlobalStyles ? await buildCtx.stylesPromise : '';
224226
s.append(`export const globalStyles = ${JSON.stringify(globalStyles)};\n`);
225227
};
226228

src/compiler/bundle/bundle-interface.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface BundleOptions {
1717
* and omitted from the generated bundle.
1818
*/
1919
externalRuntime?: boolean;
20-
platform: 'client' | 'hydrate' | 'worker';
20+
platform: BundlePlatform;
2121
/**
2222
* A collection of TypeScript transformation factories to apply during the "before" stage of the TypeScript
2323
* compilation pipeline (before built-in .js transformations)
@@ -59,3 +59,5 @@ export interface BundleOptions {
5959
*/
6060
preserveEntrySignatures?: PreserveEntrySignaturesOption;
6161
}
62+
63+
export type BundlePlatform = 'client' | 'hydrate' | 'worker';

src/compiler/bundle/core-resolve-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dirname } from 'path';
33
import type { Plugin } from 'rollup';
44

55
import type * as d from '../../declarations';
6+
import type { BundlePlatform } from './bundle-interface';
67
import { HYDRATED_CSS } from '../../runtime/runtime-constants';
78
import { fetchModuleAsync } from '../sys/fetch/fetch-module-async';
89
import { getStencilModuleUrl, packageVersions } from '../sys/fetch/fetch-utils';
@@ -20,7 +21,7 @@ import {
2021
export const coreResolvePlugin = (
2122
config: d.ValidatedConfig,
2223
compilerCtx: d.CompilerCtx,
23-
platform: 'client' | 'hydrate' | 'worker',
24+
platform: BundlePlatform,
2425
externalRuntime: boolean,
2526
lazyLoad: boolean,
2627
): Plugin => {

src/compiler/bundle/plugin-helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { buildError, relative } from '@utils';
22

33
import type * as d from '../../declarations';
4+
import type { BundlePlatform } from './bundle-interface';
45

5-
export const pluginHelper = (config: d.ValidatedConfig, builtCtx: d.BuildCtx, platform: string) => {
6+
export const pluginHelper = (config: d.ValidatedConfig, builtCtx: d.BuildCtx, platform: BundlePlatform) => {
67
return {
78
name: 'pluginHelper',
89
resolveId(importee: string, importer: string): null {

src/compiler/bundle/server-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { isAbsolute } from 'path';
33
import type { Plugin } from 'rollup';
44

55
import type * as d from '../../declarations';
6+
import type { BundlePlatform } from './bundle-interface';
67

7-
export const serverPlugin = (config: d.ValidatedConfig, platform: string): Plugin => {
8+
export const serverPlugin = (config: d.ValidatedConfig, platform: BundlePlatform): Plugin => {
89
const isHydrateBundle = platform === 'hydrate';
910
const serverVarid = `@removed-server-code`;
1011

src/compiler/bundle/worker-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { generatePreamble, hasError, normalizeFsPath } from '@utils';
22
import type { Plugin, PluginContext, TransformResult } from 'rollup';
33

44
import type * as d from '../../declarations';
5+
import type { BundlePlatform } from './bundle-interface';
56
import { optimizeModule } from '../optimize/optimize-module';
67
import { bundleOutput } from './bundle-output';
78
import { STENCIL_INTERNAL_ID } from './entry-alias-ids';
@@ -10,7 +11,7 @@ export const workerPlugin = (
1011
config: d.ValidatedConfig,
1112
compilerCtx: d.CompilerCtx,
1213
buildCtx: d.BuildCtx,
13-
platform: string,
14+
platform: BundlePlatform,
1415
inlineWorkers: boolean,
1516
): Plugin => {
1617
if (platform === 'worker' || platform === 'hydrate') {

src/compiler/config/test/validate-config.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ describe('validation', () => {
407407
expect(config.extras.tagNameTransform).toBe(false);
408408
expect(config.extras.additionalTagTransformers).toBe(false);
409409
expect(config.extras.scopedSlotTextContentFix).toBe(false);
410-
expect(config.extras.addGlobalStyleToComponents).toBe(true);
410+
expect(config.extras.addGlobalStyleToComponents).toBe('client');
411411
expect(config.extras.additionalTagTransformers).toBe(false);
412412
});
413413

src/compiler/config/validate-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ export const validateConfig = (
155155
validatedConfig.extras.additionalTagTransformers =
156156
validatedConfig.extras.additionalTagTransformers === true ||
157157
(!devMode && validatedConfig.extras.additionalTagTransformers === 'prod');
158-
validatedConfig.extras.addGlobalStyleToComponents = validatedConfig.extras.addGlobalStyleToComponents !== false;
158+
validatedConfig.extras.addGlobalStyleToComponents = isBoolean(validatedConfig.extras.addGlobalStyleToComponents)
159+
? validatedConfig.extras.addGlobalStyleToComponents
160+
: 'client';
159161

160162
// TODO(STENCIL-914): remove when `experimentalSlotFixes` is the default behavior
161163
// If the user set `experimentalSlotFixes` and any individual slot fix flags to `false`, we need to log a warning

src/declarations/stencil-private.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export interface BuildConditionals extends Partial<BuildFeatures> {
201201
experimentalSlotFixes?: boolean;
202202
// TODO(STENCIL-1086): remove this option when it's the default behavior
203203
experimentalScopedSlotChanges?: boolean;
204-
addGlobalStyleToComponents?: boolean;
204+
addGlobalStyleToComponents?: boolean | 'client';
205205
}
206206

207207
export type ModuleFormat =

src/declarations/stencil-public-compiler.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,18 @@ interface ConfigExtrasBase {
428428

429429
/**
430430
* By default Stencil turns the stylesheet provided to `globalStyle` into a constructable stylesheet
431-
* and adds it to each component which can be useful for sharing styles efficiently across components.
432-
* In some cases this can be undesirable:
433-
* - If `globalStyle` is intended to configure the lightDOM only
434-
* - If `globalStyle` is large it can bloat the size of SSR output when using declarative-shadow-dom
431+
* and adds it to each component when rendered on the client which can be useful for sharing styles
432+
* efficiently across components.
433+
*
434+
* If you want Stencil to also add the `globalStyle` to each component when rendering on the server
435+
* then set this to `true`. If your `globalStyle` sheet is large then doing this may bloat the size
436+
* of your SSR output when using declarative-shadow-dom.
437+
*
435438
* Setting this to `false` will prevent Stencil from adding any `globalStyle` to each component.
436439
*
437-
* Defaults to `true`.
440+
* Defaults to `'client'`.
438441
*/
439-
addGlobalStyleToComponents?: boolean;
442+
addGlobalStyleToComponents?: boolean | 'client';
440443
}
441444

442445
// TODO(STENCIL-914): delete this interface when `experimentalSlotFixes` is the default behavior

0 commit comments

Comments
 (0)