diff --git a/src/utilities/manifest.ts b/src/utilities/manifest.ts index c9bac25..60cca13 100644 --- a/src/utilities/manifest.ts +++ b/src/utilities/manifest.ts @@ -86,36 +86,6 @@ export async function generateManifestFiles( } } - for (const node of themeNodes) { - // Add theme nodes not present in the old import map - // They have been added manually by the user since the last time the import map was generated - if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) { - const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name) - - if (collectionNode) { - if (options.ignoreConflicts) { - // If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry - continue; - } else { - // If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry - newFilesMap[node.themeFolder][node.name] = '@theme' - logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`) - } - } else { - // If the node does not exist in the collection, add it to the new import map as a @theme entry - newFilesMap[node.themeFolder][node.name] = '@theme' - } - } - - // Persist prexisting asset entries from @theme or other collections - if (node.type === 'asset') { - const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name] - if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') { - newFilesMap[node.themeFolder][node.name] = oldImportMapValue - } - } - } - function addFilesMapEntry(themeFolder: LiquidNode['themeFolder'], name: string) { const oldImportMapValue = oldFilesMap[themeFolder]?.[name] const newImportMapValue = newFilesMap[themeFolder]?.[name] @@ -184,5 +154,44 @@ export async function generateManifestFiles( } } + for (const node of themeNodes) { + // Add theme nodes not present in the old import map + // They have been added manually by the user since the last time the import map was generated + if ((node.type === 'snippet' || node.type === 'asset') && !oldFilesMap[node.themeFolder]?.[node.name]) { + const collectionNode = collectionNodes.find(n => n.themeFolder === node.themeFolder && n.name === node.name) + + if (collectionNode) { + if (options.ignoreConflicts) { + // If the user has passed the --ignore-conflicts flag, skip the node so it can be logged later as a component entry + continue; + } else { + // If the node also exists in the collection, warn the user of the potential conflict but keep as a @theme entry + newFilesMap[node.themeFolder][node.name] = '@theme' + logger.log(`Conflict Warning: Pre-existing file ${node.themeFolder}/${node.name} without mapping conflicts with file in ${collectionName}. Keeping the theme file.`) + } + } else { + // If the node does not exist in the collection, add it to the new import map as a @theme entry + newFilesMap[node.themeFolder][node.name] = '@theme' + } + } + + // Persist prexisting asset entries from @theme or other collections + if (node.type === 'asset') { + const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name] + if (oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') { + newFilesMap[node.themeFolder][node.name] = oldImportMapValue + } + } + + // Persist prexisting snippet entries from @theme or other collections + if (node.type === 'snippet') { + const oldImportMapValue = oldFilesMap[node.themeFolder]?.[node.name] + const newImportMapValue = newFilesMap[node.themeFolder]?.[node.name] + if (newImportMapValue === undefined && oldImportMapValue !== collectionName && typeof oldImportMapValue === 'string') { + newFilesMap[node.themeFolder][node.name] = oldImportMapValue + } + } + } + return newFilesMap } \ No newline at end of file diff --git a/test/commands/theme/component/map.test.ts b/test/commands/theme/component/map.test.ts index 4f06d27..153b3ba 100644 --- a/test/commands/theme/component/map.test.ts +++ b/test/commands/theme/component/map.test.ts @@ -76,6 +76,18 @@ describe('theme component map', () => { expect(data.files.snippets['include-tag.liquid']).to.equal('@theme') // Should work with include tag }) + it('persists entries that are associated with @theme or another collection in the manifest', async () => { + const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) + expect(beforeData.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme') + expect(beforeData.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection') + + await runCommand(['theme', 'component', 'map', testThemePath]) + + const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) + expect(data.files.snippets['unreferenced-theme-snippet.liquid']).to.equal('@theme') + expect(data.files.snippets['unreferenced-other-collection-snippet.liquid']).to.equal('@other/collection') + }) + it('adds entries for newly referenced components from current collection', async () => { const beforeData = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) expect(beforeData.files.snippets['new.liquid']).to.be.undefined diff --git a/test/fixtures/theme/component.manifest.json b/test/fixtures/theme/component.manifest.json index 6470e32..2fb5e4a 100644 --- a/test/fixtures/theme/component.manifest.json +++ b/test/fixtures/theme/component.manifest.json @@ -16,7 +16,9 @@ "to-be-copied.liquid": "@archetype-themes/test-collection", "to-be-copied-snippet.liquid": "@archetype-themes/test-collection", "existing-subfolder.liquid": "@theme", - "removed-subfolder.liquid": "@theme" + "removed-subfolder.liquid": "@theme", + "unreferenced-theme-snippet.liquid": "@theme", + "unreferenced-other-collection-snippet.liquid": "@other/collection" }, "assets": { "other-collection-component.css": "@other/collection", diff --git a/test/fixtures/theme/snippets/unreferenced-other-collection-snippet.liquid b/test/fixtures/theme/snippets/unreferenced-other-collection-snippet.liquid new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/theme/snippets/unreferenced-theme-snippet.liquid b/test/fixtures/theme/snippets/unreferenced-theme-snippet.liquid new file mode 100644 index 0000000..e69de29