@gjsify/esbuild-plugin-gjsify 0.0.2 → 0.0.4

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.
@@ -1,2 +1 @@
1
- export * from './cjs.js';
2
- export * from './esm.js';
1
+ export * from './lib.js';
@@ -1,3 +1,3 @@
1
1
  import type { PluginBuild } from "esbuild";
2
2
  import type { PluginOptions } from '../types/plugin-options.js';
3
- export declare const setupCjsLib: (build: PluginBuild, pluginOptions: PluginOptions) => Promise<void>;
3
+ export declare const setupLib: (build: PluginBuild, pluginOptions: PluginOptions) => Promise<void>;
@@ -7,7 +7,7 @@ export interface PluginOptions extends DeepkitPluginOptions {
7
7
  /** An array of glob patterns to exclude matches and aliases */
8
8
  exclude?: string[];
9
9
  jsExtension?: string;
10
- /** Override the format, only be considered if the target app platform is `'node'`, otherwise it is always `'esm'` */
10
+ /** Override the format */
11
11
  format?: 'esm' | 'cjs';
12
12
  /**
13
13
  * Library Mode
@@ -1,4 +1,4 @@
1
- export declare const getJsExtensions: (allowExt: string) => {
1
+ export declare const getJsExtensions: (allowExt?: string) => {
2
2
  '.js': string;
3
3
  '.ts': string;
4
4
  '.mts': string;
package/esbuild.mjs CHANGED
@@ -17,6 +17,7 @@ const baseConfig = {
17
17
  entryPoints: ['src/index.ts'],
18
18
  bundle: true,
19
19
  minify: false,
20
+ platform: "node",
20
21
  external: [
21
22
  ...EXTERNALS_NODE,
22
23
  'typescript',
@@ -35,7 +36,6 @@ if (pkg.main) {
35
36
  outdir: dirname(pkg.main),
36
37
  format: 'cjs',
37
38
  outExtension: {'.js': extname(pkg.main)},
38
- platform: "node",
39
39
  });
40
40
  }
41
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/esbuild-plugin-gjsify",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Deepkit type compiler plugin for esbuild",
5
5
  "type": "module",
6
6
  "main": "dist/cjs/index.cjs",
@@ -12,7 +12,7 @@
12
12
  "test": "yarn print:name && echo 'nothing to do'",
13
13
  "build": "yarn print:name && yarn build:js && yarn build:types",
14
14
  "build:js": "node esbuild.mjs",
15
- "build:types": "tsc --emitDeclarationOnly || exit 0"
15
+ "build:types": "tsc --project ./tsconfig.json"
16
16
  },
17
17
  "keywords": [
18
18
  "gjs",
@@ -20,27 +20,15 @@
20
20
  "esbuild"
21
21
  ],
22
22
  "dependencies": {
23
- "@gjsify/esbuild-plugin-deepkit": "^0.0.2",
24
- "@gjsify/esbuild-plugin-deno-loader": "^0.0.2",
25
- "@gjsify/esbuild-plugin-transform-ext": "^0.0.2",
26
- "assert": "^2.0.0",
27
- "constants-browserify": "^1.0.0",
28
- "core-js": "^3.31.0",
29
- "crypto-browserify": "^3.12.0",
30
- "esm": "^3.2.25",
31
- "fast-glob": "^3.2.12",
32
- "lodash": "^4.17.21",
33
- "micromatch": "^4.0.5",
34
- "path-browserify": "^1.0.1",
35
- "punycode": "^2.3.0",
36
- "stream-browserify": "^3.0.0",
37
- "string_decoder": "^1.3.0",
38
- "test": "^3.3.0"
23
+ "@gjsify/esbuild-plugin-alias": "^0.0.4",
24
+ "@gjsify/esbuild-plugin-deepkit": "^0.0.4",
25
+ "@gjsify/esbuild-plugin-deno-loader": "^0.0.4",
26
+ "@gjsify/esbuild-plugin-transform-ext": "^0.0.4",
27
+ "fast-glob": "^3.3.2",
28
+ "lodash": "^4.17.21"
39
29
  },
40
30
  "devDependencies": {
41
- "@types/lodash": "^4.14.195",
42
- "@types/micromatch": "^4.0.2",
43
- "esbuild": "^0.18.2",
44
- "typescript": "^5.1.3"
31
+ "esbuild": "^0.19.10",
32
+ "typescript": "^5.3.3"
45
33
  }
46
34
  }
@@ -1,4 +1,4 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
2
2
  import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
3
3
  import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
4
4
  import { merge } from "lodash";
package/src/app/deno.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
2
2
  import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
3
3
  import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
4
4
  import { merge } from "lodash";
@@ -11,13 +11,16 @@ import { getAliasesForDeno, globToEntryPoints } from "../utils/index.js";
11
11
  export const setupForDeno = async (build: PluginBuild, pluginOptions: PluginOptions) => {
12
12
 
13
13
  const external = [];
14
+ const format = pluginOptions.format || 'esm';
15
+
16
+ if(format !== 'esm') throw new TypeError('Only ESM format is supported for Deno');
14
17
 
15
18
  pluginOptions.aliases ||= {};
16
19
  pluginOptions.exclude ||= [];
17
20
 
18
21
  // Set default options
19
22
  const esbuildOptions: BuildOptions = {
20
- format: 'esm',
23
+ format, // Only 'esm' is supported for Deno
21
24
  bundle: true,
22
25
  minify: false,
23
26
  sourcemap: false,
package/src/app/gjs.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
2
2
  import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
3
3
  import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
4
4
  import { merge } from "lodash";
@@ -11,15 +11,16 @@ import type { PluginOptions } from '../types/plugin-options.js';
11
11
  export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptions) => {
12
12
 
13
13
  const external = ['gi://*', 'cairo', 'gettext', 'system'];
14
+ const format = pluginOptions.format || 'esm';
14
15
 
15
16
  pluginOptions.aliases ||= {};
16
17
  pluginOptions.exclude ||= [];
17
18
 
18
19
  // Set default options
19
20
  const esbuildOptions: BuildOptions = {
20
- format: 'esm', // On Gjs we only support esm
21
+ format,
21
22
  bundle: true,
22
- metafile: true,
23
+ metafile: false,
23
24
  minify: false,
24
25
  sourcemap: false,
25
26
  treeShaking: true,
@@ -30,10 +31,10 @@ export const setupForGjs = async (build: PluginBuild, pluginOptions: PluginOptio
30
31
  // firefox91 // Since GJS 1.71.1
31
32
  // firefox102 // Since GJS 1.73.2
32
33
  target: [ "firefox102" ],
33
- platform: "neutral",
34
- mainFields: ['module', 'main'],
34
+ platform: 'neutral',
35
+ mainFields: format === 'esm' ? ['module', 'main'] : ['main', 'module'],
35
36
  // https://esbuild.github.io/api/#conditions
36
- conditions: ['module','import'],
37
+ conditions: format === 'esm' ? ['import', 'require'] : ['require', 'import'],
37
38
  external,
38
39
  loader: {
39
40
  '.ts': 'ts',
package/src/app/node.ts CHANGED
@@ -1,5 +1,4 @@
1
-
2
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
3
2
  import { denoPlugin } from '@gjsify/esbuild-plugin-deno-loader';
4
3
  import * as deepkitPlugin from '@gjsify/esbuild-plugin-deepkit';
5
4
  import { merge } from "lodash";
@@ -13,12 +12,11 @@ import type { PluginOptions } from '../types/plugin-options.js';
13
12
  export const setupForNode = async (build: PluginBuild, pluginOptions: PluginOptions) => {
14
13
 
15
14
  const external = [...EXTERNALS_NODE, 'gi://*'];
15
+ const format = pluginOptions.format || 'esm';
16
16
 
17
17
  pluginOptions.aliases ||= {};
18
18
  pluginOptions.exclude ||= [];
19
19
 
20
- const format = pluginOptions.format || 'esm';
21
-
22
20
  // Set default options
23
21
  const esbuildOptions: BuildOptions = {
24
22
  format,
@@ -29,7 +27,7 @@ export const setupForNode = async (build: PluginBuild, pluginOptions: PluginOpti
29
27
  preserveSymlinks: false, // false means follow symlinks
30
28
  target: [ "node18" ],
31
29
  platform: "node",
32
- mainFields: format === 'esm' ? ['module', 'main'] : ['main', 'module', 'browser'],
30
+ mainFields: format === 'esm' ? ['module', 'main', 'browser'] : ['main', 'module', 'browser'],
33
31
  conditions: format === 'esm' ? ['module', 'import'] : ['require'],
34
32
  external,
35
33
  loader: {
package/src/lib/index.ts CHANGED
@@ -1,2 +1 @@
1
- export * from './cjs.js';
2
- export * from './esm.js';
1
+ export * from './lib.js';
@@ -1,4 +1,4 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
2
2
  import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
3
3
  import { merge } from "lodash";
4
4
  import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
@@ -7,16 +7,19 @@ import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
7
7
  import type { PluginBuild, BuildOptions } from "esbuild";
8
8
  import type { PluginOptions } from '../types/plugin-options.js';
9
9
 
10
- export const setupEsmLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
10
+ export const setupLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
11
+
12
+ const format = pluginOptions.format || 'esm';
11
13
 
12
14
  pluginOptions.aliases ||= {};
13
15
  pluginOptions.exclude ||= [];
14
16
 
15
17
  const esbuildOptions: BuildOptions = {
18
+ format,
16
19
  bundle: false,
17
20
  minify: false,
18
21
  sourcemap: false,
19
- splitting: true, // Works only on esm
22
+ splitting: format === 'esm' ? true : false, // Works only on esm
20
23
  loader: {
21
24
  '.ts': 'ts',
22
25
  '.mts': 'ts',
@@ -29,10 +32,10 @@ export const setupEsmLib = async (build: PluginBuild, pluginOptions: PluginOptio
29
32
  '.js': 'ts',
30
33
  },
31
34
  target: [ "esnext" ],
32
- platform: "browser",
35
+ platform: "neutral",
36
+ mainFields: format === 'esm' ? ['module', 'main'] : ['main'],
33
37
  // https://esbuild.github.io/api/#conditions
34
- conditions: ['module','import'],
35
- format: 'esm'
38
+ conditions: format === 'esm' ? ['module','import'] : ['require'],
36
39
  };
37
40
 
38
41
  merge(build.initialOptions, esbuildOptions);
@@ -0,0 +1,46 @@
1
+ declare module 'lodash' {
2
+ /**
3
+ * Recursively merges own and inherited enumerable properties of source
4
+ * objects into the destination object, skipping source properties that resolve
5
+ * to `undefined`. Array and plain object properties are merged recursively.
6
+ * Other objects and value types are overridden by assignment. Source objects
7
+ * are applied from left to right. Subsequent sources overwrite property
8
+ * assignments of previous sources.
9
+ *
10
+ * **Note:** This method mutates `object`.
11
+ *
12
+ * @category Object
13
+ * @param object The destination object.
14
+ * @param [sources] The source objects.
15
+ * @returns Returns `object`.
16
+ * @example
17
+ *
18
+ * var users = {
19
+ * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
20
+ * };
21
+ *
22
+ * var ages = {
23
+ * 'data': [{ 'age': 36 }, { 'age': 40 }]
24
+ * };
25
+ *
26
+ * _.merge(users, ages);
27
+ * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
28
+ */
29
+ function merge<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
30
+ /**
31
+ * @see _.merge
32
+ */
33
+ function merge<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
34
+ /**
35
+ * @see _.merge
36
+ */
37
+ function merge<TObject, TSource1, TSource2, TSource3>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3;
38
+ /**
39
+ * @see _.merge
40
+ */
41
+ function merge<TObject, TSource1, TSource2, TSource3, TSource4>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4;
42
+ /**
43
+ * @see _.merge
44
+ */
45
+ function merge(object: any, ...otherArgs: any[]): any;
46
+ }
package/src/plugin.ts CHANGED
@@ -1,26 +1,24 @@
1
1
  import type { Plugin } from "esbuild";
2
2
  import type { PluginOptions } from './types/plugin-options.js';
3
- import { setupCjsLib, setupEsmLib } from './lib/index.js';
3
+ import { setupLib } from './lib/index.js';
4
4
  import { setupForGjs, setupForNode, setupForDeno, setupForBrowser } from './app/index.js';
5
5
 
6
6
  export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
7
7
  const plugin: Plugin = {
8
8
  name: 'gjsify',
9
9
  async setup(build) {
10
-
11
10
  // Library mode
12
11
  if(pluginOptions.library) {
13
12
  switch (pluginOptions.library) {
14
13
  case 'esm':
15
- return setupEsmLib(build, pluginOptions)
16
14
  case 'cjs':
17
- return setupCjsLib(build, pluginOptions)
15
+ return setupLib(build, pluginOptions)
18
16
  default:
19
17
  throw new TypeError('Unknown library type: ' + pluginOptions.library);
20
18
  }
21
19
  }
22
20
 
23
- pluginOptions.app ||= 'gjs';
21
+ pluginOptions.app ||= 'gjs'
24
22
 
25
23
  // End user applications or tests
26
24
  switch (pluginOptions.app) {
@@ -8,7 +8,7 @@ export interface PluginOptions extends DeepkitPluginOptions {
8
8
  /** An array of glob patterns to exclude matches and aliases */
9
9
  exclude?: string[];
10
10
  jsExtension?: string;
11
- /** Override the format, only be considered if the target app platform is `'node'`, otherwise it is always `'esm'` */
11
+ /** Override the format */
12
12
  format?: 'esm' | 'cjs'
13
13
  /**
14
14
  * Library Mode
@@ -1,6 +1,6 @@
1
- export const getJsExtensions = (allowExt: string) => {
1
+ export const getJsExtensions = (allowExt?: string) => {
2
2
  const extensions = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
3
- if(extensions[allowExt]) {
3
+ if(allowExt && extensions[allowExt]) {
4
4
  delete extensions[allowExt]
5
5
  }
6
6
  return extensions;
package/tsconfig.json CHANGED
@@ -3,11 +3,15 @@
3
3
  "module": "ESNext",
4
4
  "target": "ESNext",
5
5
  "outDir": "dist",
6
+ "types": ["node"],
7
+ "lib": ["ESNext", "DOM"],
6
8
  "declarationDir": "dist/types",
7
9
  "declaration": true,
10
+ "emitDeclarationOnly": true,
8
11
  "allowSyntheticDefaultImports": true,
9
12
  "moduleResolution": "bundler",
10
13
  "allowImportingTsExtensions": true
11
14
  },
15
+ "include": ["src/lodash.d.ts"],
12
16
  "files": ["src/index.ts"]
13
17
  }
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "esbuild";
2
- export declare const debugPlugin: () => Plugin;
@@ -1,3 +0,0 @@
1
- import type { PluginBuild } from "esbuild";
2
- import type { PluginOptions } from '../types/plugin-options.js';
3
- export declare const setupEsmLib: (build: PluginBuild, pluginOptions: PluginOptions) => Promise<void>;
@@ -1,68 +0,0 @@
1
- import { existsSync } from "fs";
2
- import { realpath } from "fs/promises";
3
-
4
- import type { Plugin } from "esbuild";
5
-
6
- export const aliasPlugin = (aliasObj: Record<string, string>) => {
7
- const aliases = Object.keys(aliasObj);
8
- const re = new RegExp(`^(${aliases.map(x => escapeRegExp(x)).join('|')})$`);
9
-
10
- const plugin: Plugin = {
11
- name: 'alias',
12
- setup(build) {
13
- // we do not register 'file' namespace here, because the root file won't be processed
14
- // https://github.com/evanw/esbuild/issues/791
15
- build.onResolve({ filter: re }, async (args) => {
16
- let resolvedAliasPath = aliasObj[args.path];
17
-
18
- let namespace = args.namespace;
19
-
20
- if (resolvedAliasPath) {
21
-
22
- if (resolvedAliasPath.startsWith('http://')) {
23
- namespace = 'http';
24
- resolvedAliasPath = resolvedAliasPath.slice(5)
25
- } else if (resolvedAliasPath.startsWith('https://')) {
26
- namespace = 'https';
27
- resolvedAliasPath = resolvedAliasPath.slice(6)
28
- } else {
29
- const resolvedAlias = (await build.resolve(resolvedAliasPath, {
30
- importer: args.importer,
31
- kind: args.kind,
32
- namespace: namespace,
33
- resolveDir: args.resolveDir,
34
- pluginData: args.pluginData,
35
- }));
36
-
37
- if (resolvedAlias.errors) {
38
- return resolvedAlias;
39
- } else {
40
- resolvedAliasPath = resolvedAlias.path;
41
- namespace = resolvedAlias.namespace;
42
- }
43
- }
44
-
45
- if (existsSync(resolvedAliasPath)) {
46
- resolvedAliasPath = await realpath(resolvedAliasPath);
47
- }
48
-
49
- // console.debug(`resolvedAliasPath: ${args.path} -> ${resolvedAliasPath}`);
50
-
51
- return {
52
- path: resolvedAliasPath,
53
- namespace: namespace,
54
- }
55
- }
56
-
57
- return null;
58
- });
59
- },
60
- };
61
-
62
- return plugin;
63
- };
64
-
65
- function escapeRegExp(str: string) {
66
- // $& means the whole matched string
67
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
68
- }
package/src/lib/cjs.ts DELETED
@@ -1,46 +0,0 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
2
- import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
3
- import { merge } from "lodash";
4
- import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
5
-
6
- // Types
7
- import type { PluginBuild, BuildOptions } from "esbuild";
8
- import type { PluginOptions } from '../types/plugin-options.js';
9
-
10
- export const setupCjsLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
11
-
12
- pluginOptions.aliases ||= {};
13
- pluginOptions.exclude ||= [];
14
-
15
- const esbuildOptions: BuildOptions = {
16
- bundle: false,
17
- splitting: false, // only works with esm, see https://esbuild.github.io/api/#splitting
18
- minify: false,
19
- sourcemap: false,
20
- loader: {
21
- '.ts': 'ts',
22
- '.mts': 'ts',
23
- '.cts': 'ts',
24
- '.tsx': 'ts',
25
- '.mtsx': 'ts',
26
- '.ctsx': 'ts',
27
- '.mjs': 'ts',
28
- '.cjs': 'ts',
29
- '.js': 'ts',
30
- },
31
- target: ['esnext'],
32
- platform: "browser",
33
- // https://esbuild.github.io/api/#conditions
34
- conditions: ['require'],
35
- format: 'cjs'
36
- };
37
-
38
- merge(build.initialOptions, esbuildOptions);
39
-
40
- build.initialOptions.entryPoints = await globToEntryPoints(build.initialOptions.entryPoints, pluginOptions.exclude)
41
-
42
- if(pluginOptions.debug) console.debug("initialOptions", build.initialOptions);
43
-
44
- await aliasPlugin(pluginOptions.aliases).setup(build);
45
- await transformExtPlugin({ outExtension: getJsExtensions(pluginOptions.jsExtension) }).setup(build);
46
- }