Skip to content

Commit

Permalink
Merge pull request #205 from ryoppippi/feature/dynamic-import
Browse files Browse the repository at this point in the history
feat(utils.ts): add dynamicImport utility function (fixes: fixes: #198)
  • Loading branch information
ryoppippi authored Jul 14, 2024
2 parents 0647231 + dd46217 commit 3177a4d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/unplugin-typia/src/core/languages/svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Data, ID, Source } from '../types.js';
import { wrap } from '../types.js';
import { dynamicImport } from '../utils.js';

/**
* Check if a file is a Svelte file.
Expand All @@ -23,7 +24,7 @@ export async function preprocess(
{ source, id, transform }:
{ source: Source; id: ID; transform: TransformFunction },
): Promise<{ code: Data }> {
const { preprocess: sveltePreprocess } = await import('svelte/compiler');
const { preprocess: sveltePreprocess } = await dynamicImport('svelte/compiler') as typeof import('svelte/compiler');
const { code } = await sveltePreprocess(source, {
script: async ({ content, filename, attributes }) => {
if (filename == null) {
Expand Down
8 changes: 8 additions & 0 deletions packages/unplugin-typia/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export function log(
export function isBun() {
return globalThis.Bun != null;
}

/**
* Dynamic import a module.
* Because JSR fails to resolve dynamic import, we have to use this workaround. (see: https://github.com/ryoppippi/unplugin-typia/issues/198)
*/
export async function dynamicImport<T extends string>(specifier: T): Promise<unknown> {
return import(specifier);
}

0 comments on commit 3177a4d

Please sign in to comment.