Skip to content

Commit c7d7ab1

Browse files
committed
chore: more comprehensive codeSplitting option
1 parent a27932b commit c7d7ab1

File tree

8 files changed

+39
-16
lines changed

8 files changed

+39
-16
lines changed

example/awesome-framework/src/vite/photonPlugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function photonPlugin(): Plugin[] {
77
fullInstall: true,
88

99
// Disables code-splitting functionality for testing purposes
10-
codeSplitting: false,
10+
codeSplitting: {
11+
framework: false,
12+
},
1113

1214
// Always use those middlewares for all entries defined by Photon
1315
resolveMiddlewares() {

packages/adapter-cloudflare/src/plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export function cloudflare(config?: Omit<PluginConfig, 'viteEnvironment'>): Plug
1515
photon: {
1616
// @cloudflare/vite-plugin has its own dev server
1717
devServer: false,
18-
codeSplitting: false,
18+
codeSplitting: {
19+
target: false,
20+
},
1921
// Should be set to the value of cloudflareVitePlugins -> viteEnvironment.name
2022
// defaultBuildEnv: 'cloudflare',
2123
},

packages/photonjs/src/api/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function addPhotonEntry(pluginContext: PluginContext, name: string, entry
2222
.union([PhotonEntryUniversalHandler, PhotonEntryServerConfig])
2323
.parse(entryToPhoton('handler-entry', entry, name))
2424

25-
if (pluginContext.environment.config.photon.codeSplitting && parsed.type === 'server-config') {
25+
if (pluginContext.environment.config.photon.codeSplitting.framework && parsed.type === 'server-config') {
2626
throw new PhotonConfigError(
2727
`Photon entry with name "${entry.name}" is of type "server-config" but code splitting is enabled. Please disable code splitting or use "universal-handler" instead.`,
2828
)
@@ -45,7 +45,7 @@ export function updatePhotonEntry(pluginContext: PluginContext, name: string, en
4545
.union([PhotonEntryUniversalHandler, PhotonEntryServerConfig])
4646
.parse(entryToPhoton('handler-entry', entry, name))
4747

48-
if (pluginContext.environment.config.photon.codeSplitting && parsed.type === 'server-config') {
48+
if (pluginContext.environment.config.photon.codeSplitting.framework && parsed.type === 'server-config') {
4949
throw new PhotonConfigError(
5050
`Photon entry with name "${entry.name}" is of type "server-config" but code splitting is enabled. Please disable code splitting or use "universal-handler" instead.`,
5151
)

packages/photonjs/src/plugin/plugins/getMiddlewaresPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ async function getAllPhotonMiddlewares(
3131
if (!isDev) {
3232
// Only inject entries for the current environment
3333
universalEntries = universalEntries.filter((h) => (h.env || defaultBuildEnv) === currentEnv)
34+
if (pluginContext.environment.config.photon.codeSplitting.target) {
35+
// Do not inject isolated entries when target supports code splitting
36+
universalEntries = universalEntries.filter((h) => h.compositionMode !== 'isolated')
37+
}
3438
}
3539
const universalEntriesIds = metaHandler ? [metaHandler.id] : universalEntries.map((e) => e.id)
3640

packages/photonjs/src/plugin/plugins/resolvePhotonConfigPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export function resolvePhotonConfigPlugin(pluginConfig?: Photon.Config): Plugin[
2828
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
2929
resolvedPhotonConfig = resolvePhotonConfig(config.photon as any)
3030
}
31-
if (resolvedPhotonConfig.codeSplitting) {
31+
if (resolvedPhotonConfig.codeSplitting.framework) {
3232
const serverConfigEntries = resolvedPhotonConfig.entries.filter((e) => e.type === 'server-config')
3333
if (serverConfigEntries.length > 0) {
3434
throw new PhotonConfigError(
35-
'server-config entries are not supported when code splitting is enabled. Please disable code splitting or remove server-config entries.',
35+
'server-config entries are not supported when codeSplitting.framework is true. Please disable code splitting or remove server-config entries.',
3636
)
3737
}
3838
}

packages/photonjs/src/plugin/plugins/targetLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export function targetLoader(
4949
for (const entry of photon.entries) {
5050
if (
5151
(entry.env || 'ssr') === envName &&
52-
// if codeSplitting is enabled or if a target has explicitely been set, emit a new entry
53-
(photon.codeSplitting || entry.target)
52+
// if framework codeSplitting is enabled or if a target has explicitely been set, emit a new entry
53+
(photon.codeSplitting.framework || entry.target)
5454
) {
5555
this.emitFile({
5656
type: 'chunk',

packages/photonjs/src/validators/coerce.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ const resolver = Validators.PhotonConfig.transform((c) => {
8080
autoServe: excludeTrue(c.devServer)?.autoServe ?? true,
8181
},
8282
middlewares: c.middlewares ?? [],
83-
// codeSplitting must explicitely be set to true by targets
84-
codeSplitting: c.codeSplitting ?? false,
83+
codeSplitting: {
84+
framework: c.codeSplitting?.framework ?? true,
85+
target: c.codeSplitting?.target ?? false,
86+
},
8587
defaultBuildEnv: c.defaultBuildEnv ?? 'ssr',
8688
hmr: c.hmr ?? (isBun || isDeno ? 'prefer-restart' : true),
8789
})
@@ -91,6 +93,7 @@ export function mergePhotonConfig(configs: Photon.Config[]): Photon.Config {
9193
const resolving: Photon.Config = {}
9294
resolving.entries = {}
9395
resolving.middlewares = []
96+
resolving.codeSplitting = {}
9497
for (const config of configs) {
9598
// server
9699
if (config.server) {
@@ -132,9 +135,14 @@ export function mergePhotonConfig(configs: Photon.Config[]): Photon.Config {
132135

133136
// codeSplitting
134137
// if codeSplitting has already been set to false, keep it that way
135-
if (resolving.codeSplitting !== false) {
136-
if (typeof config.codeSplitting !== 'undefined') {
137-
resolving.codeSplitting = config.codeSplitting
138+
if (resolving.codeSplitting.framework !== false) {
139+
if (typeof config.codeSplitting?.framework !== 'undefined') {
140+
resolving.codeSplitting.framework = config.codeSplitting.framework
141+
}
142+
}
143+
if (resolving.codeSplitting.target !== false) {
144+
if (typeof config.codeSplitting?.target !== 'undefined') {
145+
resolving.codeSplitting.target = config.codeSplitting.target
138146
}
139147
}
140148

packages/photonjs/src/validators/validators.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const PhotonEntryPartial = PhotonEntryUniversalHandler.extend({
5757

5858
export const PhotonConfig = z.looseObject({
5959
server: z.union([z.string(), PhotonEntryServerPartial]).optional(),
60-
// TODO if codeSplitting is false and one entry does not have .id -> throw error
6160
// This means that only a framework setting codeSplitting: false can also add entries without .id
6261
entries: z.record(z.string(), z.union([z.string(), PhotonEntryPartial])).optional(),
6362
hmr: z.union([z.boolean(), z.literal('prefer-restart')]).optional(),
@@ -72,7 +71,12 @@ export const PhotonConfig = z.looseObject({
7271
/**
7372
* Can be set to false by frameworks or deployment targets if code splitting is not supported
7473
*/
75-
codeSplitting: z.boolean().optional(),
74+
codeSplitting: z
75+
.object({
76+
framework: z.boolean().optional(),
77+
target: z.boolean().optional(),
78+
})
79+
.optional(),
7680
devServer: z
7781
.union([
7882
z.boolean(),
@@ -94,7 +98,10 @@ export const PhotonConfigResolved = z.looseObject({
9498
}),
9599
),
96100
defaultBuildEnv: z.string(),
97-
codeSplitting: z.boolean(),
101+
codeSplitting: z.object({
102+
framework: z.boolean(),
103+
target: z.boolean(),
104+
}),
98105
devServer: z.union([
99106
z.literal(false),
100107
z.object({

0 commit comments

Comments
 (0)