Skip to content

Commit

Permalink
fix: do not transform paths when writing import map to disk (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Dec 12, 2022
1 parent f90fc35 commit 277dc30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions 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'
Expand Down Expand Up @@ -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())
})
2 changes: 1 addition & 1 deletion node/import_map.ts
Expand Up @@ -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))
}
Expand Down

0 comments on commit 277dc30

Please sign in to comment.