Skip to content

Commit

Permalink
fix: read other import map paths when one doesn't exist (#268)
Browse files Browse the repository at this point in the history
* fix: read other import map paths when one doesn't exist

* chore: fix test
  • Loading branch information
eduardoboucas committed Dec 19, 2022
1 parent e32d186 commit 8410c1c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
49 changes: 35 additions & 14 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 @@ -346,25 +347,45 @@ test('Loads declarations and import maps from the deploy configuration', async (
test("Ignores entries in `importMapPaths` that don't point to an existing import map file", async () => {
const systemLogger = vi.fn()
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const sourceDirectory = join(basePath, 'functions')
const importMapPath = join(distPath, 'some-file-that-does-not-exist.json')
const declarations = [
const sourceDirectory = join(basePath, 'user-functions')

// Creating import map file
const importMap = await tmp.file()
const importMapContents = {
imports: {
helper: pathToFileURL(join(basePath, 'helper.ts')).toString(),
},
scopes: {
[pathToFileURL(join(sourceDirectory, 'func3')).toString()]: {
helper: pathToFileURL(join(basePath, 'helper2.ts')).toString(),
},
},
}

await fs.writeFile(importMap.path, JSON.stringify(importMapContents))

const nonExistingImportMapPath = join(distPath, 'some-file-that-does-not-exist.json')
const result = await bundle(
[sourceDirectory],
distPath,
[
{
function: 'func1',
path: '/func1',
},
],
{
function: 'func1',
path: '/func1',
basePath,
importMapPaths: [nonExistingImportMapPath, importMap.path],
systemLogger,
},
]
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
importMapPaths: [importMapPath],
systemLogger,
})
)
const generatedFiles = await fs.readdir(distPath)

expect(result.functions.length).toBe(1)
expect(result.functions.length).toBe(2)
expect(generatedFiles.length).toBe(2)
expect(systemLogger).toHaveBeenCalledWith(`Did not find an import map file at '${importMapPath}'.`)
expect(systemLogger).toHaveBeenCalledWith(`Did not find an import map file at '${nonExistingImportMapPath}'.`)

await cleanup()
await importMap.cleanup()
})
2 changes: 1 addition & 1 deletion node/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ImportMap {
async addFiles(paths: (string | undefined)[], logger: Logger) {
for (const path of paths) {
if (path === undefined) {
return
continue
}

await this.addFile(path, logger)
Expand Down
5 changes: 2 additions & 3 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ const dirname = fileURLToPath(url)
const fixturesDir = resolve(dirname, '..', 'fixtures')

const useFixture = async (fixtureName: string) => {
const tmpDir = await tmp.dir()
const cleanup = () => fs.rmdir(tmpDir.path, { recursive: true })
const tmpDir = await tmp.dir({ unsafeCleanup: true })
const fixtureDir = resolve(fixturesDir, fixtureName)
const distPath = join(tmpDir.path, '.netlify', 'edge-functions-dist')

return {
basePath: fixtureDir,
cleanup,
cleanup: tmpDir.cleanup,
distPath,
}
}
Expand Down

0 comments on commit 8410c1c

Please sign in to comment.