@gjsify/esbuild-plugin-deepkit 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @gjsify/esbuild-plugin-deepkit
2
+
3
+ A [esbuild](https://esbuild.github.io/) plugin for the the compiler/transformer of [@deepkit/type](https://deepkit.io/library/type).
4
+
5
+ ## Example
6
+
7
+ src/index.ts:
8
+ ```ts
9
+ import { deserialize } from '@deepkit/type';
10
+
11
+ interface Config {
12
+ color: string;
13
+ }
14
+
15
+ interface User {
16
+ id: number;
17
+ createdAt: Date;
18
+ firstName?: string;
19
+ lastName?: string;
20
+ config: Config;
21
+ username: string;
22
+ }
23
+
24
+ //deserialize JSON object to real instances
25
+ const user = deserialize<User>({
26
+ id: 0,
27
+ username: 'peter',
28
+ createdAt: '2021-06-26T12:34:41.061Z',
29
+ config: {color: '#221122'},
30
+ });
31
+
32
+
33
+ console.log(user.createdAt instanceof Date); // true
34
+ ```
35
+
36
+ esbuild.mjs:
37
+ ```js
38
+ import { build } from 'esbuild';
39
+ import { deepkitPlugin } from '@gjsify/esbuild-plugin-deepkit';
40
+
41
+ build({
42
+ entryPoints: ['src/index.ts'],
43
+ bundle: true,
44
+ minify: true,
45
+ outfile: 'dist/index.js',
46
+ format: 'esm',
47
+ platform: "node",
48
+ external: ['@deepkit/type'],
49
+ plugins: [deepkitPlugin()],
50
+ });
51
+ ```
52
+ Start:
53
+ ```bash
54
+ node dist/index.js
55
+ -> true
56
+ ```
@@ -0,0 +1 @@
1
+ var g=Object.create;var i=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var n in e)i(t,n,{get:e[n],enumerable:!0})},a=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of O(e))!y.call(t,o)&&o!==n&&i(t,o,{get:()=>e[o],enumerable:!(r=D(e,o))||r.enumerable});return t};var k=(t,e,n)=>(n=t!=null?g(L(t)):{},a(e||!t||!t.__esModule?i(n,"default",{value:t,enumerable:!0}):n,t)),P=t=>a(i({},"__esModule",{value:!0}),t);var T={};h(T,{deepkitPlugin:()=>m,default:()=>A,transformExtern:()=>R});module.exports=P(T);var l=require("fs/promises"),c=k(require("@deepkit/type-compiler")),s=require("util");var p=t=>new TextDecoder().decode(t);var f=new c.DeepkitLoader,d=(...t)=>{console.log("printDiagnostics",(0,s.inspect)(t,!1,10,!0))},u={filter:/\.(m|c)?tsx?$/,namespace:"file"},R=(t,e,n)=>{if(!t.reflection||!n.contents||!u.filter.test(e.path)||e.pluginData?.isReflected)return n;let r=e.namespace+":"+e.path;try{let o=typeof n.contents=="string"?n.contents:p(n.contents);n.contents=f.transform(o,r),n.pluginData=n.pluginData||{},n.pluginData.isReflected=!0}catch(o){d({file:r,error:o})}return n},x=async t=>{let e;try{if(e=await(0,l.readFile)(t.path,"utf8"),t.pluginData?.isReflected)return{contents:e,loader:"ts",pluginData:{isReflected:!0}};e=f.transform(e,t.path)}catch(n){return d({file:t.path,error:n}),null}return{contents:e,loader:"ts",pluginData:{isReflected:!0}}},m=(t={})=>({name:"deepkit",setup(e){t.reflection=t.reflection===void 0?!0:t.reflection,t.reflection&&e.onLoad(u,x)}}),A=m;0&&(module.exports={deepkitPlugin,transformExtern});
@@ -0,0 +1 @@
1
+ import{readFile as s}from"fs/promises";import*as a from"@deepkit/type-compiler";import{inspect as f}from"util";var i=t=>new TextDecoder().decode(t);var p=new a.DeepkitLoader,l=(...t)=>{console.log("printDiagnostics",f(t,!1,10,!0))},c={filter:/\.(m|c)?tsx?$/,namespace:"file"},L=(t,e,n)=>{if(!t.reflection||!n.contents||!c.filter.test(e.path)||e.pluginData?.isReflected)return n;let r=e.namespace+":"+e.path;try{let o=typeof n.contents=="string"?n.contents:i(n.contents);n.contents=p.transform(o,r),n.pluginData=n.pluginData||{},n.pluginData.isReflected=!0}catch(o){l({file:r,error:o})}return n},d=async t=>{let e;try{if(e=await s(t.path,"utf8"),t.pluginData?.isReflected)return{contents:e,loader:"ts",pluginData:{isReflected:!0}};e=p.transform(e,t.path)}catch(n){return l({file:t.path,error:n}),null}return{contents:e,loader:"ts",pluginData:{isReflected:!0}}},u=(t={})=>({name:"deepkit",setup(e){t.reflection=t.reflection===void 0?!0:t.reflection,t.reflection&&e.onLoad(c,d)}}),y=u;export{u as deepkitPlugin,y as default,L as transformExtern};
@@ -0,0 +1,16 @@
1
+ import type { Plugin, OnLoadArgs, OnLoadResult } from 'esbuild';
2
+ export interface DeepkitPluginOptions {
3
+ reflection?: boolean;
4
+ }
5
+ /**
6
+ * Use this method if you want to transform your typescript in any other plugin / onLoad callback method
7
+ * @param options +
8
+ * @param args
9
+ * @param result
10
+ * @returns
11
+ */
12
+ export declare const transformExtern: (options: DeepkitPluginOptions, args: Partial<OnLoadArgs> & {
13
+ path: string;
14
+ }, result: OnLoadResult) => OnLoadResult | null;
15
+ export declare const deepkitPlugin: (options?: DeepkitPluginOptions) => Plugin;
16
+ export default deepkitPlugin;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Utf8Array to string
3
+ * @param array Utf-8 Array
4
+ * @returns The converted string
5
+ * @credits https://stackoverflow.com/a/41798356/1465919
6
+ * @credits https://stackoverflow.com/a/36949791/1465919
7
+ */
8
+ export declare const Utf8ArrayToStr: (array: Uint8Array) => string;
package/esbuild.mjs ADDED
@@ -0,0 +1,46 @@
1
+ import { build } from 'esbuild';
2
+ import { readFile } from 'fs/promises';
3
+
4
+ const pkg = JSON.parse(
5
+ await readFile(
6
+ new URL('./package.json', import.meta.url)
7
+ )
8
+ );
9
+
10
+ const baseConfig = {
11
+ entryPoints: ['src/index.ts'],
12
+ bundle: true,
13
+ minify: true,
14
+ external: [
15
+ 'fs',
16
+ 'fs/promises',
17
+ 'util',
18
+ 'path',
19
+ 'process',
20
+ 'util',
21
+ 'typescript',
22
+ '@deepkit/type-compiler',
23
+ 'esbuild',
24
+ // '@gjsify/resolve-npm', can't be required in cjs builds
25
+ '@gjsify/esbuild-plugin-transform-ext',
26
+ '@gjsify/esbuild-plugin-deno-loader',
27
+ '@gjsify/esbuild-plugin-gjsify',
28
+ ]
29
+ }
30
+
31
+ if(pkg.main) {
32
+ build({
33
+ ...baseConfig,
34
+ outfile: pkg.main,
35
+ format: 'cjs',
36
+ platform: "node",
37
+ });
38
+ }
39
+
40
+ if(pkg.main) {
41
+ build({
42
+ ...baseConfig,
43
+ outfile: pkg.module,
44
+ format: 'esm',
45
+ });
46
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@gjsify/esbuild-plugin-deepkit",
3
+ "version": "0.0.2",
4
+ "description": "Deepkit type compiler plugin for esbuild",
5
+ "type": "commonjs",
6
+ "main": "dist/cjs/index.cjs",
7
+ "module": "dist/esm/index.mjs",
8
+ "types": "dist/types/index.d.ts",
9
+ "scripts": {
10
+ "clear": "rm -rf dist tsconfig.tsbuildinfo",
11
+ "print:name": "echo '@gjsify/esbuild-plugin-deepkit'",
12
+ "build": "yarn print:name && yarn build:js && yarn build:types",
13
+ "build:js": "node esbuild.mjs",
14
+ "build:types": "yarn tsc --emitDeclarationOnly"
15
+ },
16
+ "keywords": [
17
+ "gjs",
18
+ "node",
19
+ "fs"
20
+ ],
21
+ "dependencies": {
22
+ "@deepkit/type-compiler": "^1.0.1-alpha.97"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^20.3.1",
26
+ "esbuild": "^0.18.2",
27
+ "typescript": "^5.1.3"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { readFile } from 'fs/promises';
2
+ import * as DkType from '@deepkit/type-compiler';
3
+ import { inspect } from 'util';
4
+ import { Utf8ArrayToStr } from './utils.js';
5
+
6
+ import type { Plugin, OnLoadArgs, OnLoadResult, OnLoadOptions } from 'esbuild';
7
+
8
+ const loader = new DkType.DeepkitLoader();
9
+
10
+ export interface DeepkitPluginOptions {
11
+ reflection?: boolean;
12
+ }
13
+
14
+ const printDiagnostics = (...args: any[]) => {
15
+ console.log("printDiagnostics", inspect(args, false, 10, true));
16
+ }
17
+
18
+ const deepkitPluginOnLoadOptions: OnLoadOptions = { filter: /\.(m|c)?tsx?$/, namespace: 'file'};
19
+
20
+ /**
21
+ * Use this method if you want to transform your typescript in any other plugin / onLoad callback method
22
+ * @param options +
23
+ * @param args
24
+ * @param result
25
+ * @returns
26
+ */
27
+ export const transformExtern = (options: DeepkitPluginOptions, args: Partial<OnLoadArgs> & { path: string; }, result: OnLoadResult): OnLoadResult | null => {
28
+ if(!options.reflection || !result.contents || !deepkitPluginOnLoadOptions.filter.test(args.path) || args.pluginData?.isReflected) {
29
+ return result;
30
+ }
31
+
32
+ const path = args.namespace + ':' + args.path;
33
+
34
+ try {
35
+ const contentStr = typeof result.contents === 'string' ? result.contents : Utf8ArrayToStr(result.contents);
36
+ result.contents = loader.transform(contentStr, path);
37
+ result.pluginData = result.pluginData || {};
38
+ result.pluginData.isReflected = true;
39
+ } catch (error) {
40
+ printDiagnostics({ file: path, error });
41
+ }
42
+
43
+ return result;
44
+ }
45
+
46
+ const onLoad = async (args: OnLoadArgs): Promise<OnLoadResult | null | undefined> => {
47
+ let contents: string;
48
+
49
+ // console.debug("[deepkit] onLoad", args.path);
50
+
51
+ try {
52
+ contents = await readFile(args.path, 'utf8');
53
+ // If already reflected do nothing
54
+ if(args.pluginData?.isReflected) {
55
+ return { contents, loader: 'ts', pluginData: { isReflected: true } };
56
+ }
57
+
58
+ contents = loader.transform(contents, args.path);
59
+ } catch (error) {
60
+ printDiagnostics({ file: args.path, error });
61
+ return null;
62
+ }
63
+
64
+ return { contents, loader: 'ts', pluginData: { isReflected: true } };
65
+ }
66
+
67
+ export const deepkitPlugin = (options: DeepkitPluginOptions = {}): Plugin => {
68
+ return {
69
+ name: 'deepkit',
70
+ setup(build) {
71
+ options.reflection = options.reflection === undefined ? true : options.reflection;
72
+
73
+ if (!options.reflection) {
74
+ return;
75
+ }
76
+
77
+ build.onLoad(deepkitPluginOnLoadOptions, onLoad);
78
+
79
+ }
80
+ }
81
+ }
82
+
83
+ export default deepkitPlugin;
package/src/utils.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Utf8Array to string
3
+ * @param array Utf-8 Array
4
+ * @returns The converted string
5
+ * @credits https://stackoverflow.com/a/41798356/1465919
6
+ * @credits https://stackoverflow.com/a/36949791/1465919
7
+ */
8
+ export const Utf8ArrayToStr = (array: Uint8Array) => {
9
+ return new TextDecoder().decode(array);
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "dist",
4
+ "declarationDir": "dist/types",
5
+ "declaration": true,
6
+ "target": "ESNext",
7
+ "module": "ESNext",
8
+ "types": ["node"],
9
+ "moduleResolution": "bundler"
10
+ },
11
+ "files": ["src/index.ts"]
12
+ }