Skip to content

Commit

Permalink
Refactor dynamic import transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
decs committed Mar 2, 2024
1 parent 95f9616 commit a269573
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-sort-keys": "^2.3.5",
"expect-type": "^0.17.3",
"magic-regexp": "^0.7.0",
"npm-check-updates": "^16.14.12",
"pnpm-sync-dependencies-meta-injected": "^0.0.10",
"prettier": "^3.2.4",
Expand Down
14 changes: 3 additions & 11 deletions packages/effect/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@ import type {ValidationAdapter} from '@typeschema/core';

import {memoize} from '@typeschema/core';

const importValidationEitherModule = memoize(async () => {
const importValidationModule = memoize(async () => {
const {isRight} = await import('effect/Either');
return {isRight};
});
const importValidationSchemaModule = memoize(async () => {
const {parseEither} = await import('@effect/schema/Schema');
return {parseEither};
});
const importValidationTreeFormatterModule = memoize(async () => {
const {formatError} = await import('@effect/schema/TreeFormatter');
return {formatError};
return {formatError, isRight, parseEither};
});

export const validationAdapter: ValidationAdapter<
AdapterResolver
> = async schema => {
const {isRight} = await importValidationEitherModule();
const {parseEither} = await importValidationSchemaModule();
const {formatError} = await importValidationTreeFormatterModule();
const {formatError, isRight, parseEither} = await importValidationModule();
const parseSchema = parseEither(schema);
return async data => {
const result = parseSchema(data);
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 47 additions & 12 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,56 @@ import type {Plugin} from 'esbuild';

import fs from 'node:fs';

import {
anyOf,
char,
createRegExp,
exactly,
global,
maybe,
multiline,
oneOrMore,
whitespace,
wordChar,
} from 'magic-regexp';
import {defineConfig} from 'tsup';

const dynamicImportsPlugin: Plugin = {
name: 'dynamicImports',
const regex = createRegExp(
maybe(oneOrMore(whitespace)).groupedAs('indentation'),
anyOf('const', 'let', 'var').groupedAs('declarationType'),
oneOrMore(whitespace),
oneOrMore(wordChar).or('{', oneOrMore(char), '}').groupedAs('variableName'),
oneOrMore(whitespace),
'=',
oneOrMore(whitespace),
maybe('await', oneOrMore(whitespace)).groupedAs('await'),
'import(',
maybe(oneOrMore(whitespace)),
exactly("'", oneOrMore(char), "'")
.or('"', oneOrMore(char), '"')
.groupedAs('modulePath'),
maybe(oneOrMore(whitespace)),
')',
maybe(';').groupedAs('terminator'),
[global, multiline],
);
const template = `
try {
var dynamicallyImportedModule = $<await>import(
/* webpackIgnore: true */
$<modulePath>
)$<terminator>
} catch (moduleImportError) {
throw moduleImportError$<terminator>
}
$<declarationType> $<variableName> = dynamicallyImportedModule$<terminator>
`.trim();

const optionalPeerDependenciesPlugin: Plugin = {
name: 'optionalPeerDependencies',
setup(build) {
build.onLoad({filter: /\.ts$/}, async args => ({
contents: fs.readFileSync(args.path, 'utf-8').replace(
/([ \t]*)const (\w+|{.+}) = await import\((.*)\);\s*return ([^;]+);/g,
`$1try {
$1 const $2 = await import(/* webpackIgnore: true */ $3);
$1 return $4;
$1} catch (error) {
$1 throw error;
$1}`,
),
contents: fs.readFileSync(args.path, 'utf-8').replace(regex, template),
loader: 'ts',
}));
},
Expand All @@ -26,6 +61,6 @@ export default defineConfig({
clean: true,
dts: true,
entry: ['src/index.ts'],
esbuildPlugins: [dynamicImportsPlugin],
esbuildPlugins: [optionalPeerDependenciesPlugin],
format: ['esm', 'cjs'],
});

0 comments on commit a269573

Please sign in to comment.