From e7fa97cdd9d95d128f9c5be4ece9be0dd51aa7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 19 Dec 2022 16:55:16 +0000 Subject: [PATCH 1/2] fix: read other import map paths when one doesn't exist --- node/bundler.test.ts | 48 +++++++++++++++++++++++++++++++------------- node/import_map.ts | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/node/bundler.test.ts b/node/bundler.test.ts index f05f447b..413ba89f 100644 --- a/node/bundler.test.ts +++ b/node/bundler.test.ts @@ -346,25 +346,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: join(basePath, 'helper.ts'), + }, + scopes: { + [join(sourceDirectory, 'func3')]: { + helper: join(basePath, 'helper2.ts'), + }, + }, + } + + 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() }) diff --git a/node/import_map.ts b/node/import_map.ts index fd392b10..0f1f7caf 100644 --- a/node/import_map.ts +++ b/node/import_map.ts @@ -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) From 9a7912ba918c2a80ec089875e2ddaa8724398e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 19 Dec 2022 17:20:31 +0000 Subject: [PATCH 2/2] chore: fix test --- node/bundler.test.ts | 7 ++++--- test/util.ts | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/node/bundler.test.ts b/node/bundler.test.ts index 413ba89f..b2baeed8 100644 --- a/node/bundler.test.ts +++ b/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' @@ -352,11 +353,11 @@ test("Ignores entries in `importMapPaths` that don't point to an existing import const importMap = await tmp.file() const importMapContents = { imports: { - helper: join(basePath, 'helper.ts'), + helper: pathToFileURL(join(basePath, 'helper.ts')).toString(), }, scopes: { - [join(sourceDirectory, 'func3')]: { - helper: join(basePath, 'helper2.ts'), + [pathToFileURL(join(sourceDirectory, 'func3')).toString()]: { + helper: pathToFileURL(join(basePath, 'helper2.ts')).toString(), }, }, } diff --git a/test/util.ts b/test/util.ts index e2d76d73..db0b6044 100644 --- a/test/util.ts +++ b/test/util.ts @@ -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, } }