Skip to content

Commit

Permalink
fix: add guard for import map with extraneous dist directory (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Dec 7, 2022
1 parent 258fc9e commit f0ac6d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
38 changes: 38 additions & 0 deletions node/bundler.test.ts
@@ -1,6 +1,7 @@
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import process from 'process'
import { pathToFileURL } from 'url'

import { deleteAsync } from 'del'
import tmp from 'tmp-promise'
Expand Down Expand Up @@ -345,3 +346,40 @@ test('Loads declarations and import maps from the deploy configuration', async (

await cleanup()
})

test('Uses an absolute URL for the import map when the dist directory is not a child of the base path', async () => {
const { basePath, cleanup } = await useFixture('with_import_maps')
const { path: distPath } = await tmp.dir()
const declarations = [
{
function: 'func1',
path: '/func1',
},
]
const sourceDirectory = join(basePath, 'functions')
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
})
const generatedFiles = await fs.readdir(distPath)

expect(result.functions.length).toBe(1)

// ESZIP, manifest and import map.
expect(generatedFiles.length).toBe(3)

const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
expect(() => validateManifest(manifest)).not.toThrowError()
const { bundles, import_map: importMapURL } = manifest

expect(bundles.length).toBe(1)
expect(bundles[0].format).toBe('eszip2')
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)

const importMapPath = join(distPath, 'import_map.json')
expect(importMapURL).toBe(pathToFileURL(importMapPath).toString())

await cleanup()
await fs.rm(distPath, { recursive: true })
})
14 changes: 12 additions & 2 deletions node/formats/eszip.ts
Expand Up @@ -69,8 +69,18 @@ const createUserImportMap = async (importMap: ImportMap, basePath: string, distD

await importMap.writeToFile(destPath)

const relativePath = relative(basePath, destPath)
const importMapURL = new URL(relativePath, virtualRoot)
let virtualPath = relative(basePath, destPath)

// If the dist directory is not a child of the base path, we can't represent
// the relative path as a file URL (because something like 'file://../foo' is
// not valid). This should never happen, but it's best to leave the absolute
// path untransformed to avoid getting a build error due to a missing import
// map.
if (virtualPath.startsWith('..')) {
virtualPath = destPath
}

const importMapURL = new URL(virtualPath, virtualRoot)

return importMapURL.toString()
}
Expand Down

0 comments on commit f0ac6d0

Please sign in to comment.