@gjsify/esbuild-plugin-gjsify 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.
Files changed (44) hide show
  1. package/dist/cjs/index.cjs +11752 -0
  2. package/dist/esm/index.mjs +11726 -0
  3. package/dist/types/alias-plugin.d.ts +2 -0
  4. package/dist/types/app/browser.d.ts +3 -0
  5. package/dist/types/app/deno.d.ts +3 -0
  6. package/dist/types/app/gjs.d.ts +3 -0
  7. package/dist/types/app/index.d.ts +4 -0
  8. package/dist/types/app/node.d.ts +3 -0
  9. package/dist/types/debug-plugin.d.ts +2 -0
  10. package/dist/types/index.d.ts +7 -0
  11. package/dist/types/lib/cjs.d.ts +3 -0
  12. package/dist/types/lib/esm.d.ts +3 -0
  13. package/dist/types/lib/index.d.ts +2 -0
  14. package/dist/types/plugin.d.ts +4 -0
  15. package/dist/types/types/app.d.ts +1 -0
  16. package/dist/types/types/index.d.ts +3 -0
  17. package/dist/types/types/plugin-options.d.ts +17 -0
  18. package/dist/types/types/resolve-alias-options.d.ts +2 -0
  19. package/dist/types/utils/alias.d.ts +15 -0
  20. package/dist/types/utils/entry-points.d.ts +5 -0
  21. package/dist/types/utils/extension.d.ts +8 -0
  22. package/dist/types/utils/index.d.ts +3 -0
  23. package/esbuild.mjs +49 -0
  24. package/package.json +46 -0
  25. package/src/alias-plugin.ts +68 -0
  26. package/src/app/browser.ts +60 -0
  27. package/src/app/deno.ts +60 -0
  28. package/src/app/gjs.ts +66 -0
  29. package/src/app/index.ts +4 -0
  30. package/src/app/node.ts +63 -0
  31. package/src/index.ts +7 -0
  32. package/src/lib/cjs.ts +46 -0
  33. package/src/lib/esm.ts +44 -0
  34. package/src/lib/index.ts +2 -0
  35. package/src/plugin.ts +43 -0
  36. package/src/types/app.ts +1 -0
  37. package/src/types/index.ts +3 -0
  38. package/src/types/plugin-options.ts +18 -0
  39. package/src/types/resolve-alias-options.ts +1 -0
  40. package/src/utils/alias.ts +59 -0
  41. package/src/utils/entry-points.ts +33 -0
  42. package/src/utils/extension.ts +7 -0
  43. package/src/utils/index.ts +3 -0
  44. package/tsconfig.json +13 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { Plugin } from "esbuild";
2
+ import type { PluginOptions } from './types/plugin-options.js';
3
+ import { setupCjsLib, setupEsmLib } from './lib/index.js';
4
+ import { setupForGjs, setupForNode, setupForDeno, setupForBrowser } from './app/index.js';
5
+
6
+ export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
7
+ const plugin: Plugin = {
8
+ name: 'gjsify',
9
+ async setup(build) {
10
+
11
+ // Library mode
12
+ if(pluginOptions.library) {
13
+ switch (pluginOptions.library) {
14
+ case 'esm':
15
+ return setupEsmLib(build, pluginOptions)
16
+ case 'cjs':
17
+ return setupCjsLib(build, pluginOptions)
18
+ default:
19
+ throw new TypeError('Unknown library type: ' + pluginOptions.library);
20
+ }
21
+ }
22
+
23
+ pluginOptions.app ||= 'gjs';
24
+
25
+ // End user applications or tests
26
+ switch (pluginOptions.app) {
27
+ case 'gjs':
28
+ return await setupForGjs(build, pluginOptions);
29
+ case 'node':
30
+ return await setupForNode(build, pluginOptions);
31
+ case 'deno':
32
+ return await setupForDeno(build, pluginOptions);
33
+ case 'browser':
34
+ return await setupForBrowser(build, pluginOptions);
35
+ default:
36
+ throw new TypeError('Unknown app platform: ' + pluginOptions.app);
37
+ }
38
+ }
39
+ }
40
+ return plugin;
41
+ };
42
+
43
+ export default gjsifyPlugin;
@@ -0,0 +1 @@
1
+ export type App = 'gjs' | 'node' | 'deno' | 'browser';
@@ -0,0 +1,3 @@
1
+ export * from './app.js';
2
+ export * from './plugin-options.js';
3
+ export * from './resolve-alias-options.js';
@@ -0,0 +1,18 @@
1
+ import type { App } from './app.js';
2
+ import type { DeepkitPluginOptions } from '@gjsify/esbuild-plugin-deepkit';
3
+
4
+ export interface PluginOptions extends DeepkitPluginOptions {
5
+ debug?: boolean;
6
+ app?: App;
7
+ aliases?: Record<string, string>;
8
+ /** An array of glob patterns to exclude matches and aliases */
9
+ exclude?: string[];
10
+ jsExtension?: string;
11
+ /** Override the format, only be considered if the target app platform is `'node'`, otherwise it is always `'esm'` */
12
+ format?: 'esm' | 'cjs'
13
+ /**
14
+ * Library Mode
15
+ * Use this if you want to build a library for Gjsify instead of an end user application.
16
+ */
17
+ library?: 'esm' | 'cjs';
18
+ }
@@ -0,0 +1 @@
1
+ export interface ResolveAliasOptions { }
@@ -0,0 +1,59 @@
1
+ import {
2
+ EXTERNALS_NODE,
3
+ EXTERNALS_NPM,
4
+ ALIASES_GENERAL_FOR_DENO,
5
+ ALIASES_NODE_FOR_DENO,
6
+ ALIASES_GJS_FOR_DENO,
7
+ ALIASES_GENERAL_FOR_GJS,
8
+ ALIASES_NODE_FOR_GJS,
9
+ ALIASES_WEB_FOR_GJS,
10
+ ALIASES_GENERAL_FOR_NODE,
11
+ ALIASES_GJS_FOR_NODE,
12
+ ALIASES_WEB_FOR_NODE
13
+ } from "@gjsify/resolve-npm";
14
+
15
+ import type { ResolveAliasOptions } from '../types/index.js';
16
+
17
+ export const setNodeAliasPrefix = (ALIASES: Record<string, string>) => {
18
+ // Also resolve alias names with `npm:${ALIAS}`
19
+ for (const ALIAS in ALIASES) {
20
+ if(ALIAS.startsWith('node:')) {
21
+ continue
22
+ }
23
+ const key = `node:${ALIAS}`;
24
+ if(!ALIASES[key]) ALIASES[key] = ALIASES[ALIAS];
25
+ }
26
+ return ALIASES;
27
+ }
28
+
29
+ const getAliasesGeneralForGjs = (options: ResolveAliasOptions) => ALIASES_GENERAL_FOR_GJS;
30
+ const getAliasesNodeForGjs = (options: ResolveAliasOptions) => setNodeAliasPrefix(ALIASES_NODE_FOR_GJS);
31
+ const getAliasesWebForGjs = (options: ResolveAliasOptions) => ALIASES_WEB_FOR_GJS;
32
+
33
+ const getAliasesGeneralForDeno = (options: ResolveAliasOptions) => ALIASES_GENERAL_FOR_DENO;
34
+ const getAliasesNodeForDeno = (options: ResolveAliasOptions) => setNodeAliasPrefix(ALIASES_NODE_FOR_DENO);
35
+ const getAliasesGjsForDeno = (options: ResolveAliasOptions) => ALIASES_GJS_FOR_DENO;
36
+
37
+ const getAliasesGeneralForNode = (options: ResolveAliasOptions) => ALIASES_GENERAL_FOR_NODE;
38
+ const getAliasesGjsForNode = (options: ResolveAliasOptions) => ALIASES_GJS_FOR_NODE;
39
+ const getAliasesWebForNode = (options: ResolveAliasOptions) => ALIASES_WEB_FOR_NODE;
40
+
41
+
42
+ export const getAliasesForGjs = (options: ResolveAliasOptions) => {
43
+ return {...getAliasesGeneralForGjs(options), ...getAliasesNodeForGjs(options), ...getAliasesWebForGjs(options) }
44
+ }
45
+
46
+ export const getAliasesForDeno = (options: ResolveAliasOptions) => {
47
+ return {...getAliasesGeneralForDeno(options), ...getAliasesNodeForDeno(options), ...getAliasesGjsForDeno(options) }
48
+ }
49
+
50
+ export const getAliasesForNode = (options: ResolveAliasOptions) => {
51
+ return {...getAliasesGeneralForNode(options), ...getAliasesGjsForNode(options), ...getAliasesWebForNode(options) }
52
+ }
53
+
54
+ /** Array of Node.js build in module names (also with node: prefix) */
55
+ export const externalNode = [...EXTERNALS_NODE, ...EXTERNALS_NODE.map(E => `node:${E}`)];
56
+
57
+ /** Array of NPM module names for which we have our own implementation */
58
+ export const externalNPM = [...EXTERNALS_NPM];
59
+
@@ -0,0 +1,33 @@
1
+ import type { BuildOptions } from "esbuild";
2
+ import fastGlob from 'fast-glob';
3
+
4
+ export const globToEntryPoints = async (_entryPoints: BuildOptions['entryPoints'], ignore: string[] = []) => {
5
+
6
+ if(Array.isArray(_entryPoints) && typeof _entryPoints[0] === 'string') {
7
+ return await fastGlob(_entryPoints as string[], {ignore})
8
+ } else if(Array.isArray(_entryPoints) && typeof _entryPoints[0] === 'object') {
9
+ const entryPoints: { in: string, out: string }[] = [];
10
+
11
+ for(const entryPoint of _entryPoints as { in: string, out: string }[]) {
12
+ const inputs = await fastGlob(entryPoint.in, {ignore})
13
+
14
+ for (let i = 0; i < inputs.length; i++) {
15
+ entryPoints.push({
16
+ in: inputs[i],
17
+ out: entryPoint.out
18
+ })
19
+ }
20
+ }
21
+
22
+ return entryPoints;
23
+ }
24
+ const entryPoints: Record<string, string> = {};
25
+ for (const input in _entryPoints as Record<string, string>) {
26
+ const output = entryPoints[input];
27
+ const inputs = await fastGlob(input, {ignore});
28
+ for (const input of inputs) {
29
+ entryPoints[input] = output;
30
+ }
31
+ }
32
+ return entryPoints;
33
+ }
@@ -0,0 +1,7 @@
1
+ export const getJsExtensions = (allowExt: string) => {
2
+ const extensions = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
3
+ if(extensions[allowExt]) {
4
+ delete extensions[allowExt]
5
+ }
6
+ return extensions;
7
+ }
@@ -0,0 +1,3 @@
1
+ export * from './alias.js';
2
+ export * from './entry-points.js';
3
+ export * from './extension.js';
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "ESNext",
4
+ "target": "ESNext",
5
+ "outDir": "dist",
6
+ "declarationDir": "dist/types",
7
+ "declaration": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "moduleResolution": "bundler",
10
+ "allowImportingTsExtensions": true
11
+ },
12
+ "files": ["src/index.ts"]
13
+ }