Skip to content

Commit 72e5cc8

Browse files
Clean up module outputFile calculation and move it to the specifications
1 parent 91fb70d commit 72e5cc8

13 files changed

Lines changed: 54 additions & 40 deletions

File tree

packages/app/src/cli/models/extensions/extension-instance.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ describe('keepBuiltSourcemapsLocally', async () => {
7272

7373
await extensionInstance.keepBuiltSourcemapsLocally(bundleDirectory)
7474

75-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js'))).toBe(false)
76-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js.map'))).toBe(true)
77-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js'))).toBe(false)
78-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js.map'))).toBe(false)
75+
expect(fileExistsSync(joinPath(outputPath, 'uid1', 'scriptToMove.js'))).toBe(false)
76+
expect(fileExistsSync(joinPath(outputPath, 'uid1', 'scriptToMove.js.map'))).toBe(true)
77+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js'))).toBe(false)
78+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js.map'))).toBe(false)
7979
})
8080
})
8181
})
@@ -103,10 +103,10 @@ describe('keepBuiltSourcemapsLocally', async () => {
103103

104104
await extensionInstance.keepBuiltSourcemapsLocally(bundleInputPath)
105105

106-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js'))).toBe(false)
107-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js.map'))).toBe(false)
108-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js'))).toBe(false)
109-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js.map'))).toBe(false)
106+
expect(fileExistsSync(joinPath(outputPath, 'wrongUID', 'scriptToMove.js'))).toBe(false)
107+
expect(fileExistsSync(joinPath(outputPath, 'wrongUID', 'scriptToMove.js.map'))).toBe(false)
108+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js'))).toBe(false)
109+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js.map'))).toBe(false)
110110
})
111111
})
112112
})
@@ -133,10 +133,10 @@ describe('keepBuiltSourcemapsLocally', async () => {
133133

134134
await extensionInstance.keepBuiltSourcemapsLocally(bundleDirectory)
135135

136-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js'))).toBe(false)
137-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToMove.js.map'))).toBe(false)
138-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js'))).toBe(false)
139-
expect(fileExistsSync(joinPath(outputPath, 'dist', 'scriptToIgnore.js.map'))).toBe(false)
136+
expect(fileExistsSync(joinPath(outputPath, 'uid1', 'scriptToMove.js'))).toBe(false)
137+
expect(fileExistsSync(joinPath(outputPath, 'uid1', 'scriptToMove.js.map'))).toBe(false)
138+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js'))).toBe(false)
139+
expect(fileExistsSync(joinPath(outputPath, 'otherUID', 'scriptToIgnore.js.map'))).toBe(false)
140140
})
141141
})
142142
})

packages/app/src/cli/models/extensions/extension-instance.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {ok} from '@shopify/cli-kit/node/result'
2727
import {constantize, slugify} from '@shopify/cli-kit/common/string'
2828
import {hashString, nonRandomUUID} from '@shopify/cli-kit/node/crypto'
2929
import {partnersFqdn} from '@shopify/cli-kit/node/context/fqdn'
30-
import {joinPath, basename, normalizePath, resolvePath} from '@shopify/cli-kit/node/path'
30+
import {joinPath, normalizePath, resolvePath, relativePath, basename} from '@shopify/cli-kit/node/path'
3131
import {fileExists, touchFile, moveFile, writeFile, glob, copyFile, globSync} from '@shopify/cli-kit/node/fs'
3232
import {getPathValue} from '@shopify/cli-kit/common/object'
3333
import {outputDebug} from '@shopify/cli-kit/node/output'
@@ -137,14 +137,11 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
137137
}
138138

139139
get outputFileName() {
140-
const mode = this.specification.buildConfig.mode
141-
if (mode === 'copy_files' || mode === 'theme') {
142-
return ''
143-
} else if (mode === 'function') {
144-
return 'index.wasm'
145-
} else {
146-
return `${this.handle}.js`
147-
}
140+
return basename(this.outputRelativePath)
141+
}
142+
143+
get outputRelativePath() {
144+
return this.specification.getOutputRelativePath?.(this) ?? ''
148145
}
149146

150147
constructor(options: {
@@ -162,19 +159,9 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
162159
this.handle = this.buildHandle()
163160
this.localIdentifier = this.handle
164161
this.idEnvironmentVariableName = `SHOPIFY_${constantize(this.localIdentifier)}_ID`
165-
this.outputPath = this.directory
162+
this.outputPath = joinPath(this.directory, this.outputRelativePath)
166163
this.uid = this.buildUIDFromStrategy()
167164
this.devUUID = `dev-${this.uid}`
168-
169-
if (this.features.includes('esbuild') || this.type === 'tax_calculation') {
170-
this.outputPath = joinPath(this.directory, 'dist', this.outputFileName)
171-
}
172-
173-
if (this.isFunctionExtension) {
174-
const config = this.configuration as unknown as FunctionConfigType
175-
const defaultPath = joinPath('dist', 'index.wasm')
176-
this.outputPath = joinPath(this.directory, config.build?.path ?? defaultPath)
177-
}
178165
}
179166

180167
get draftMessages() {
@@ -247,7 +234,7 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
247234
const pathToMove = pathsToMove[0]
248235
if (pathToMove === undefined) return Promise.resolve()
249236

250-
const outputPath = joinPath(this.directory, 'dist', basename(pathToMove))
237+
const outputPath = joinPath(this.directory, relativePath(inputPath, pathToMove))
251238
await moveFile(pathToMove, outputPath, {overwrite: true})
252239
outputDebug(`Source map for ${this.localIdentifier} created: ${outputPath}`)
253240
}
@@ -408,8 +395,7 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
408395

409396
getOutputPathForDirectory(directory: string, outputId?: string) {
410397
const id = this.getOutputFolderId(outputId)
411-
const outputFile = this.outputFileName === '' ? '' : joinPath('dist', this.outputFileName)
412-
return joinPath(directory, id, outputFile)
398+
return joinPath(directory, id, this.outputRelativePath)
413399
}
414400

415401
get singleTarget() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface ExtensionSpecification<TConfiguration extends BaseConfigType =
7272
buildConfig: BuildConfig
7373
dependency?: string
7474
graphQLType?: string
75+
getOutputRelativePath?: (extension: ExtensionInstance<TConfiguration>) => string
7576
getBundleExtensionStdinContent?: (config: TConfiguration) => {main: string; assets?: Asset[]}
7677
deployConfig?: (
7778
config: TConfiguration,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import {ExtensionInstance} from '../extension-instance.js'
12
import {BaseSchema, MetafieldSchema} from '../schemas.js'
23
import {createExtensionSpecification} from '../specification.js'
34
import {zod} from '@shopify/cli-kit/node/schema'
45

56
const dependency = '@shopify/post-purchase-ui-extensions'
67

8+
type CheckoutPostPurchaseConfigType = zod.infer<typeof CheckoutPostPurchaseSchema>
79
const CheckoutPostPurchaseSchema = BaseSchema.extend({
810
metafields: zod.array(MetafieldSchema).optional(),
911
})
@@ -15,6 +17,8 @@ const checkoutPostPurchaseSpec = createExtensionSpecification({
1517
schema: CheckoutPostPurchaseSchema,
1618
appModuleFeatures: (_) => ['ui_preview', 'cart_url', 'esbuild', 'single_js_entry_path'],
1719
buildConfig: {mode: 'ui'},
20+
getOutputRelativePath: (extension: ExtensionInstance<CheckoutPostPurchaseConfigType>) =>
21+
`dist/${extension.handle}.js`,
1822
deployConfig: async (config, _) => {
1923
return {metafields: config.metafields ?? []}
2024
},

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {createExtensionSpecification} from '../specification.js'
22
import {BaseSchema, MetafieldSchema} from '../schemas.js'
33
import {loadLocalesConfig} from '../../../utilities/extensions/locales-configuration.js'
4+
import {ExtensionInstance} from '../extension-instance.js'
45
import {zod} from '@shopify/cli-kit/node/schema'
56

67
const dependency = '@shopify/checkout-ui-extensions'
78

9+
type CheckoutConfigType = zod.infer<typeof CheckoutSchema>
810
const CheckoutSchema = BaseSchema.extend({
911
name: zod.string(),
1012
extension_points: zod.array(zod.string()).optional(),
@@ -22,6 +24,7 @@ const checkoutSpec = createExtensionSpecification({
2224
schema: CheckoutSchema,
2325
appModuleFeatures: (_) => ['ui_preview', 'cart_url', 'esbuild', 'single_js_entry_path', 'generates_source_maps'],
2426
buildConfig: {mode: 'ui'},
27+
getOutputRelativePath: (extension: ExtensionInstance<CheckoutConfigType>) => `dist/${extension.handle}.js`,
2528
deployConfig: async (config, directory) => {
2629
return {
2730
extension_points: config.extension_points,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createExtensionSpecification} from '../specification.js'
22
import {BaseSchema} from '../schemas.js'
33
import {loadLocalesConfig} from '../../../utilities/extensions/locales-configuration.js'
4+
import {ExtensionInstance} from '../extension-instance.js'
45
import {zod} from '@shopify/cli-kit/node/schema'
56
import {joinPath} from '@shopify/cli-kit/node/path'
67
import {fileExists, readFile} from '@shopify/cli-kit/node/fs'
@@ -88,6 +89,8 @@ const functionSpec = createExtensionSpecification({
8889
schema: FunctionExtensionSchema,
8990
appModuleFeatures: (_) => ['function'],
9091
buildConfig: {mode: 'function'},
92+
getOutputRelativePath: (extension: ExtensionInstance<FunctionConfigType>) =>
93+
extension.configuration.build?.path ?? joinPath('dist', 'index.wasm'),
9194
deployConfig: async (config, directory, apiKey) => {
9295
let inputQuery: string | undefined
9396
const moduleId = randomUUID()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import {getDependencyVersion} from '../../app/app.js'
22
import {createExtensionSpecification} from '../specification.js'
33
import {BaseSchema} from '../schemas.js'
4+
import {ExtensionInstance} from '../extension-instance.js'
45
import {BugError} from '@shopify/cli-kit/node/error'
56
import {zod} from '@shopify/cli-kit/node/schema'
67

78
const dependency = '@shopify/retail-ui-extensions'
89

10+
type PosUIConfigType = zod.infer<typeof PosUISchema>
11+
const PosUISchema = BaseSchema.extend({name: zod.string()})
12+
913
const posUISpec = createExtensionSpecification({
1014
identifier: 'pos_ui_extension',
1115
dependency,
12-
schema: BaseSchema.extend({name: zod.string()}),
16+
schema: PosUISchema,
1317
appModuleFeatures: (_) => ['ui_preview', 'esbuild', 'single_js_entry_path'],
1418
buildConfig: {mode: 'ui'},
19+
getOutputRelativePath: (extension: ExtensionInstance<PosUIConfigType>) => `dist/${extension.handle}.js`,
1520
deployConfig: async (config, directory) => {
1621
const result = await getDependencyVersion(dependency, directory)
1722
if (result === 'not_found') throw new BugError(`Dependency ${dependency} not found`)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import {getDependencyVersion} from '../../app/app.js'
22
import {createExtensionSpecification} from '../specification.js'
33
import {BaseSchema} from '../schemas.js'
4+
import {ExtensionInstance} from '../extension-instance.js'
45
import {BugError} from '@shopify/cli-kit/node/error'
6+
import {zod} from '@shopify/cli-kit/node/schema'
57

68
const dependency = '@shopify/admin-ui-extensions'
79

10+
type ProductSubscriptionConfigType = zod.infer<typeof BaseSchema>
11+
812
const productSubscriptionSpec = createExtensionSpecification({
913
identifier: 'product_subscription',
1014
additionalIdentifiers: ['subscription_management'],
@@ -13,6 +17,7 @@ const productSubscriptionSpec = createExtensionSpecification({
1317
schema: BaseSchema,
1418
appModuleFeatures: (_) => ['ui_preview', 'esbuild', 'single_js_entry_path'],
1519
buildConfig: {mode: 'ui'},
20+
getOutputRelativePath: (extension: ExtensionInstance<ProductSubscriptionConfigType>) => `dist/${extension.handle}.js`,
1621
deployConfig: async (_, directory) => {
1722
const result = await getDependencyVersion(dependency, directory)
1823
if (result === 'not_found') throw new BugError(`Dependency ${dependency} not found`)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {createExtensionSpecification} from '../specification.js'
22
import {BaseSchema, MetafieldSchema} from '../schemas.js'
3+
import {ExtensionInstance} from '../extension-instance.js'
34
import {zod} from '@shopify/cli-kit/node/schema'
45

56
const CartLinePropertySchema = zod.object({
67
key: zod.string(),
78
})
89

10+
type TaxCalculationsConfigType = zod.infer<typeof TaxCalculationsSchema>
911
const TaxCalculationsSchema = BaseSchema.extend({
1012
production_api_base_url: zod.string(),
1113
benchmark_api_base_url: zod.string().optional(),
@@ -29,6 +31,7 @@ const spec = createExtensionSpecification({
2931
schema: TaxCalculationsSchema,
3032
appModuleFeatures: (_) => [],
3133
buildConfig: {mode: 'tax_calculation'},
34+
getOutputRelativePath: (extension: ExtensionInstance<TaxCalculationsConfigType>) => `dist/${extension.handle}.js`,
3235
deployConfig: async (config, _) => {
3336
return {
3437
production_api_base_url: config.production_api_base_url,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface BuildManifest {
3636

3737
const missingExtensionPointsMessage = 'No extension targets defined, add a `targeting` field to your configuration'
3838

39+
type UIExtensionConfigType = zod.infer<typeof UIExtensionSchema>
3940
export const UIExtensionSchema = BaseSchema.extend({
4041
name: zod.string(),
4142
type: zod.literal('ui_extension'),
@@ -102,6 +103,7 @@ const uiExtensionSpec = createExtensionSpecification({
102103
dependency,
103104
schema: UIExtensionSchema,
104105
buildConfig: {mode: 'ui'},
106+
getOutputRelativePath: (extension: ExtensionInstance<UIExtensionConfigType>) => `dist/${extension.handle}.js`,
105107
appModuleFeatures: (config) => {
106108
const basic: ExtensionFeature[] = ['ui_preview', 'esbuild', 'generates_source_maps']
107109
const needsCart =

0 commit comments

Comments
 (0)