Skip to content

Commit 84c4102

Browse files
authored
Merge pull request #6772 from Shopify/01-16-expose_tools_and_instructions_in_the_dev_server_payload
Expose tools and instructions in the Dev Server payload
2 parents be47a98 + 6ca4e28 commit 84c4102

6 files changed

Lines changed: 262 additions & 155 deletions

File tree

packages/app/src/cli/models/extensions/specification.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export interface Asset {
4848
content: string
4949
}
5050

51+
export interface BuildAsset {
52+
filepath: string
53+
module: string
54+
static?: boolean
55+
}
56+
5157
type BuildConfig =
5258
| {mode: 'ui' | 'theme' | 'function' | 'tax_calculation' | 'none'}
5359
| {mode: 'copy_files'; filePatterns: string[]; ignoredFilePatterns?: string[]}

packages/app/src/cli/models/extensions/specifications/ui_extension.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ describe('ui_extension', async () => {
207207
{
208208
target: 'EXTENSION::POINT::A',
209209
tools: undefined,
210+
instructions: undefined,
210211
module: './src/ExtensionPointA.js',
211212
metafields: [{namespace: 'test', key: 'test'}],
212213
default_placement_reference: undefined,
@@ -273,6 +274,7 @@ describe('ui_extension', async () => {
273274
{
274275
target: 'EXTENSION::POINT::A',
275276
tools: undefined,
277+
instructions: undefined,
276278
module: './src/ExtensionPointA.js',
277279
metafields: [],
278280
default_placement_reference: 'PLACEMENT_REFERENCE1',
@@ -335,6 +337,7 @@ describe('ui_extension', async () => {
335337
{
336338
target: 'EXTENSION::POINT::A',
337339
tools: undefined,
340+
instructions: undefined,
338341
module: './src/ExtensionPointA.js',
339342
metafields: [],
340343
urls: {},
@@ -397,6 +400,7 @@ describe('ui_extension', async () => {
397400
{
398401
target: 'EXTENSION::POINT::A',
399402
tools: undefined,
403+
instructions: undefined,
400404
module: './src/ExtensionPointA.js',
401405
metafields: [],
402406
default_placement_reference: undefined,
@@ -462,6 +466,7 @@ describe('ui_extension', async () => {
462466
{
463467
target: 'EXTENSION::POINT::A',
464468
tools: undefined,
469+
instructions: undefined,
465470
module: './src/ExtensionPointA.js',
466471
metafields: [],
467472
default_placement_reference: undefined,
@@ -529,6 +534,7 @@ describe('ui_extension', async () => {
529534
{
530535
target: 'EXTENSION::POINT::A',
531536
tools: undefined,
537+
instructions: undefined,
532538
module: './src/ExtensionPointA.js',
533539
metafields: [],
534540
default_placement_reference: undefined,
@@ -596,6 +602,7 @@ describe('ui_extension', async () => {
596602
target: 'EXTENSION::POINT::A',
597603
module: './src/ExtensionPointA.js',
598604
tools: './tools.json',
605+
instructions: undefined,
599606
metafields: [],
600607
default_placement_reference: undefined,
601608
capabilities: undefined,
@@ -663,6 +670,7 @@ describe('ui_extension', async () => {
663670
target: 'EXTENSION::POINT::A',
664671
module: './src/ExtensionPointA.js',
665672
tools: undefined,
673+
instructions: './instructions.md',
666674
metafields: [],
667675
default_placement_reference: undefined,
668676
capabilities: undefined,
@@ -890,6 +898,7 @@ Please check the configuration in ${joinPath(tmpDir, 'shopify.extension.toml')}`
890898
target: 'EXTENSION::POINT::A',
891899
module: './src/ExtensionPointA.js',
892900
tools: './tools.json',
901+
instructions: './instructions.md',
893902
metafields: [],
894903
default_placement_reference: undefined,
895904
capabilities: undefined,

packages/app/src/cli/models/extensions/specifications/ui_extension.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
createToolsTypeDefinition,
77
ToolsFileSchema,
88
} from './type-generation.js'
9-
import {Asset, AssetIdentifier, ExtensionFeature, createExtensionSpecification} from '../specification.js'
9+
import {Asset, AssetIdentifier, BuildAsset, ExtensionFeature, createExtensionSpecification} from '../specification.js'
1010
import {NewExtensionPointSchemaType, NewExtensionPointsSchema, BaseSchema, MetafieldSchema} from '../schemas.js'
1111
import {loadLocalesConfig} from '../../../utilities/extensions/locales-configuration.js'
1212
import {getExtensionPointTargetSurface} from '../../../services/dev/extension/utilities.js'
@@ -27,16 +27,10 @@ const validatePoints = (config: {extension_points?: unknown[]; targeting?: unkno
2727
export interface BuildManifest {
2828
assets: {
2929
// Main asset is always required
30-
[AssetIdentifier.Main]: {
31-
filepath: string
32-
module?: string
33-
}
34-
} & {
35-
[key in AssetIdentifier]?: {
36-
filepath: string
37-
module?: string
38-
static?: boolean
39-
}
30+
[AssetIdentifier.Main]: BuildAsset
31+
[AssetIdentifier.ShouldRender]?: BuildAsset
32+
[AssetIdentifier.Tools]?: BuildAsset
33+
[AssetIdentifier.Instructions]?: BuildAsset
4034
}
4135
}
4236

@@ -88,7 +82,6 @@ export const UIExtensionSchema = BaseSchema.extend({
8882
}
8983

9084
return {
91-
tools: targeting.tools,
9285
target: targeting.target,
9386
module: targeting.module,
9487
metafields: targeting.metafields ?? config.metafields ?? [],
@@ -97,6 +90,8 @@ export const UIExtensionSchema = BaseSchema.extend({
9790
capabilities: targeting.capabilities,
9891
preloads: targeting.preloads ?? {},
9992
build_manifest: buildManifest,
93+
tools: targeting.tools,
94+
instructions: targeting.instructions,
10095
}
10196
})
10297
return {...config, extension_points: extensionPoints}
@@ -148,27 +143,10 @@ const uiExtensionSpec = createExtensionSpecification({
148143

149144
const assets: {[key: string]: Asset} = {}
150145
extensionPoints.forEach((extensionPoint) => {
151-
// Start of Selection
152-
Object.entries(extensionPoint.build_manifest.assets).forEach(([identifier, asset]) => {
153-
if (identifier === AssetIdentifier.Main) {
154-
return
155-
}
156-
157-
// Skip static assets - they are copied after esbuild completes in rebuildContext
158-
if (asset.static && asset.module) {
159-
return
160-
}
161-
162-
assets[identifier] = {
163-
identifier: identifier as AssetIdentifier,
164-
outputFileName: asset.filepath,
165-
content: shouldIncludeShopifyExtend
166-
? `import shouldRender from '${asset.module}';shopify.extend('${getShouldRenderTarget(
167-
extensionPoint.target,
168-
)}', (...args) => shouldRender(...args));`
169-
: `import '${asset.module}'`,
170-
}
171-
})
146+
const shouldRenderAsset = buildShouldRenderAsset(extensionPoint, shouldIncludeShopifyExtend)
147+
if (shouldRenderAsset) {
148+
assets[AssetIdentifier.ShouldRender] = shouldRenderAsset
149+
}
172150
})
173151

174152
const assetsArray = Object.values(assets)
@@ -181,8 +159,8 @@ const uiExtensionSpec = createExtensionSpecification({
181159
if (!isRemoteDomExtension(config)) return
182160

183161
await Promise.all(
184-
config.extension_points.map((extensionPoint) => {
185-
if (!('build_manifest' in extensionPoint)) return Promise.resolve()
162+
config.extension_points.flatMap((extensionPoint) => {
163+
if (!('build_manifest' in extensionPoint)) return []
186164

187165
return Object.entries(extensionPoint.build_manifest.assets).map(([_, asset]) => {
188166
if (asset.static && asset.module) {
@@ -459,4 +437,23 @@ export function getShouldRenderTarget(target: string) {
459437
return target.replace(/\.render$/, '.should-render')
460438
}
461439

440+
function buildShouldRenderAsset(
441+
extensionPoint: NewExtensionPointSchemaType & {build_manifest: BuildManifest},
442+
shouldIncludeShopifyExtend: boolean,
443+
) {
444+
const shouldRenderAsset = extensionPoint.build_manifest.assets[AssetIdentifier.ShouldRender]
445+
if (!shouldRenderAsset) {
446+
return
447+
}
448+
return {
449+
identifier: AssetIdentifier.ShouldRender,
450+
outputFileName: shouldRenderAsset.filepath,
451+
content: shouldIncludeShopifyExtend
452+
? `import shouldRender from '${shouldRenderAsset.module}';shopify.extend('${getShouldRenderTarget(
453+
extensionPoint.target,
454+
)}', (...args) => shouldRender(...args));`
455+
: `import '${shouldRenderAsset.module}'`,
456+
}
457+
}
458+
462459
export default uiExtensionSpec

0 commit comments

Comments
 (0)