From 40eadc9642bb447c64edcac14d38548f9c15316d Mon Sep 17 00:00:00 2001 From: Peter van Vliet <108156553+petermasking@users.noreply.github.com> Date: Tue, 13 Feb 2024 20:19:45 +0100 Subject: [PATCH] fix(chaching): explicit import result requests (#484) --- .../src/building/utils/ImportRewriter.ts | 3 ++- .../test/_fixtures/CacheFiles.fixture.ts | 16 ++++++------ .../building/utils/ImportRewriter.fixture.ts | 26 +++++++++---------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/caching/src/building/utils/ImportRewriter.ts b/packages/caching/src/building/utils/ImportRewriter.ts index 322ba8be..b4af95d2 100644 --- a/packages/caching/src/building/utils/ImportRewriter.ts +++ b/packages/caching/src/building/utils/ImportRewriter.ts @@ -51,8 +51,9 @@ export default class ImportRewriter } const members = this.#rewriteImportMembers(dependency); + const extractDefault = this.#mustUseAs(dependency) ? 'true' : 'false'; - return `const ${members} = await __import("${from}", "${scope}");`; + return `const ${members} = await __import("${from}", "${scope}", ${extractDefault});`; } #rewriteImportFrom(dependency: ReflectionImport, filename: string): string diff --git a/packages/caching/test/_fixtures/CacheFiles.fixture.ts b/packages/caching/test/_fixtures/CacheFiles.fixture.ts index 75ad216e..6598edc4 100644 --- a/packages/caching/test/_fixtures/CacheFiles.fixture.ts +++ b/packages/caching/test/_fixtures/CacheFiles.fixture.ts @@ -38,7 +38,7 @@ export const segment = new Segment("product") \t)`; const CREATE_ORDER_LOCAL = -`const { Order, OrderLine } = await __import("./order/models.js", "application"); +`const { Order, OrderLine } = await __import("./order/models.js", "application", false); export default async function createOrder(items) { @@ -56,9 +56,9 @@ const CREATE_ORDER_REMOTE = }`; const STORE_ORDER_LOCAL = -`const mysql = await __import("mysql", "runtime"); -const createId = await __import("uuid", "runtime"); -const { Order } = await __import("./order/models.js", "application"); +`const mysql = await __import("mysql", "runtime", true); +const createId = await __import("uuid", "runtime", true); +const { Order } = await __import("./order/models.js", "application", false); export async function v0_0_0(order) { @@ -87,8 +87,8 @@ Order.source = "./order/models.js"; OrderLine.source = "./order/models.js";`; const GET_PRODUCTS_LOCAL = -`const mongodb = await __import("mongodb", "runtime"); -const { Product } = await __import("./product/models.js", "application"); +`const mongodb = await __import("mongodb", "runtime", true); +const { Product } = await __import("./product/models.js", "application", false); export default async function getProducts(id) { @@ -115,8 +115,8 @@ export async function searchProducts({ query , sort }) { }`; const GET_PRODUCTS_LOCAL_V1 = -`const mongodb = await __import("mongodb", "runtime"); -const { Product } = await __import("./product/models.js", "application"); +`const mongodb = await __import("mongodb", "runtime", true); +const { Product } = await __import("./product/models.js", "application", false); export default async function getProducts(id) { diff --git a/packages/caching/test/_fixtures/building/utils/ImportRewriter.fixture.ts b/packages/caching/test/_fixtures/building/utils/ImportRewriter.fixture.ts index ceb6433f..e298c729 100644 --- a/packages/caching/test/_fixtures/building/utils/ImportRewriter.fixture.ts +++ b/packages/caching/test/_fixtures/building/utils/ImportRewriter.fixture.ts @@ -8,8 +8,8 @@ import { component1, component2 } from '../path/to/components.js'; const APPLICATION_IMPORTS_RESULT = `await __import("./components/path/to/file.js", "application"); -const component = await __import("./components/path/to/component.js", "application"); -const { component1, component2 } = await __import("./path/to/components.js", "application"); +const component = await __import("./components/path/to/component.js", "application", true); +const { component1, component2 } = await __import("./path/to/components.js", "application", false); `; const RUNTIME_IMPORTS = @@ -21,14 +21,14 @@ import { component1, component2 } from 'https'; const RUNTIME_IMPORTS_RESULT = `await __import("polyfills", "runtime"); -const fs = await __import("fs", "runtime"); -const component = await __import("http", "runtime"); -const { component1, component2 } = await __import("https", "runtime"); +const fs = await __import("fs", "runtime", true); +const component = await __import("http", "runtime", true); +const { component1, component2 } = await __import("https", "runtime", false); `; const IMPORT_WITHOUT_SEMICOLON = `import { runProcedure } from 'jitar'`; -const IMPORT_WITHOUT_SEMICOLON_RESULT = `const { runProcedure } = await __import("jitar", "runtime");`; +const IMPORT_WITHOUT_SEMICOLON_RESULT = `const { runProcedure } = await __import("jitar", "runtime", false);`; // Without .js extension const MIXED_IMPORTS = @@ -39,10 +39,10 @@ import main, { some as other } from 'library'; `; const MIXED_IMPORTS_RESULT = -`const component = await __import("./components/path/to/component.js", "application"); -const os = await __import("os", "runtime"); -const { runProcedure } = await __import("jitar", "runtime"); -const { default : main, some : other } = await __import("library", "runtime"); +`const component = await __import("./components/path/to/component.js", "application", true); +const os = await __import("os", "runtime", true); +const { runProcedure } = await __import("jitar", "runtime", false); +const { default : main, some : other } = await __import("library", "runtime", false); `; const DYNAMIC_IMPORTS = @@ -51,7 +51,7 @@ const os = await import('os'); `; const DYNAMIC_IMPORTS_RESULT = -`const component = await __import("./components/path/to/component.js", "application"); +`const component = await __import("./components/path/to/component.js", "application", true); const os = await import('os'); `; @@ -64,11 +64,11 @@ import { runProcedure } from 'jitar'; `; const IMPORTS_AND_CONTENT_RESULT = -`const os = await __import("os", "runtime"); +`const os = await __import("os", "runtime", true); export default function test() {} -const { runProcedure } = await __import("jitar", "runtime"); +const { runProcedure } = await __import("jitar", "runtime", false); `; const INPUTS =