Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate local sourcemap files for UI extensions #4687

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import {DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'
import {joinPath} from '@shopify/cli-kit/node/path'
import {describe, expect, test} from 'vitest'
import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs'
import {inTemporaryDirectory, readFile, mkdir, writeFile, fileExists, tempDirectory} from '@shopify/cli-kit/node/fs'
import {slugify} from '@shopify/cli-kit/common/string'
import {hashString} from '@shopify/cli-kit/node/crypto'
import {Writable} from 'stream'
Expand Down Expand Up @@ -114,6 +114,28 @@
})
})

describe('keepBuiltSourcemapsLocally', async () => {
test('moves the appropriate source map files to the expected directory', async () => {
await inTemporaryDirectory(async (bundleDirectory: string) => {
const outputPath = tempDirectory()
const extensionInstance = await testUIExtension({handle: 'scriptToMove', directory: outputPath})
const someDirPath = joinPath(bundleDirectory, 'some_dir')
const otherDirPath = joinPath(bundleDirectory, 'other_dir')
await mkdir(someDirPath).then(() => writeFile(joinPath(someDirPath, 'scriptToMove.js'), 'abc'))
await mkdir(someDirPath).then(() => writeFile(joinPath(someDirPath, 'scriptToMove.js.map'), 'abc map'))
await mkdir(otherDirPath).then(() => writeFile(joinPath(otherDirPath, 'scriptToIgnore.js'), 'abc'))
await mkdir(otherDirPath).then(() => writeFile(joinPath(otherDirPath, 'scriptToIgnore.js.map'), 'abc map'))

await extensionInstance.keepBuiltSourcemapsLocally(bundleDirectory)

expect(fileExists(joinPath(outputPath, 'scriptToMove.js'))).toBe(false)
expect(fileExists(joinPath(outputPath, 'scriptToMove.js.map'))).toBe(true)
expect(fileExists(joinPath(outputPath, 'scriptToIgnore.js'))).toBe(false)
expect(fileExists(joinPath(outputPath, 'scriptToIgnore.js.map'))).toBe(false)
})
})
})

describe('build', async () => {
test('creates a valid JS file for tax calculation extensions', async () => {
await inTemporaryDirectory(async (tmpDir) => {
Expand Down Expand Up @@ -337,7 +359,7 @@
// Given
const extensionInstance = await testUIExtension()

const result = extensionInstance.configuration.handle ?? slugify(extensionInstance.configuration.name ?? '')

Check warning on line 362 in packages/app/src/cli/models/extensions/extension-instance.test.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.
// Then
expect(extensionInstance.handle).toBe(result)
})
Expand All @@ -360,7 +382,7 @@
// When
const subscription = extensionInstance.configuration as unknown as SingleWebhookSubscriptionType
let result = ''
if (subscription) {

Check warning on line 385 in packages/app/src/cli/models/extensions/extension-instance.test.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, value is always truthy.
result = hashString(subscription.topic + subscription.uri + subscription.filter).substring(
0,
MAX_EXTENSION_HANDLE_LENGTH,
Expand Down
25 changes: 23 additions & 2 deletions packages/app/src/cli/models/extensions/extension-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import {constantize, slugify} from '@shopify/cli-kit/common/string'
import {hashString, randomUUID} from '@shopify/cli-kit/node/crypto'
import {partnersFqdn} from '@shopify/cli-kit/node/context/fqdn'
import {joinPath} from '@shopify/cli-kit/node/path'
import {fileExists, touchFile, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath, basename} from '@shopify/cli-kit/node/path'
import {fileExists, touchFile, moveFile, writeFile, glob} from '@shopify/cli-kit/node/fs'
import {getPathValue} from '@shopify/cli-kit/common/object'
import {useThemebundling} from '@shopify/cli-kit/node/context/local'

Expand Down Expand Up @@ -112,6 +112,10 @@
return this.features.includes('esbuild')
}

get isSourceMapGeneratingExtension() {
return this.features.includes('generates_source_maps')
}

get isAppConfigExtension() {
return ['single', 'dynamic'].includes(this.specification.uidStrategy)
}
Expand Down Expand Up @@ -219,6 +223,23 @@
return this.specification.buildValidation(this)
}

async keepBuiltSourcemapsLocally(bundleDirectory: string): Promise<void> {
if (!this.isSourceMapGeneratingExtension) return Promise.resolve()

const pathsToMove = await glob(`**/${this.handle}*.map`, {
cwd: bundleDirectory,
absolute: true,
followSymbolicLinks: false,
})

await Promise.all(
pathsToMove.map(async (filePath) => {
const outputPath = joinPath(this.directory, 'dist', basename(filePath))
await moveFile(filePath, outputPath, {overwrite: true})
}),
)
}

async publishURL(options: {orgId: string; appId: string; extensionId?: string}) {
const fqdn = await partnersFqdn()
const parnersPath = this.specification.partnersWebIdentifier
Expand All @@ -230,7 +251,7 @@
if (this.specification.getBundleExtensionStdinContent) {
return this.specification.getBundleExtensionStdinContent(this.configuration)
}
const relativeImportPath = this.entrySourceFilePath?.replace(this.directory, '')

Check warning on line 254 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary optional chain on a non-nullish value.
return `import '.${relativeImportPath}';`
}

Expand All @@ -239,7 +260,7 @@
}

hasExtensionPointTarget(target: string): boolean {
return this.specification.hasExtensionPointTarget?.(this.configuration, target) || false

Check warning on line 263 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
}

// Functions specific properties
Expand All @@ -257,7 +278,7 @@
return null
}

const watchPaths: string[] = configuredPaths ?? []

Check warning on line 281 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.
if (this.isJavaScript && configuredPaths.length === 0) {
watchPaths.push(joinPath('src', '**', '*.{js,ts}'))
}
Expand Down Expand Up @@ -291,7 +312,7 @@
}

get isJavaScript() {
return Boolean(this.entrySourceFilePath?.endsWith('.js') || this.entrySourceFilePath?.endsWith('.ts'))

Check warning on line 315 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary optional chain on a non-nullish value.

Check warning on line 315 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary optional chain on a non-nullish value.
}

async build(options: ExtensionBuildOptions): Promise<void> {
Expand Down Expand Up @@ -328,7 +349,7 @@
}

get singleTarget() {
const targets = (getPathValue(this.configuration, 'targeting') as {target: string}[]) ?? []

Check warning on line 352 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.

Check warning on line 352 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/non-nullable-type-assertion-style] Use a ! assertion to more succinctly remove null and undefined from the type.
if (targets.length !== 1) return undefined
return targets[0]?.target
}
Expand Down Expand Up @@ -372,7 +393,7 @@
case 'single':
return slugify(this.specification.identifier)
case 'uuid':
return this.configuration.handle ?? slugify(this.configuration.name ?? '')

Check warning on line 396 in packages/app/src/cli/models/extensions/extension-instance.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.
case 'dynamic':
// Hardcoded temporal solution for webhooks
const subscription = this.configuration as unknown as SingleWebhookSubscriptionType
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/cli/models/extensions/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type ExtensionFeature =
| 'cart_url'
| 'esbuild'
| 'single_js_entry_path'
| 'generates_source_maps'

export interface TransformationConfig {
[key: string]: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const checkoutSpec = createExtensionSpecification({
identifier: 'checkout_ui_extension',
dependency,
schema: CheckoutSchema,
appModuleFeatures: (_) => ['ui_preview', 'bundling', 'cart_url', 'esbuild', 'single_js_entry_path'],
appModuleFeatures: (_) => [
'ui_preview',
'bundling',
'cart_url',
'esbuild',
'single_js_entry_path',
'generates_source_maps',
],
deployConfig: async (config, directory) => {
return {
extension_points: config.extension_points,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
return {
target: targeting.target,
module: targeting.module,
metafields: targeting.metafields ?? config.metafields ?? [],

Check warning on line 31 in packages/app/src/cli/models/extensions/specifications/ui_extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.
default_placement_reference: targeting.default_placement,
capabilities: targeting.capabilities,
}
Expand All @@ -41,9 +41,9 @@
dependency,
schema: UIExtensionSchema,
appModuleFeatures: (config) => {
const basic: ExtensionFeature[] = ['ui_preview', 'bundling', 'esbuild']
const basic: ExtensionFeature[] = ['ui_preview', 'bundling', 'esbuild', 'generates_source_maps']
const needsCart =
config?.extension_points?.find((extensionPoint) => {

Check warning on line 46 in packages/app/src/cli/models/extensions/specifications/ui_extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary optional chain on a non-nullish value.
return getExtensionPointTargetSurface(extensionPoint.target) === 'checkout'
}) !== undefined
return needsCart ? [...basic, 'cart_url'] : basic
Expand Down Expand Up @@ -83,7 +83,7 @@
const uniqueTargets: string[] = []
const duplicateTargets: string[] = []

if (!extensionPoints || extensionPoints.length === 0) {

Check warning on line 86 in packages/app/src/cli/models/extensions/specifications/ui_extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-unnecessary-condition] Unnecessary conditional, value is always falsy.
return err(missingExtensionPointsMessage)
}

Expand All @@ -100,11 +100,11 @@
)
}

if (!uniqueTargets.includes(target)) {
uniqueTargets.push(target)
} else {
duplicateTargets.push(target)
}

Check warning on line 107 in packages/app/src/cli/models/extensions/specifications/ui_extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/models/extensions/specifications/ui_extension.ts#L103-L107

[no-negated-condition] Unexpected negated condition.
}

if (duplicateTargets.length) {
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/cli/services/build/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export async function buildUIExtension(extension: ExtensionInstance, options: Ex
env,
stderr: options.stderr,
stdout: options.stdout,
sourceMaps: extension.isSourceMapGeneratingExtension,
})
} catch (extensionBundlingError) {
// this fails if the app's own source code is broken; wrap such that this isn't flagged as a CLI bug
Expand Down
6 changes: 6 additions & 0 deletions packages/app/src/cli/services/deploy/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export async function bundleAndBuildExtensions(options: BundleOptions, systemEnv
showTimestamps: false,
})

await Promise.all(
options.app.allExtensions.map(async (extension) => {
await extension.keepBuiltSourcemapsLocally(bundleDirectory)
}),
)

if (options.bundlePath) {
await zip({
inputDirectory: bundleDirectory,
Expand Down
Loading