Skip to content

Commit

Permalink
fix: respect import map files containing only scopes (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Oct 16, 2023
1 parent d3d09ca commit 6812209
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
27 changes: 27 additions & 0 deletions node/import_map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import tmp from 'tmp-promise'
import { describe, test, expect } from 'vitest'

import { ImportMap } from './import_map.js'
import { getLogger } from './logger.js'

test('Handles import maps with full URLs without specifying a base URL', () => {
const basePath = join(cwd(), 'my-cool-site', 'import-map.json')
Expand Down Expand Up @@ -176,6 +177,32 @@ test('Writes import map file to disk', async () => {
expect(imports['alias:pets']).toBe(pathToFileURL(expectedPath).toString())
})

test('Respects import map when it has only scoped key', async () => {
const file = await tmp.file()
const importMap = {
scopes: {
'./foo': {
'alias:pets': './heart/pets/file.ts',
},
},
}
await fs.writeFile(file.path, JSON.stringify(importMap))
const map = new ImportMap()
await map.addFile(file.path, getLogger())

expect(map.getContents()).toEqual({
imports: {
'netlify:edge': 'https://edge.netlify.com/v1/index.ts?v=legacy',
'@netlify/edge-functions': 'https://edge.netlify.com/v1/index.ts',
},
scopes: {
[pathToFileURL(join(file.path, '../foo')).href]: {
'alias:pets': pathToFileURL(join(file.path, '../heart/pets/file.ts')).href,
},
},
})
})

test('Clones an import map', () => {
const basePath = join(cwd(), 'my-cool-site', 'import-map.json')
const inputFile1 = {
Expand Down
6 changes: 1 addition & 5 deletions node/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Imports = Record<string, string>

export interface ImportMapFile {
baseURL: URL
imports: Imports
imports?: Imports
scopes?: Record<string, Imports>
}

Expand Down Expand Up @@ -47,10 +47,6 @@ export class ImportMap {
async addFile(path: string, logger: Logger) {
const source = await ImportMap.readFile(path, logger)

if (Object.keys(source.imports).length === 0) {
return
}

return this.add(source)
}

Expand Down

0 comments on commit 6812209

Please sign in to comment.