Skip to content

Commit

Permalink
Move localization file size validation to core
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronbarker committed Jan 16, 2025
1 parent 4126f48 commit 6df2563
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-guests-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/app': patch
---

Remove localization file size validations from the CLI and move them into Shopify's backend.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ describe('loadLocalesConfig', () => {
})
})

test('Throws if one locale is too big', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const localesPath = joinPath(tmpDir, 'locales')
const enDefault = joinPath(localesPath, 'en.default.json')
const es = joinPath(localesPath, 'es.json')

await mkdir(localesPath)
await writeFile(enDefault, JSON.stringify({hello: 'Hello'}))
const bigArray = new Array(6000).fill('a')
await writeFile(es, JSON.stringify(bigArray))

// When
const got = loadLocalesConfig(tmpDir, 'checkout_ui')
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})

test('Throws if there are no defaults', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
Expand Down Expand Up @@ -92,23 +74,4 @@ describe('loadLocalesConfig', () => {
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})

test('Throws if bundle is too big', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const localesPath = joinPath(tmpDir, 'locales')
const en = joinPath(localesPath, 'en.default.json')
const es = joinPath(localesPath, 'es.json')

await mkdir(localesPath)
const bigArray = JSON.stringify(new Array(4000).fill('a'))

await writeFile(en, JSON.stringify(bigArray))
await writeFile(es, JSON.stringify(bigArray))

// When
const got = loadLocalesConfig(tmpDir, 'checkout_ui')
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})
})
19 changes: 0 additions & 19 deletions packages/app/src/cli/utilities/extensions/locales-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import {glob} from '@shopify/cli-kit/node/fs'
import {AbortError, BugError} from '@shopify/cli-kit/node/error'
import fs from 'fs'

const L10N_FILE_SIZE_LIMIT = 20 * 1024
const L10N_BUNDLE_SIZE_LIMIT = 256 * 1024

export async function loadLocalesConfig(extensionPath: string, extensionIdentifier: string) {
const localesPaths = await glob(joinPath(extensionPath, 'locales/*.json'))
if (localesPaths.length === 0) return {}

// Bundle validations
const totalBundleSize = bundleSize(localesPaths)
const defaultLanguageCode = findDefaultLocale(localesPaths)

if (defaultLanguageCode.length === 0)
Expand All @@ -26,20 +22,9 @@ export async function loadLocalesConfig(extensionPath: string, extensionIdentifi
`There must be one (and only one) locale identified as the default locale: e.g. "en.default.json"`,
)

if (totalBundleSize > L10N_BUNDLE_SIZE_LIMIT)
throw new AbortError(
`Error loading ${extensionIdentifier}`,
`Total size of all locale files must be less than ${L10N_BUNDLE_SIZE_LIMIT}`,
)

// Locale validations
for (const locale of localesPaths) {
const size = fs.statSync(locale).size
if (size > L10N_FILE_SIZE_LIMIT)
throw new AbortError(
`Error loading ${extensionIdentifier}`,
`Locale file ${locale} size must be less than ${L10N_FILE_SIZE_LIMIT}`,
)
if (size === 0) throw new AbortError(`Error loading ${extensionIdentifier}`, `Locale file ${locale} can't be empty`)
}

Expand All @@ -64,10 +49,6 @@ function getAllLocales(localesPath: string[]) {
return all
}

function bundleSize(localesPaths: string[]) {
return localesPaths.map((locale) => fs.statSync(locale).size).reduce((acc, size) => acc + size, 0)
}

function failIfUnset<T>(value: T | undefined, message: string) {
if (value === undefined) {
throw new BugError(message)
Expand Down

0 comments on commit 6df2563

Please sign in to comment.