Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add guard for import map with extraneous dist directory #243

Merged
merged 1 commit into from
Dec 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions node/bundler.test.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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