Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/commands/theme/component/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* - Maps the components in the theme directory
* - Copies rendered component files (snippets and assets) into the theme directory
* - Cleans up unnecessary component files in the theme directory
* - Generates an import-map.liquid snippet file based on JS assets
* - Generates a head.import-map.liquid snippet file based on JS assets
*/

import Args from '../../../utilities/args.js'
Expand Down
8 changes: 4 additions & 4 deletions src/commands/theme/generate/import-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* - Reads all JS files in the assets directory
* - Creates an import map object with the asset URLs
* - Writes the import map to snippets/import-map.liquid
* - Writes the import map to snippets/head.import-map.liquid
*/

import * as fs from 'node:fs'
Expand Down Expand Up @@ -50,15 +50,15 @@ export default class GenerateImportMap extends BaseCommand {
)
}

// Write the import map to snippets/import-map.liquid
// Write the import map to snippets/head.import-map.liquid
const importMapContent = `<script type="importmap">\n${JSON.stringify(importMap, null, 2)}\n</script>`
const importMapPath = path.join(snippetsDir, 'import-map.liquid')
const importMapPath = path.join(snippetsDir, 'head.import-map.liquid')
if (!fs.existsSync(importMapPath) || fs.readFileSync(importMapPath, 'utf8') !== importMapContent) {
fs.writeFileSync(importMapPath, importMapContent)
}

if (!this.flags[Flags.QUIET]) {
this.log('Successfully generated import map at snippets/import-map.liquid')
this.log('Successfully generated import map at snippets/head.import-map.liquid')
}
}
}
26 changes: 13 additions & 13 deletions test/commands/theme/generate/import-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ describe('theme generate import-map', () => {

it('generates empty import map when no JS files exist', async () => {
await runCommand(['theme:generate:import-map', testThemePath])
const importMapPath = path.join(testThemePath, 'snippets', 'import-map.liquid')

const importMapPath = path.join(testThemePath, 'snippets', 'head.import-map.liquid')
const content = fs.readFileSync(importMapPath, 'utf8')

const jsonContent = extractImportMapJson(content)
expect(jsonContent).to.deep.equal({
imports: {}
Expand All @@ -36,12 +36,12 @@ describe('theme generate import-map', () => {
const assetsPath = path.join(testThemePath, 'assets')
fs.writeFileSync(path.join(assetsPath, 'main.js'), '')
fs.writeFileSync(path.join(assetsPath, 'utils.js'), '')

await runCommand(['theme:generate:import-map', testThemePath])
const importMapPath = path.join(testThemePath, 'snippets', 'import-map.liquid')

const importMapPath = path.join(testThemePath, 'snippets', 'head.import-map.liquid')
const content = fs.readFileSync(importMapPath, 'utf8')

const jsonContent = extractImportMapJson(content)
expect(jsonContent).to.deep.equal({
imports: {
Expand All @@ -54,12 +54,12 @@ describe('theme generate import-map', () => {
it('does not include .min in the entry key', async () => {
const assetsPath = path.join(testThemePath, 'assets')
fs.writeFileSync(path.join(assetsPath, 'component.min.js'), '')

await runCommand(['theme:generate:import-map', testThemePath])
const importMapPath = path.join(testThemePath, 'snippets', 'import-map.liquid')

const importMapPath = path.join(testThemePath, 'snippets', 'head.import-map.liquid')
const content = fs.readFileSync(importMapPath, 'utf8')

const jsonContent = extractImportMapJson(content)
expect(jsonContent).to.deep.equal({
imports: {
Expand Down Expand Up @@ -90,12 +90,12 @@ describe('theme generate import-map', () => {
it('updates existing import map', async () => {
const assetsPath = path.join(testThemePath, 'assets')
fs.writeFileSync(path.join(assetsPath, 'main.js'), '')
const importMapPath = path.join(testThemePath, 'snippets', 'import-map.liquid')
const importMapPath = path.join(testThemePath, 'snippets', 'head.import-map.liquid')
const initialContent = `<script type="importmap">\n{\n "imports": {}\n}\n</script>`
fs.writeFileSync(importMapPath, initialContent)

await runCommand(['theme:generate:import-map', testThemePath])

const content = fs.readFileSync(importMapPath, 'utf8')
const jsonContent = extractImportMapJson(content)
expect(jsonContent).to.deep.equal({
Expand Down