@gjsify/esbuild-plugin-gjsify 0.0.4 → 0.1.0
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 +32 -0
- package/dist/esm/index.mjs +577 -6223
- package/dist/shims/console-gjs.js +51 -0
- package/dist/types/app/index.d.ts +0 -1
- package/dist/types/types/app.d.ts +1 -1
- package/dist/types/types/plugin-options.d.ts +7 -0
- package/dist/types/utils/alias.d.ts +0 -3
- package/dist/types/utils/entry-points.d.ts +1 -1
- package/dist/types/utils/extension.d.ts +1 -8
- package/dist/types/utils/merge.d.ts +2 -0
- package/dist/types/utils/patch-to-common-js.d.ts +12 -0
- package/esbuild.mjs +20 -35
- package/package.json +19 -15
- package/src/app/browser.ts +2 -4
- package/src/app/gjs.ts +28 -8
- package/src/app/index.ts +0 -1
- package/src/app/node.ts +14 -5
- package/src/lib/lib.ts +1 -1
- package/src/plugin.ts +2 -4
- package/src/shims/console-gjs.ts +19 -0
- package/src/types/app.ts +1 -1
- package/src/types/plugin-options.ts +8 -1
- package/src/utils/alias.ts +1 -14
- package/src/utils/extension.ts +1 -1
- package/src/utils/merge.ts +22 -0
- package/src/utils/patch-to-common-js.ts +45 -0
- package/tsconfig.json +17 -7
- package/dist/cjs/index.cjs +0 -11802
- package/dist/types/alias-plugin.d.ts +0 -2
- package/dist/types/app/deno.d.ts +0 -3
- package/src/app/deno.ts +0 -63
- package/src/lodash.d.ts +0 -46
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
log,
|
|
3
|
+
info,
|
|
4
|
+
debug,
|
|
5
|
+
warn,
|
|
6
|
+
error,
|
|
7
|
+
dir,
|
|
8
|
+
dirxml,
|
|
9
|
+
table,
|
|
10
|
+
time,
|
|
11
|
+
timeEnd,
|
|
12
|
+
timeLog,
|
|
13
|
+
trace,
|
|
14
|
+
assert,
|
|
15
|
+
clear,
|
|
16
|
+
count,
|
|
17
|
+
countReset,
|
|
18
|
+
group,
|
|
19
|
+
groupCollapsed,
|
|
20
|
+
groupEnd,
|
|
21
|
+
profile,
|
|
22
|
+
profileEnd,
|
|
23
|
+
timeStamp
|
|
24
|
+
} from "@gjsify/console";
|
|
25
|
+
let console = {
|
|
26
|
+
log,
|
|
27
|
+
info,
|
|
28
|
+
debug,
|
|
29
|
+
warn,
|
|
30
|
+
error,
|
|
31
|
+
dir,
|
|
32
|
+
dirxml,
|
|
33
|
+
table,
|
|
34
|
+
time,
|
|
35
|
+
timeEnd,
|
|
36
|
+
timeLog,
|
|
37
|
+
trace,
|
|
38
|
+
assert,
|
|
39
|
+
clear,
|
|
40
|
+
count,
|
|
41
|
+
countReset,
|
|
42
|
+
group,
|
|
43
|
+
groupCollapsed,
|
|
44
|
+
groupEnd,
|
|
45
|
+
profile,
|
|
46
|
+
profileEnd,
|
|
47
|
+
timeStamp
|
|
48
|
+
};
|
|
49
|
+
export {
|
|
50
|
+
console
|
|
51
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type App = 'gjs' | 'node' | '
|
|
1
|
+
export type App = 'gjs' | 'node' | 'browser';
|
|
@@ -14,4 +14,11 @@ export interface PluginOptions extends DeepkitPluginOptions {
|
|
|
14
14
|
* Use this if you want to build a library for Gjsify instead of an end user application.
|
|
15
15
|
*/
|
|
16
16
|
library?: 'esm' | 'cjs';
|
|
17
|
+
/**
|
|
18
|
+
* Inject a console shim into GJS builds that uses print()/printerr() instead of
|
|
19
|
+
* GLib.log_structured(). This removes the "Gjs-Console-Message:" prefix and allows
|
|
20
|
+
* ANSI escape codes to be interpreted correctly by the terminal.
|
|
21
|
+
* Only applies to GJS app builds. Default: true.
|
|
22
|
+
*/
|
|
23
|
+
consoleShim?: boolean;
|
|
17
24
|
}
|
|
@@ -3,9 +3,6 @@ export declare const setNodeAliasPrefix: (ALIASES: Record<string, string>) => Re
|
|
|
3
3
|
export declare const getAliasesForGjs: (options: ResolveAliasOptions) => {
|
|
4
4
|
[x: string]: string;
|
|
5
5
|
};
|
|
6
|
-
export declare const getAliasesForDeno: (options: ResolveAliasOptions) => {
|
|
7
|
-
[x: string]: string;
|
|
8
|
-
};
|
|
9
6
|
export declare const getAliasesForNode: (options: ResolveAliasOptions) => {
|
|
10
7
|
[x: string]: string;
|
|
11
8
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BuildOptions } from "esbuild";
|
|
2
|
-
export declare const globToEntryPoints: (_entryPoints: BuildOptions[
|
|
2
|
+
export declare const globToEntryPoints: (_entryPoints: BuildOptions["entryPoints"], ignore?: string[]) => Promise<string[] | Record<string, string> | {
|
|
3
3
|
in: string;
|
|
4
4
|
out: string;
|
|
5
5
|
}[]>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PluginBuild } from 'esbuild';
|
|
2
|
+
/**
|
|
3
|
+
* Fix CJS-ESM interop: esbuild's __toCommonJS wraps ESM modules in a
|
|
4
|
+
* namespace object { __esModule, default, ...namedExports }. CJS code that
|
|
5
|
+
* does `var fn = require('esm-pkg')` gets the namespace instead of the
|
|
6
|
+
* default export. This breaks npm packages like is-promise, depd, etc.
|
|
7
|
+
* that export a single default function consumed by CJS require().
|
|
8
|
+
*
|
|
9
|
+
* The patch makes __toCommonJS return the default export directly when
|
|
10
|
+
* the module has no named exports (only __esModule + default).
|
|
11
|
+
*/
|
|
12
|
+
export declare function registerToCommonJSPatch(build: PluginBuild): void;
|
package/esbuild.mjs
CHANGED
|
@@ -1,49 +1,34 @@
|
|
|
1
1
|
import { build } from 'esbuild';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
import { extname, dirname } from 'path';
|
|
4
2
|
import { EXTERNALS_NODE } from '@gjsify/resolve-npm';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
new URL('./package.json', import.meta.url), 'utf8'
|
|
9
|
-
)
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
if (!pkg.main && !pkg.module) {
|
|
13
|
-
throw new Error("package.json: The main or module property is required!");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const baseConfig = {
|
|
4
|
+
// Build main plugin bundle
|
|
5
|
+
await build({
|
|
17
6
|
entryPoints: ['src/index.ts'],
|
|
18
7
|
bundle: true,
|
|
19
8
|
minify: false,
|
|
20
|
-
platform:
|
|
9
|
+
platform: 'node',
|
|
10
|
+
format: 'esm',
|
|
11
|
+
outfile: 'dist/esm/index.mjs',
|
|
21
12
|
external: [
|
|
22
13
|
...EXTERNALS_NODE,
|
|
23
14
|
'typescript',
|
|
24
15
|
'@deepkit/type-compiler',
|
|
25
16
|
'esbuild',
|
|
26
|
-
// '@gjsify/resolve-npm', can't be required in cjs builds
|
|
27
17
|
'@gjsify/esbuild-plugin-transform-ext',
|
|
28
|
-
'@gjsify/esbuild-plugin-deno-loader',
|
|
29
18
|
'@gjsify/esbuild-plugin-deepkit',
|
|
30
|
-
]
|
|
31
|
-
|
|
19
|
+
],
|
|
20
|
+
banner: {
|
|
21
|
+
js: 'import { createRequire } from "module"; const require = createRequire(import.meta.url);',
|
|
22
|
+
},
|
|
23
|
+
});
|
|
32
24
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
build({
|
|
44
|
-
...baseConfig,
|
|
45
|
-
outdir: dirname(pkg.module),
|
|
46
|
-
format: 'esm',
|
|
47
|
-
outExtension: {'.js': extname(pkg.module)},
|
|
48
|
-
});
|
|
49
|
-
}
|
|
25
|
+
// Build GJS console shim — standalone file injected into GJS bundles at build time.
|
|
26
|
+
// Must be ESM with a named export `console` for esbuild's inject feature.
|
|
27
|
+
await build({
|
|
28
|
+
entryPoints: ['src/shims/console-gjs.ts'],
|
|
29
|
+
bundle: false,
|
|
30
|
+
minify: false,
|
|
31
|
+
platform: 'neutral',
|
|
32
|
+
format: 'esm',
|
|
33
|
+
outfile: 'dist/shims/console-gjs.js',
|
|
34
|
+
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/esbuild-plugin-gjsify",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Deepkit type compiler plugin for esbuild",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/
|
|
7
|
-
"module": "dist/esm/index.mjs",
|
|
6
|
+
"main": "dist/esm/index.mjs",
|
|
8
7
|
"types": "dist/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/types/index.d.ts",
|
|
11
|
+
"default": "./dist/esm/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
9
14
|
"scripts": {
|
|
10
|
-
"clear": "rm -rf dist tsconfig.tsbuildinfo",
|
|
11
|
-
"
|
|
12
|
-
"test": "
|
|
13
|
-
"build": "yarn
|
|
15
|
+
"clear": "rm -rf dist tsconfig.tsbuildinfo || exit 0",
|
|
16
|
+
"check": "tsc --noEmit",
|
|
17
|
+
"test": "echo 'nothing to do'",
|
|
18
|
+
"build": "yarn build:js && yarn build:types",
|
|
14
19
|
"build:js": "node esbuild.mjs",
|
|
15
20
|
"build:types": "tsc --project ./tsconfig.json"
|
|
16
21
|
},
|
|
@@ -20,15 +25,14 @@
|
|
|
20
25
|
"esbuild"
|
|
21
26
|
],
|
|
22
27
|
"dependencies": {
|
|
23
|
-
"@gjsify/esbuild-plugin-alias": "^0.0
|
|
24
|
-
"@gjsify/esbuild-plugin-deepkit": "^0.0
|
|
25
|
-
"@gjsify/esbuild-plugin-
|
|
26
|
-
"@gjsify/
|
|
27
|
-
"fast-glob": "^3.3.
|
|
28
|
-
"lodash": "^4.17.21"
|
|
28
|
+
"@gjsify/esbuild-plugin-alias": "^0.1.0",
|
|
29
|
+
"@gjsify/esbuild-plugin-deepkit": "^0.1.0",
|
|
30
|
+
"@gjsify/esbuild-plugin-transform-ext": "^0.1.0",
|
|
31
|
+
"@gjsify/resolve-npm": "^0.1.0",
|
|
32
|
+
"fast-glob": "^3.3.3"
|
|
29
33
|
},
|
|
30
34
|
"devDependencies": {
|
|
31
|
-
"esbuild": "^0.
|
|
32
|
-
"typescript": "^
|
|
35
|
+
"esbuild": "^0.27.4",
|
|
36
|
+
"typescript": "^6.0.2"
|
|
33
37
|
}
|
|
34
38
|
}
|
package/src/app/browser.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
|
|
2
|
-
import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
|
|
3
2
|
import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
|
|
4
|
-
import { merge } from "
|
|
3
|
+
import { merge } from "../utils/merge.js";
|
|
5
4
|
import { globToEntryPoints } from "../utils/index.js";
|
|
6
5
|
|
|
7
6
|
// Types
|
|
@@ -10,7 +9,7 @@ import type { PluginOptions } from '../types/plugin-options.js';
|
|
|
10
9
|
|
|
11
10
|
export const setupForBrowser = async (build: PluginBuild, pluginOptions: PluginOptions) => {
|
|
12
11
|
|
|
13
|
-
const external = [];
|
|
12
|
+
const external: string[] = [];
|
|
14
13
|
|
|
15
14
|
pluginOptions.aliases ||= {};
|
|
16
15
|
pluginOptions.exclude ||= [];
|
|
@@ -55,6 +54,5 @@ export const setupForBrowser = async (build: PluginBuild, pluginOptions: PluginO
|
|
|
55
54
|
if(pluginOptions.debug) console.debug("initialOptions", build.initialOptions);
|
|
56
55
|
|
|
57
56
|
await aliasPlugin(aliases).setup(build);
|
|
58
|
-
await denoPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
59
57
|
await deepkitPlugin.deepkitPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
60
58
|
}
|
package/src/app/gjs.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
|
|
2
|
-
import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
|
|
3
2
|
import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
|
|
4
|
-
import { merge } from "
|
|
3
|
+
import { merge } from "../utils/merge.js";
|
|
5
4
|
import { getAliasesForGjs, globToEntryPoints } from "../utils/index.js";
|
|
5
|
+
import { registerToCommonJSPatch } from "../utils/patch-to-common-js.js";
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import { dirname, resolve } from 'path';
|
|
6
8
|
|
|
7
9
|
// Types
|
|
8
10
|
import type { PluginBuild, BuildOptions } from "esbuild";
|
|
9
11
|
import type { PluginOptions } from '../types/plugin-options.js';
|
|
10
12
|
|
|
13
|
+
const _shimDir = dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
|
|
11
15
|
export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptions) => {
|
|
12
16
|
|
|
13
17
|
const external = ['gi://*', 'cairo', 'gettext', 'system'];
|
|
@@ -18,7 +22,7 @@ export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptio
|
|
|
18
22
|
|
|
19
23
|
// Set default options
|
|
20
24
|
const esbuildOptions: BuildOptions = {
|
|
21
|
-
format,
|
|
25
|
+
format,
|
|
22
26
|
bundle: true,
|
|
23
27
|
metafile: false,
|
|
24
28
|
minify: false,
|
|
@@ -30,11 +34,15 @@ export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptio
|
|
|
30
34
|
// firefox78 // Since GJS 1.65.90
|
|
31
35
|
// firefox91 // Since GJS 1.71.1
|
|
32
36
|
// firefox102 // Since GJS 1.73.2
|
|
33
|
-
|
|
37
|
+
// firefox128 // Since GJS 1.86.0
|
|
38
|
+
target: [ "firefox128" ],
|
|
34
39
|
platform: 'neutral',
|
|
35
|
-
|
|
40
|
+
// 'browser' field is needed so packages like create-hash, create-hmac, randombytes
|
|
41
|
+
// use their pure-JS browser entry instead of index.js (which does require('crypto')
|
|
42
|
+
// and causes circular dependencies via the crypto → @gjsify/crypto alias).
|
|
43
|
+
mainFields: format === 'esm' ? ['browser', 'module', 'main'] : ['browser', 'main', 'module'],
|
|
36
44
|
// https://esbuild.github.io/api/#conditions
|
|
37
|
-
conditions: format === 'esm' ? ['import', 'require'] : ['require', 'import'],
|
|
45
|
+
conditions: format === 'esm' ? ['browser', 'import', 'require'] : ['browser', 'require', 'import'],
|
|
38
46
|
external,
|
|
39
47
|
loader: {
|
|
40
48
|
'.ts': 'ts',
|
|
@@ -50,9 +58,20 @@ export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptio
|
|
|
50
58
|
define: {
|
|
51
59
|
global: 'globalThis',
|
|
52
60
|
window: 'globalThis',
|
|
61
|
+
// Make readable-stream delegate to @gjsify/stream instead of using
|
|
62
|
+
// its own implementation (avoids 'process is not defined' at load time)
|
|
63
|
+
'process.env.READABLE_STREAM': '"disable"',
|
|
53
64
|
},
|
|
54
65
|
};
|
|
55
66
|
|
|
67
|
+
// Inject the console shim for GJS builds (default: enabled).
|
|
68
|
+
// The shim replaces all `console` references in the bundle with print()/printerr()-based
|
|
69
|
+
// implementations that bypass GLib.log_structured() — no prefix, ANSI codes work.
|
|
70
|
+
if (pluginOptions.consoleShim !== false) {
|
|
71
|
+
// Resolve relative to the compiled shims/ directory next to this file
|
|
72
|
+
esbuildOptions.inject = [resolve(_shimDir, '../shims/console-gjs.js')];
|
|
73
|
+
}
|
|
74
|
+
|
|
56
75
|
merge(build.initialOptions, esbuildOptions);
|
|
57
76
|
|
|
58
77
|
build.initialOptions.entryPoints = await globToEntryPoints(build.initialOptions.entryPoints, pluginOptions.exclude);
|
|
@@ -62,6 +81,7 @@ export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptio
|
|
|
62
81
|
if(pluginOptions.debug) console.debug("initialOptions", build.initialOptions);
|
|
63
82
|
|
|
64
83
|
await aliasPlugin(aliases).setup(build);
|
|
65
|
-
await denoPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
66
84
|
await deepkitPlugin.deepkitPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
67
|
-
|
|
85
|
+
|
|
86
|
+
registerToCommonJSPatch(build);
|
|
87
|
+
}
|
package/src/app/index.ts
CHANGED
package/src/app/node.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
|
|
2
|
-
import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
|
|
3
2
|
import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
|
|
4
|
-
import { merge } from "
|
|
3
|
+
import { merge } from "../utils/merge.js";
|
|
5
4
|
import { getAliasesForNode, globToEntryPoints } from "../utils/index.js";
|
|
5
|
+
import { registerToCommonJSPatch } from "../utils/patch-to-common-js.js";
|
|
6
6
|
import { EXTERNALS_NODE } from "@gjsify/resolve-npm";
|
|
7
7
|
|
|
8
8
|
// Types
|
|
@@ -11,7 +11,7 @@ import type { PluginOptions } from '../types/plugin-options.js';
|
|
|
11
11
|
|
|
12
12
|
export const setupForNode = async (build: PluginBuild, pluginOptions: PluginOptions) => {
|
|
13
13
|
|
|
14
|
-
const external = [...EXTERNALS_NODE, 'gi://*'];
|
|
14
|
+
const external = [...EXTERNALS_NODE, 'gi://*', '@girs/*'];
|
|
15
15
|
const format = pluginOptions.format || 'esm';
|
|
16
16
|
|
|
17
17
|
pluginOptions.aliases ||= {};
|
|
@@ -25,10 +25,18 @@ export const setupForNode = async (build: PluginBuild, pluginOptions: PluginOpti
|
|
|
25
25
|
sourcemap: false,
|
|
26
26
|
treeShaking: true,
|
|
27
27
|
preserveSymlinks: false, // false means follow symlinks
|
|
28
|
-
target: [ "
|
|
28
|
+
target: [ "node24" ],
|
|
29
29
|
platform: "node",
|
|
30
30
|
mainFields: format === 'esm' ? ['module', 'main', 'browser'] : ['main', 'module', 'browser'],
|
|
31
31
|
conditions: format === 'esm' ? ['module', 'import'] : ['require'],
|
|
32
|
+
// In ESM output, CJS require() calls to external modules (Node.js
|
|
33
|
+
// builtins) need a real require function. Node.js ESM doesn't provide
|
|
34
|
+
// one natively, so we create it via createRequire().
|
|
35
|
+
...(format === 'esm' ? {
|
|
36
|
+
banner: {
|
|
37
|
+
js: "import { createRequire as __gjsify_createRequire } from 'module';\nconst require = __gjsify_createRequire(import.meta.url);",
|
|
38
|
+
},
|
|
39
|
+
} : {}),
|
|
32
40
|
external,
|
|
33
41
|
loader: {
|
|
34
42
|
'.ts': 'ts',
|
|
@@ -56,6 +64,7 @@ export const setupForNode = async (build: PluginBuild, pluginOptions: PluginOpti
|
|
|
56
64
|
if(pluginOptions.debug) console.debug("initialOptions", build.initialOptions);
|
|
57
65
|
|
|
58
66
|
await aliasPlugin(aliases).setup(build);
|
|
59
|
-
await denoPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
60
67
|
await deepkitPlugin.deepkitPlugin({reflection: pluginOptions.reflection}).setup(build);
|
|
68
|
+
|
|
69
|
+
registerToCommonJSPatch(build);
|
|
61
70
|
}
|
package/src/lib/lib.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
|
|
2
2
|
import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
|
|
3
|
-
import { merge } from "
|
|
3
|
+
import { merge } from "../utils/merge.js";
|
|
4
4
|
import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
|
|
5
5
|
|
|
6
6
|
// Types
|
package/src/plugin.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Plugin } from "esbuild";
|
|
2
2
|
import type { PluginOptions } from './types/plugin-options.js';
|
|
3
3
|
import { setupLib } from './lib/index.js';
|
|
4
|
-
import { setupForGjs, setupForNode,
|
|
4
|
+
import { setupForGjs, setupForNode, setupForBrowser } from './app/index.js';
|
|
5
5
|
|
|
6
6
|
export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
|
|
7
7
|
const plugin: Plugin = {
|
|
@@ -19,15 +19,13 @@ export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
pluginOptions.app ||= 'gjs'
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
// End user applications or tests
|
|
24
24
|
switch (pluginOptions.app) {
|
|
25
25
|
case 'gjs':
|
|
26
26
|
return await setupForGjs(build, pluginOptions);
|
|
27
27
|
case 'node':
|
|
28
28
|
return await setupForNode(build, pluginOptions);
|
|
29
|
-
case 'deno':
|
|
30
|
-
return await setupForDeno(build, pluginOptions);
|
|
31
29
|
case 'browser':
|
|
32
30
|
return await setupForBrowser(build, pluginOptions);
|
|
33
31
|
default:
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// GJS console shim — injected by esbuild into GJS bundles via the `inject` option.
|
|
2
|
+
// Delegates to @gjsify/console which uses print()/printerr() on GJS, bypassing
|
|
3
|
+
// GLib.log_structured() — no "Gjs-Console-Message:" prefix, ANSI codes interpreted.
|
|
4
|
+
//
|
|
5
|
+
// esbuild resolves the @gjsify/console import at user-build time and deduplicates
|
|
6
|
+
// it with any explicit `import from 'node:console'` in the same bundle.
|
|
7
|
+
import {
|
|
8
|
+
log, info, debug, warn, error, dir, dirxml, table,
|
|
9
|
+
time, timeEnd, timeLog, trace, assert, clear,
|
|
10
|
+
count, countReset, group, groupCollapsed, groupEnd,
|
|
11
|
+
profile, profileEnd, timeStamp,
|
|
12
|
+
} from '@gjsify/console';
|
|
13
|
+
|
|
14
|
+
export let console = {
|
|
15
|
+
log, info, debug, warn, error, dir, dirxml, table,
|
|
16
|
+
time, timeEnd, timeLog, trace, assert, clear,
|
|
17
|
+
count, countReset, group, groupCollapsed, groupEnd,
|
|
18
|
+
profile, profileEnd, timeStamp,
|
|
19
|
+
};
|
package/src/types/app.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export type App = 'gjs' | 'node' | '
|
|
1
|
+
export type App = 'gjs' | 'node' | 'browser';
|
|
@@ -10,9 +10,16 @@ export interface PluginOptions extends DeepkitPluginOptions {
|
|
|
10
10
|
jsExtension?: string;
|
|
11
11
|
/** Override the format */
|
|
12
12
|
format?: 'esm' | 'cjs'
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
14
|
* Library Mode
|
|
15
15
|
* Use this if you want to build a library for Gjsify instead of an end user application.
|
|
16
16
|
*/
|
|
17
17
|
library?: 'esm' | 'cjs';
|
|
18
|
+
/**
|
|
19
|
+
* Inject a console shim into GJS builds that uses print()/printerr() instead of
|
|
20
|
+
* GLib.log_structured(). This removes the "Gjs-Console-Message:" prefix and allows
|
|
21
|
+
* ANSI escape codes to be interpreted correctly by the terminal.
|
|
22
|
+
* Only applies to GJS app builds. Default: true.
|
|
23
|
+
*/
|
|
24
|
+
consoleShim?: boolean;
|
|
18
25
|
}
|
package/src/utils/alias.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EXTERNALS_NODE,
|
|
3
3
|
EXTERNALS_NPM,
|
|
4
|
-
ALIASES_GENERAL_FOR_DENO,
|
|
5
|
-
ALIASES_NODE_FOR_DENO,
|
|
6
|
-
ALIASES_GJS_FOR_DENO,
|
|
7
4
|
ALIASES_GENERAL_FOR_GJS,
|
|
8
5
|
ALIASES_NODE_FOR_GJS,
|
|
9
6
|
ALIASES_WEB_FOR_GJS,
|
|
@@ -15,7 +12,7 @@ import {
|
|
|
15
12
|
import type { ResolveAliasOptions } from '../types/index.js';
|
|
16
13
|
|
|
17
14
|
export const setNodeAliasPrefix = (ALIASES: Record<string, string>) => {
|
|
18
|
-
// Also resolve alias names with `
|
|
15
|
+
// Also resolve alias names with `node:${ALIAS}`
|
|
19
16
|
for (const ALIAS in ALIASES) {
|
|
20
17
|
if(ALIAS.startsWith('node:')) {
|
|
21
18
|
continue
|
|
@@ -30,23 +27,14 @@ const getAliasesGeneralForGjs = (options: ResolveAliasOptions) => ALIASES_GENERA
|
|
|
30
27
|
const getAliasesNodeForGjs = (options: ResolveAliasOptions) => setNodeAliasPrefix(ALIASES_NODE_FOR_GJS);
|
|
31
28
|
const getAliasesWebForGjs = (options: ResolveAliasOptions) => ALIASES_WEB_FOR_GJS;
|
|
32
29
|
|
|
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
30
|
const getAliasesGeneralForNode = (options: ResolveAliasOptions) => ALIASES_GENERAL_FOR_NODE;
|
|
38
31
|
const getAliasesGjsForNode = (options: ResolveAliasOptions) => ALIASES_GJS_FOR_NODE;
|
|
39
32
|
const getAliasesWebForNode = (options: ResolveAliasOptions) => ALIASES_WEB_FOR_NODE;
|
|
40
33
|
|
|
41
|
-
|
|
42
34
|
export const getAliasesForGjs = (options: ResolveAliasOptions) => {
|
|
43
35
|
return {...getAliasesGeneralForGjs(options), ...getAliasesNodeForGjs(options), ...getAliasesWebForGjs(options) }
|
|
44
36
|
}
|
|
45
37
|
|
|
46
|
-
export const getAliasesForDeno = (options: ResolveAliasOptions) => {
|
|
47
|
-
return {...getAliasesGeneralForDeno(options), ...getAliasesNodeForDeno(options), ...getAliasesGjsForDeno(options) }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
38
|
export const getAliasesForNode = (options: ResolveAliasOptions) => {
|
|
51
39
|
return {...getAliasesGeneralForNode(options), ...getAliasesGjsForNode(options), ...getAliasesWebForNode(options) }
|
|
52
40
|
}
|
|
@@ -56,4 +44,3 @@ export const externalNode = [...EXTERNALS_NODE, ...EXTERNALS_NODE.map(E => `node
|
|
|
56
44
|
|
|
57
45
|
/** Array of NPM module names for which we have our own implementation */
|
|
58
46
|
export const externalNPM = [...EXTERNALS_NPM];
|
|
59
|
-
|
package/src/utils/extension.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const getJsExtensions = (allowExt?: string) => {
|
|
2
|
-
const extensions = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
|
|
2
|
+
const extensions: Record<string, string> = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
|
|
3
3
|
if(allowExt && extensions[allowExt]) {
|
|
4
4
|
delete extensions[allowExt]
|
|
5
5
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Deep merge objects (replaces lodash.merge) */
|
|
2
|
+
export function merge<T extends Record<string, any>>(target: T, ...sources: Record<string, any>[]): T {
|
|
3
|
+
for (const source of sources) {
|
|
4
|
+
if (!source) continue;
|
|
5
|
+
for (const key of Object.keys(source)) {
|
|
6
|
+
const targetVal = (target as any)[key];
|
|
7
|
+
const sourceVal = source[key];
|
|
8
|
+
if (sourceVal !== undefined) {
|
|
9
|
+
if (isPlainObject(targetVal) && isPlainObject(sourceVal)) {
|
|
10
|
+
merge(targetVal, sourceVal);
|
|
11
|
+
} else {
|
|
12
|
+
(target as any)[key] = sourceVal;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return target;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isPlainObject(val: unknown): val is Record<string, any> {
|
|
21
|
+
return typeof val === 'object' && val !== null && !Array.isArray(val) && Object.getPrototypeOf(val) === Object.prototype;
|
|
22
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
2
|
+
import type { PluginBuild } from 'esbuild';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fix CJS-ESM interop: esbuild's __toCommonJS wraps ESM modules in a
|
|
6
|
+
* namespace object { __esModule, default, ...namedExports }. CJS code that
|
|
7
|
+
* does `var fn = require('esm-pkg')` gets the namespace instead of the
|
|
8
|
+
* default export. This breaks npm packages like is-promise, depd, etc.
|
|
9
|
+
* that export a single default function consumed by CJS require().
|
|
10
|
+
*
|
|
11
|
+
* The patch makes __toCommonJS return the default export directly when
|
|
12
|
+
* the module has no named exports (only __esModule + default).
|
|
13
|
+
*/
|
|
14
|
+
export function registerToCommonJSPatch(build: PluginBuild): void {
|
|
15
|
+
build.onEnd((result) => {
|
|
16
|
+
if (result.errors.length > 0) return;
|
|
17
|
+
|
|
18
|
+
const outfile = build.initialOptions.outfile;
|
|
19
|
+
if (!outfile) return;
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
let content = readFileSync(outfile, 'utf-8');
|
|
23
|
+
|
|
24
|
+
const toCommonJSPattern =
|
|
25
|
+
/var __toCommonJS = \(mod\d?\) => __copyProps\(__defProp\(\{\}, "__esModule", \{ value: true \}\), mod\d?\);/;
|
|
26
|
+
|
|
27
|
+
if (toCommonJSPattern.test(content)) {
|
|
28
|
+
content = content.replace(toCommonJSPattern,
|
|
29
|
+
`var __toCommonJS = (mod) => {
|
|
30
|
+
var ns = __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
if (typeof ns.default !== "undefined") {
|
|
32
|
+
var keys = Object.keys(ns);
|
|
33
|
+
if (keys.length === 1 || (keys.length === 2 && keys.includes("__esModule"))) return ns.default;
|
|
34
|
+
}
|
|
35
|
+
return ns;
|
|
36
|
+
};`
|
|
37
|
+
);
|
|
38
|
+
writeFileSync(outfile, content);
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
// Non-critical: if patching fails, CJS-ESM interop issues may
|
|
42
|
+
// surface at runtime but the build itself is not broken.
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -2,16 +2,26 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"module": "ESNext",
|
|
4
4
|
"target": "ESNext",
|
|
5
|
+
"rootDir": "src",
|
|
5
6
|
"outDir": "dist",
|
|
6
|
-
"types": [
|
|
7
|
-
|
|
7
|
+
"types": [
|
|
8
|
+
"node"
|
|
9
|
+
],
|
|
10
|
+
"lib": [
|
|
11
|
+
"ESNext",
|
|
12
|
+
"DOM"
|
|
13
|
+
],
|
|
8
14
|
"declarationDir": "dist/types",
|
|
9
15
|
"declaration": true,
|
|
10
16
|
"emitDeclarationOnly": true,
|
|
11
|
-
"allowSyntheticDefaultImports": true,
|
|
12
17
|
"moduleResolution": "bundler",
|
|
13
|
-
"allowImportingTsExtensions": true
|
|
18
|
+
"allowImportingTsExtensions": true,
|
|
19
|
+
"strict": false
|
|
14
20
|
},
|
|
15
|
-
"include": [
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
"include": [
|
|
22
|
+
"src/lodash.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"src/index.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|