From 448fd806db60753f3115c06acab9e5acbce717dd Mon Sep 17 00:00:00 2001 From: dmcdaniel9 Date: Fri, 1 Aug 2025 12:42:31 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Create=20Blog=20=E2=80=9Ca-neat-way-to-use-?= =?UTF-8?q?dynamic-javascript-imports=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...t-way-to-use-dynamic-javascript-imports.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md diff --git a/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md b/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md new file mode 100644 index 0000000..ab8281f --- /dev/null +++ b/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md @@ -0,0 +1,38 @@ +--- +title: "A neat way to use dynamic JavaScript imports " +excerpt: Conditionals can be used in combination with dynamic imports to allow + more modular code. +publishDate: 2025-08-01T13:33:00.000+01:00 +updatedDate: 2025-08-01T13:33:00.000+01:00 +isFeatured: true +tags: + - javascript + - code-snippet +seo: + image: + src: https://miro.medium.com/v2/resize:fit:1024/1*VqO8QQaI4thLx3F87Nd4Rw.jpeg + alt: Dynamic imports in JavaScript +--- +### Continued learning while job searching + +While I search for new work, I'm making sure to keep up my continued learning by learning new skills through mini-projects and improving on older ones. Today, I have been working on a Node CLI app that I used to convert CSV data into JSON. In my data internship a couple months ago, I did plenty of converting this data into SQL tables, but I find JSON just really easy to work with, so it seemed like a fun idea to make a simple tool for it. + +I'll go into more detail about the project in another post, but today I want to to demonstrate a neat solution to a (probably common) problem. + +### Function dependencies + +I have a function `parseCsvToJson` that uses a utility function called `blankline` to simply log a blank line to the console. I have this utility function in a file called `functions.ts`, containing other functions I may need. This works, but it means that the `parseCsvToJson` function is dependent on `functions.ts`. + +```ts +import { blankLine } from '../functions.js' + +export default function parseCsvToJson(filePath: string) { + // Code... +} +``` + +### Dynamic imports +```ts +const { blankLine } = await import('../functions.js') + +### Conditional Operators From aed2ea41153b3b3fb06d2116689b74b42eb0d365 Mon Sep 17 00:00:00 2001 From: dmcdaniel9 Date: Fri, 1 Aug 2025 13:44:09 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Update=20Blog=20=E2=80=9Ca-neat-way-to-use-?= =?UTF-8?q?dynamic-javascript-imports=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blog/a-neat-way-to-use-dynamic-javascript-imports.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md b/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md index ab8281f..24c190d 100644 --- a/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md +++ b/src/content/blog/a-neat-way-to-use-dynamic-javascript-imports.md @@ -23,16 +23,18 @@ I'll go into more detail about the project in another post, but today I want to I have a function `parseCsvToJson` that uses a utility function called `blankline` to simply log a blank line to the console. I have this utility function in a file called `functions.ts`, containing other functions I may need. This works, but it means that the `parseCsvToJson` function is dependent on `functions.ts`. -```ts +```typescript import { blankLine } from '../functions.js' export default function parseCsvToJson(filePath: string) { // Code... -} +}f ``` ### Dynamic imports -```ts + +```typescript const { blankLine } = await import('../functions.js') ### Conditional Operators +```