This commit is contained in:
2026-07-14 16:11:39 +03:00
parent 1280eb6fcd
commit ab7042001a
126 changed files with 5670 additions and 4120 deletions

View File

@@ -44,9 +44,9 @@ test('standalone generates an asset and deployment-neutral JSON manifest', async
assert.equal(result.manifestPath, path.join(rootDir, '.svg-sprite', 'svg-sprite.manifest.json'))
assert.deepEqual(generatedFiles(rootDir), [
'sprite.svg',
'state.json',
'svg-sprite.manifest.json',
])
assert.equal(fs.existsSync(path.join(rootDir, '.gitignore')), false)
const source = fs.readFileSync(result.spritePath, 'utf8')
assert.match(source, /@generated by @gromlab\/svg-sprites/)
@@ -65,8 +65,20 @@ test('standalone generates an asset and deployment-neutral JSON manifest', async
/^icon-[a-f0-9]{16}$/,
)
const state = JSON.parse(fs.readFileSync(path.join(rootDir, '.svg-sprite', 'state.json'), 'utf8'))
assert.deepEqual(state.owner, { mode: 'standalone', contractVersion: 2 })
})
test('standalone preserves a user-owned .gitignore', async () => {
const rootDir = fixture()
const gitignorePath = path.join(rootDir, '.gitignore')
fs.writeFileSync(gitignorePath, '/public/cache/\n')
await generateSprite(rootDir, {
mode: 'standalone',
name: 'app-icons',
generatedNotice: false,
})
assert.equal(fs.readFileSync(gitignorePath, 'utf8'), '/public/cache/\n')
})
test('standalone Vite generates a typed facade and resolved manifest', async () => {
@@ -78,13 +90,13 @@ test('standalone Vite generates a typed facade and resolved manifest', async ()
})
assert.equal(result.target, 'vite')
assert.equal(fs.existsSync(path.join(rootDir, '.gitignore')), true)
assert.deepEqual(generatedFiles(rootDir), [
'icon-data.d.ts',
'icon-data.js',
'index.d.ts',
'index.js',
'sprite.svg',
'state.json',
'svg-sprite.manifest.d.ts',
'svg-sprite.manifest.js',
])
@@ -92,13 +104,26 @@ test('standalone Vite generates a typed facade and resolved manifest', async ()
const entry = fs.readFileSync(path.join(rootDir, '.svg-sprite', 'index.js'), 'utf8')
const declarations = fs.readFileSync(path.join(rootDir, '.svg-sprite', 'index.d.ts'), 'utf8')
const manifest = fs.readFileSync(result.manifestPath, 'utf8')
const manifestDeclarations = fs.readFileSync(path.join(rootDir, '.svg-sprite', 'svg-sprite.manifest.d.ts'), 'utf8')
assert.match(entry, /sprite\.svg\?no-inline/)
assert.match(entry, /export const iconsSpriteUrl = spriteUrl/)
assert.match(entry, /export const iconsIconTagName = "icons-icon"/)
assert.match(entry, /export function getIconsIconHref\(icon\)/)
assert.match(entry, /export function defineIconsIconElement\(\)/)
assert.match(entry, /class IconsIconElement extends globalThis\.HTMLElement/)
assert.match(entry, /this\.attachShadow\(\{ mode: 'open' \}\)/)
assert.match(entry, /"folder open": "0 0 16 16"/)
assert.match(declarations, /getIconsIconHref\(icon: IconsIconName\): string/)
assert.match(declarations, /export interface IconsIconElement extends HTMLElement/)
assert.match(declarations, /icon: IconsIconName \| null/)
assert.match(declarations, /"icons-icon": IconsIconElement/)
assert.match(manifest, /"mode": "standalone@vite"/)
assert.match(manifest, /export const spriteManifest = createSpriteManifest\(spriteUrl\)/)
assert.doesNotMatch(entry, /react|jsx|\.css/)
assert.match(manifestDeclarations, /export type SpriteManifestData/)
assert.match(manifestDeclarations, /mode: 'standalone@vite'/)
assert.doesNotMatch(manifestDeclarations, /from '@gromlab\/svg-sprites'/)
assert.doesNotMatch(entry, /react|jsx|\.css|\blit\b/i)
})
test('standalone Webpack generates a typed facade and resolved manifest', async () => {
@@ -112,31 +137,39 @@ test('standalone Webpack generates a typed facade and resolved manifest', async
assert.equal(result.target, 'webpack')
const entry = fs.readFileSync(path.join(rootDir, '.svg-sprite', 'index.js'), 'utf8')
const manifest = fs.readFileSync(result.manifestPath, 'utf8')
const manifestDeclarations = fs.readFileSync(path.join(rootDir, '.svg-sprite', 'svg-sprite.manifest.d.ts'), 'utf8')
assert.match(entry, /new URL\('\.\/sprite\.svg', import\.meta\.url\)\.href/)
assert.match(entry, /export function getIconsIconHref\(icon\)/)
assert.match(entry, /export const iconsIconTagName = "icons-icon"/)
assert.match(entry, /export function defineIconsIconElement\(\)/)
assert.match(manifest, /"mode": "standalone@webpack"/)
assert.match(manifest, /new URL\('\.\/sprite\.svg', import\.meta\.url\)\.href/)
assert.doesNotMatch(entry, /react|jsx|\.css/)
assert.match(manifestDeclarations, /export type SpriteManifestData/)
assert.match(manifestDeclarations, /mode: 'standalone@webpack'/)
assert.doesNotMatch(manifestDeclarations, /from '@gromlab\/svg-sprites'/)
assert.doesNotMatch(entry, /react|jsx|\.css|\blit\b/i)
})
test('switching standalone modes removes obsolete managed files', async () => {
test('switching standalone modes replaces the generated directory', async () => {
const rootDir = fixture()
const options = { name: 'icons', generatedNotice: false }
await generateSprite(rootDir, { ...options, mode: 'standalone@vite' })
assert.equal(fs.existsSync(path.join(rootDir, '.gitignore')), true)
fs.writeFileSync(path.join(rootDir, '.svg-sprite', 'user.txt'), 'keep')
await generateSprite(rootDir, { ...options, mode: 'standalone' })
assert.deepEqual(generatedFiles(rootDir), [
'sprite.svg',
'state.json',
'svg-sprite.manifest.json',
'user.txt',
])
assert.equal(fs.readFileSync(path.join(rootDir, '.svg-sprite', 'user.txt'), 'utf8'), 'keep')
assert.equal(fs.existsSync(path.join(rootDir, '.gitignore')), false)
assert.equal(fs.existsSync(path.join(rootDir, '.svg-sprite', 'user.txt')), false)
await generateSprite(rootDir, { ...options, mode: 'standalone@webpack' })
assert.equal(fs.existsSync(path.join(rootDir, '.gitignore')), true)
assert.equal(fs.existsSync(path.join(rootDir, '.svg-sprite', 'svg-sprite.manifest.json')), false)
assert.equal(fs.existsSync(path.join(rootDir, '.svg-sprite', 'index.js')), true)
assert.equal(fs.existsSync(path.join(rootDir, '.svg-sprite', 'user.txt')), true)
assert.equal(fs.existsSync(path.join(rootDir, '.svg-sprite', 'user.txt')), false)
})