diff --git a/node/import_map.test.ts b/node/import_map.test.ts index b699fbca..92ae3ce3 100644 --- a/node/import_map.test.ts +++ b/node/import_map.test.ts @@ -1,7 +1,9 @@ +import { promises as fs } from 'fs' import { join } from 'path' import { cwd } from 'process' import { pathToFileURL } from 'url' +import tmp from 'tmp-promise' import { test, expect } from 'vitest' import { ImportMap } from './import_map.js' @@ -85,3 +87,27 @@ test('Throws when an import map uses a relative path to reference a file outside `Import map cannot reference '${join(cwd(), 'file.js')}' as it's outside of the base directory '${basePath}'`, ) }) + +test('Writes import map file to disk', async () => { + const file = await tmp.file() + const basePath = join(cwd(), 'my-cool-site', 'import-map.json') + const inputFile1 = { + baseURL: pathToFileURL(basePath), + imports: { + 'alias:pets': './heart/pets/file.ts', + }, + } + + const map = new ImportMap([inputFile1]) + + await map.writeToFile(file.path) + + const createdFile = await fs.readFile(file.path, 'utf8') + const { imports } = JSON.parse(createdFile) + const expectedPath = join(cwd(), 'my-cool-site', 'heart', 'pets', 'file.ts') + + await file.cleanup() + + expect(imports['netlify:edge']).toBe('https://edge.netlify.com/v1/index.ts') + expect(imports['alias:pets']).toBe(pathToFileURL(expectedPath).toString()) +}) diff --git a/node/import_map.ts b/node/import_map.ts index d5e4b310..f5c695aa 100644 --- a/node/import_map.ts +++ b/node/import_map.ts @@ -115,7 +115,7 @@ class ImportMap { await fs.mkdir(distDirectory, { recursive: true }) - const contents = this.getContents(distDirectory) + const contents = this.getContents() await fs.writeFile(path, JSON.stringify(contents)) }